Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge #3
New Posts  All Forums:Forum Nav:

Programming Challenge #3 - Page 4

post #31 of 57
Man I spent a lot of time on this one. I wanted to do it in C++ which I haven't used for a while because I've been using C recently and I totally forgot how to work with the string class instead of char []. Finally I think I got it.

Here's the code:
Code:
#include <iostream>
#include <string>
using namespace std;
int main(){
        string input, output;
        cout << "Enter a number: ";
        cin >> input;
        int b = 2 - ((input.size() - 1) % 3);
        for(int a = 0; a < input.size(); a++, b++){
                if(b % 3 == 0 && a != 0)
                        output += ',';
                output += input[a];
        }
        cout << output << endl;
        return 0;
}

Here's what it does:

OCN3.png
My rig
(18 items)
 
  
CPUMotherboardGraphicsRAM
4770k M power EVGA GTX 980 SC G.SKILL Sniper (2 x 4GB) DDR3 1600 9-9-9-24 
Hard DriveCoolingOSMonitor
512GB Samsung 940 Pro h100 Windows 8 Home premium Asus VG248QE 
MonitorKeyboardPowerCase
Asus VG248QE Corsair K90 Kingwin Lazer Gold 850w Fractal Design Arc Midi R2 
MouseMouse PadAudioAudio
Logitech G700s Corsair Asus Xonar M-Audio BX5 d2 
Audio
krk 10s 
  hide details  
Reply
My rig
(18 items)
 
  
CPUMotherboardGraphicsRAM
4770k M power EVGA GTX 980 SC G.SKILL Sniper (2 x 4GB) DDR3 1600 9-9-9-24 
Hard DriveCoolingOSMonitor
512GB Samsung 940 Pro h100 Windows 8 Home premium Asus VG248QE 
MonitorKeyboardPowerCase
Asus VG248QE Corsair K90 Kingwin Lazer Gold 850w Fractal Design Arc Midi R2 
MouseMouse PadAudioAudio
Logitech G700s Corsair Asus Xonar M-Audio BX5 d2 
Audio
krk 10s 
  hide details  
Reply
post #32 of 57
Code:
#include <iostream>
#include <string>
using namespace std;

int main()
{
        string userNumber;
        cout << "Enter a number: ";
        cin >> userNumber;
        cout << endl << endl;

        for(int i = userNumber.size(); i > 3; i -= 3)
        {
                userNumber = userNumber.substr(0, i - 3) + "," + userNumber.substr(i - 3);
        }

        cout << userNumber << endl;

        system("PAUSE");
        return 0;
}
Theres mine.
Edit: its a lot like Seventh_Badgers post on Page 2, its not - I dont plagiarize.
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
post #33 of 57
Quote:
Originally Posted by FiX View Post

Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
        string userNumber;
        cout << "Enter a number: ";
        cin >> userNumber;
        cout << endl << endl;
        for(int i = userNumber.size(); i > 3; i -= 3)
        {
                userNumber = userNumber.substr(0, i - 3) + "," + userNumber.substr(i - 3);
        }
        cout << userNumber << endl;
        system("PAUSE");
        return 0;
}
Theres mine.
Edit: its a lot like Seventh_Badgers post on Page 2, its not - I dont plagiarize.

THIEF! tongue.gif
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #34 of 57
Done recursively in Java:
Code:
public class Comma {
        public static String addComma(String str) {
                if (str.length() <= 3)
                        return str;
                else
                        return addComma(str.substring(0, str.length() - 3)) + "," + str.substring(str.length() - 3);
        }

        public static void main(String[] args) {
                if (args.length > 0)
                        System.out.println(args[0] + "->" + addComma(args[0]));
        }
}

If I have time, I'm going to try it in x86 asm.
Workstation
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i7 920 c0 @ 3.50 Asus Sabertooth X58 Nvidia gtx 570 Nvidia gtx 210 
RAMHard DriveOptical DriveOptical Drive
12 GB (Patriot 4GB DDR3 1600 Mhz + G.Skill Ripj... OCZ Vertex II 60GB + x2 WD 1TB + WD 500 GB Lite-On DVD Burner LG Blu Ray Burner 
OSMonitorMonitorMonitor
Windows 8 Professional x64, Arch Linux x64 Samsung 22 inch 1920x1080 60Hz Asus 23 inch 1920x1080 IPS Acer 19 inch 1600x900 
KeyboardPowerCaseMouse
Logitech g11 Corsair 750 Watt NZXT Tempest Razer deathadder 3500 dpi 
Mouse PadAudioAudio
OCZ Audigy SE Sony MDR-V6 
  hide details  
Reply
Workstation
(19 items)
 
  
CPUMotherboardGraphicsGraphics
Intel i7 920 c0 @ 3.50 Asus Sabertooth X58 Nvidia gtx 570 Nvidia gtx 210 
RAMHard DriveOptical DriveOptical Drive
12 GB (Patriot 4GB DDR3 1600 Mhz + G.Skill Ripj... OCZ Vertex II 60GB + x2 WD 1TB + WD 500 GB Lite-On DVD Burner LG Blu Ray Burner 
OSMonitorMonitorMonitor
Windows 8 Professional x64, Arch Linux x64 Samsung 22 inch 1920x1080 60Hz Asus 23 inch 1920x1080 IPS Acer 19 inch 1600x900 
KeyboardPowerCaseMouse
Logitech g11 Corsair 750 Watt NZXT Tempest Razer deathadder 3500 dpi 
Mouse PadAudioAudio
OCZ Audigy SE Sony MDR-V6 
  hide details  
Reply
post #35 of 57
Quote:
Originally Posted by Seventh Badger View Post

THIEF! tongue.gif

I promise I didnt steal it tongue.gif I made mine, posted it and then went browsing through to see how others did it. Urs was quite similar tongue.gif
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
post #36 of 57
Quote:
Originally Posted by FiX View Post

I promise I didnt steal it tongue.gif I made mine, posted it and then went browsing through to see how others did it. Urs was quite similar tongue.gif

Well great minds think alike! lol
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
Viking
(13 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 2500K @ 4.8 GHz 1.45V P8Z68-V GEN3 [2x] EVGA GeForce GTX 570 SLI [2x] Kingston HyperX 4GB 1600MHz 
Hard DriveHard DriveCoolingCooling
Crucial M4 128GB [2x] WD 320GB XSPC Single Bay Reservoir Swiftech MCP655 
CoolingCoolingOSPower
XSPC Raystorm XSPC EX240 Windows 7 Pro 64-bit Corsair HX850 
Case
Corsair Obsidian 650D 
  hide details  
Reply
post #37 of 57
I tried to do mine without any special functions, just a loop and a strlen (which i could replicate with another loop)
Its in C btw Warning: Spoiler! (Click to show)
Code:
#include <stdio.h>

void main()
{
        char buffer[256];
        char szNewString[256];
        int iNewIndex, iOldIndex, iRealLength, iIteration;
        
        printf("Enter your number: ");
        gets(buffer);
        
        iIteration = 0;
        iOldIndex = strlen(buffer);     
        iRealLength = iNewIndex = (iOldIndex - 1) / 3 + iOldIndex;
        
        while(iNewIndex >= 0)
        {
                szNewString[iNewIndex] = buffer[iOldIndex];
                if(!(iIteration % 3) && iNewIndex != iRealLength)
                {
                        iNewIndex--;
                        szNewString[iNewIndex] = ',';
                }
                
                iIteration++;
                iOldIndex--;
                iNewIndex--;
        }
        
        printf("\n%s!\n", szNewString);
}
pooter scooter
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 D0 stepping evga x58 SLI LE EVGA geforce 260 GTX 55nm 6gb dominator 7-7-7-20 1600 
Hard DriveOSMonitorPower
2tb caviar black + 32 gb intel SSD windows 7 24" DELL hx850 corsair 
Case
corsair obsidian 800d 
  hide details  
Reply
pooter scooter
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 D0 stepping evga x58 SLI LE EVGA geforce 260 GTX 55nm 6gb dominator 7-7-7-20 1600 
Hard DriveOSMonitorPower
2tb caviar black + 32 gb intel SSD windows 7 24" DELL hx850 corsair 
Case
corsair obsidian 800d 
  hide details  
Reply
post #38 of 57
Maybe in a month or two I'll come back and do this. Got a book on beginning C++ for Christmas, and it is proving interesting lunch break material. I feel I should add that I have zero experience with coding.
post #39 of 57
MINE!
Code:
#include <iostream>
#include <string>
using namespace std;

void main()
{
        string input;
        cout << "enter a number: ";
        cin >> input;
        for(int x=input.length()-3;x>0;x-=3)
        {
                input.insert(x,",");
        }
        cout << "result: " << input << endl;
}
Budget Build!
(15 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 955 Black Edition MSI 870A-G54 ATI radeon 6850 G Skill 8GB DDR3 PC12800 
Hard DriveHard DriveCoolingOS
Seagate Barracuda 7200.12A 500GB Crucial M4 CoolerMaster 212+ Windows 7 Home Premium 64bit 
MonitorKeyboardPowerCase
LG 18.5" 1943SE-PF Logitech Aerocool 500W PSU Fractal Design R3 
MouseMouse Pad
Razer Deathadder Razer Mantis 
  hide details  
Reply
Budget Build!
(15 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II X4 955 Black Edition MSI 870A-G54 ATI radeon 6850 G Skill 8GB DDR3 PC12800 
Hard DriveHard DriveCoolingOS
Seagate Barracuda 7200.12A 500GB Crucial M4 CoolerMaster 212+ Windows 7 Home Premium 64bit 
MonitorKeyboardPowerCase
LG 18.5" 1943SE-PF Logitech Aerocool 500W PSU Fractal Design R3 
MouseMouse Pad
Razer Deathadder Razer Mantis 
  hide details  
Reply
post #40 of 57
Quote:
Originally Posted by icyblizard View Post

MINE!
Code:
#include <iostream>
#include <string>
using namespace std;
void main()
{
        string input;
        cout << "enter a number: ";
        cin >> input;
        for(int x=input.length()-3;x>0;x-=3)
        {
                input.insert(x,",");
        }
        cout << "result: " << input << endl;
}

Maybe its because its late here, but I dont think your code is 100%. Wouldn't a number such as 12345678 result in a string of 123,456,78 which is not correct?
Sokar
(20 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 3.75 GHz EVGA x58 3x SLI EVGA 670 4GB 12GB Samsung PC3 12800 
Hard DriveHard DriveHard DriveHard Drive
Cruical M4 WD Caviar Black WD Caviar Black Seagate 
CoolingOSMonitorKeyboard
Custom WC Loop Windows 7 x64 x3 Dell U2312HM I-ROCKS KR-6820E-BK 
PowerCaseMouseMouse Pad
Ultra x3 1000w Danger Den Torture Rack Razer Deathadder Black Razer Goliathis 
AudioAudioAudio
Inifitity P153 Speakers Lepai TA202A Amp Asus Xonar DG 
  hide details  
Reply
Sokar
(20 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 3.75 GHz EVGA x58 3x SLI EVGA 670 4GB 12GB Samsung PC3 12800 
Hard DriveHard DriveHard DriveHard Drive
Cruical M4 WD Caviar Black WD Caviar Black Seagate 
CoolingOSMonitorKeyboard
Custom WC Loop Windows 7 x64 x3 Dell U2312HM I-ROCKS KR-6820E-BK 
PowerCaseMouseMouse Pad
Ultra x3 1000w Danger Den Torture Rack Razer Deathadder Black Razer Goliathis 
AudioAudioAudio
Inifitity P153 Speakers Lepai TA202A Amp Asus Xonar DG 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge #3