Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)
New Posts  All Forums:Forum Nav:

Programming Challenge (Out-of-Date) - Page 6  

Poll Results: Are you interested in participating in and/or helping organise and post these programming challenges?

 
  • 100% (2)
    I want to participate.
  • 0% (0)
    I want to contribute by helping posting and organise these challenges.
  • 0% (0)
    I'll only take part if other people are willing to participate.
  • 0% (0)
    I can help and participate - I love programming!
  • 0% (0)
    I do not wish to participate or help.
2 Total Votes  
post #51 of 306
From the version that accepts cmd line args

Code:
time ./a.out $56.08
fifty six dollars and eight cents

real0m0.004s
user0m0.001s
sys0m0.003s
C may be messy for this sort of task (string manipulation), but it is fast as always
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
post #52 of 306
Quote:
Originally Posted by alawadhi3000 View Post
Here's my code in C++

After I finished I realized that the program should accepts two numbers after decimal point

Also it only accepts entering the data manually as I want to code it fast

I will try to fix them later
FYI, instead of writing code like this

Code:
i=temp2/100;
if(i==1) cout<<"One";
else if(i==2) cout<<"Two";
else if(i==3) cout<<"Three";
else if(i==4) cout<<"Four";
else if(i==5) cout<<"Five";
else if(i==6) cout<<"Six";
else if(i==7) cout<<"Seven";
else if(i==8) cout<<"Eight";
else if(i==9) cout<<"Nine";
You should write

Code:
i=temp2/100;
switch(i){
 case(1) :
  cout << "One";
  break;
 case(2) :
  cout << "Two";
  break;

//etc, etc

}
It will run faster than cascading else if's
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
post #53 of 306
Quote:
Originally Posted by rabidgnome229 View Post
From the version that accepts cmd line args

Code:
time ./a.out $56.08
fifty six dollars and eight cents

real    0m0.004s
user    0m0.001s
sys    0m0.003s
C may be messy for this sort of task (string manipulation), but it is fast as always
Yeah thats the downside of scripting languages. I was interested though in seeing how much faster it would be in C.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
post #54 of 306
Quote:
Originally Posted by rabidgnome229 View Post
FYI, instead of writing code like this

Code:
i=temp2/100;
if(i==1) cout<<"One";
else if(i==2) cout<<"Two";
else if(i==3) cout<<"Three";
else if(i==4) cout<<"Four";
else if(i==5) cout<<"Five";
else if(i==6) cout<<"Six";
else if(i==7) cout<<"Seven";
else if(i==8) cout<<"Eight";
else if(i==9) cout<<"Nine";
You should write

Code:
i=temp2/100;
switch(i){
 case(1) :
  cout << "One";
  break;
 case(2) :
  cout << "Two";
  break;

//etc, etc

}
It will run faster than cascading else if's
I know, I did what I was fast at
There's a lot of methods (class, another function, using an array to store values ....etc) which is smaller/consumes less memory but as I told you I did what I'm fast at

Thanks anyway
My PC
(23 items)
 
Home Server
(12 items)
 
Aorus X7 DT v7
(11 items)
 
CPUMotherboardGraphicsGraphics
Intel Core i7 5820K ASUS X99 Deluxe NVIDIA MSI GTX1080 EK X NVIDIA MSI GTX1080 EK X 
RAMHard DriveHard DriveOptical Drive
32GB GSkill Trident Z DDR4 3000MHz CL14 Samsung 850 EVO 500GB SSD Seagate 4TB LG 14X BluRay Writer 
CoolingCoolingCoolingCooling
EK ASUS X99 Monoblock 2x EK XE480 Radiators Swiftech MCP35X (Dual DDC) Pump EK X3 250 Reservoir 
OSMonitorKeyboardPower
Windows 10 Pro X64 LG C6 55" UHD OLED Smart TV Logitech G710 Corsair RM1000 1000W 
CaseMouseMouse PadAudio
Corsair 900D Logitech G700S Razer ExactMat Pioneer VSX-930-K Receiver 
AudioAudioAudio
Pioneer S-RS77TB Speakers Pioneer S-RS3SW Subwoofer Logitech G35 
CPUMotherboardGraphicsRAM
Intel Core i7 7820HK Aorus HM175 NVIDIA GTX 1080 Notebook 32GB Kingston Hyper X DDR4 2400MHz 
Hard DriveHard DriveOSMonitor
512GB Samsung SM961 SSD Seagate FireCuda 2TB Windows 10 Pro 17.3" Sharp IGZO 4K 
KeyboardPowerCase
Aorus RGB Aorus 255W Power Adapter Aorus X7 v7 
  hide details  
My PC
(23 items)
 
Home Server
(12 items)
 
Aorus X7 DT v7
(11 items)
 
CPUMotherboardGraphicsGraphics
Intel Core i7 5820K ASUS X99 Deluxe NVIDIA MSI GTX1080 EK X NVIDIA MSI GTX1080 EK X 
RAMHard DriveHard DriveOptical Drive
32GB GSkill Trident Z DDR4 3000MHz CL14 Samsung 850 EVO 500GB SSD Seagate 4TB LG 14X BluRay Writer 
CoolingCoolingCoolingCooling
EK ASUS X99 Monoblock 2x EK XE480 Radiators Swiftech MCP35X (Dual DDC) Pump EK X3 250 Reservoir 
OSMonitorKeyboardPower
Windows 10 Pro X64 LG C6 55" UHD OLED Smart TV Logitech G710 Corsair RM1000 1000W 
CaseMouseMouse PadAudio
Corsair 900D Logitech G700S Razer ExactMat Pioneer VSX-930-K Receiver 
AudioAudioAudio
Pioneer S-RS77TB Speakers Pioneer S-RS3SW Subwoofer Logitech G35 
CPUMotherboardGraphicsRAM
Intel Core i7 7820HK Aorus HM175 NVIDIA GTX 1080 Notebook 32GB Kingston Hyper X DDR4 2400MHz 
Hard DriveHard DriveOSMonitor
512GB Samsung SM961 SSD Seagate FireCuda 2TB Windows 10 Pro 17.3" Sharp IGZO 4K 
KeyboardPowerCase
Aorus RGB Aorus 255W Power Adapter Aorus X7 v7 
  hide details  
post #55 of 306
Quote:
Originally Posted by rabidgnome229 View Post
From the version that accepts cmd line args

Code:
time ./a.out $56.08
fifty six dollars and eight cents

real    0m0.004s
user    0m0.001s
sys    0m0.003s
C may be messy for this sort of task (string manipulation), but it is fast as always

The last time I tested its execution speed was on my macbook pro. I just ran in on my linux box (Slackware 12.1) and it was this.
Code:
$ time ruby challange_1.rb $56.04                                             
"fifty six dollars and four cents"

real    0m0.004s
user    0m0.004s
sys     0m0.000s
I think it would make sense as its not a very big program.
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
post #56 of 306
Quote:
Originally Posted by dangerousHobo View Post
The last time I tested its execution speed was on my macbook pro. I just ran in on my linux box (Slackware 12.1) and it was this.
Code:
$ time ruby challange_1.rb $56.04                                             
"fifty six dollars and four cents"

real    0m0.004s
user    0m0.004s
sys     0m0.000s
I think it would make sense as its not a very big program.
Mine was tested on the low end macbook

Here is the code for the command line version if you want to test for consistency

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFF_SIZE 1024

char *toString(int num);
char *makeString(char *buffer);

int main(int argc, char **argv){

enum { file, arguments } mode;  //describes input type
FILE *infile;
int i = -1;//counts argument position
char read_buffer[BUFF_SIZE], *string, *curr_pos, *decimal_ptr;
int dollars, cents;

if(argc < 2){
printf("Usage: ./a.out [filename][amount...]n");
exit(1);
}else if(argv[1][0] == '$'){
mode = arguments;
i = 1;
}else{
printf("Reading from filen");
mode = file;
infile = fopen(argv[1], NULL);
if(!infile){
printf("Unable to open %sn", argv[1]);
}
fgets(read_buffer, BUFF_SIZE, infile);
printf("%sn", read_buffer);
}

while(((i > 0) && (i < argc)) || ((i < 0) && fgets(read_buffer, BUFF_SIZE, infile))){
if(mode == file)
string = read_buffer;
else
string = argv[i++];

curr_pos = string;
if(*curr_pos != '$'){
printf("Formatting error on input: %sn", string);
exit(1);
}

/* separate decimal from whole number amount */
while(*curr_pos && (*curr_pos != '.')) curr_pos++;
*curr_pos = '';
decimal_ptr = ++curr_pos;
curr_pos = string + 1;

/* get integer values of dollars/cents */
dollars = atoi(curr_pos);
cents = atoi(decimal_ptr);
char *d = (dollars == 1) ? "dollar" : "dollars";
char *c = (cents == 1) ? "cent" : "cents";

printf("%s %s and %s %sn", toString(dollars), d, toString(cents), c);
}
}

char *toString(int num){

char *ones[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
char *teens[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
char *tens[] = {"twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety" };
char buffer[1024], *buff_ptr = buffer, *ret;
buffer[0] = '';

if(num == 0) return makeString(ones[0]);

int index = num/100;
if(index){
strcpy(buff_ptr, ones[index]);
buff_ptr += strlen(ones[index]);
*buff_ptr++ = ' ';
strcpy(buff_ptr, "hundred");
buff_ptr += strlen("hundred");
*buff_ptr = '';
}
num %= 100;

index = num/10;
if(index){
if(buffer[0])
*buff_ptr++ = ' ';

if(index == 1){
strcpy(buff_ptr, teens[num-10]);
return makeString(buffer);
}

strcpy(buff_ptr, tens[index-2]);
buff_ptr += strlen(tens[index-2]);
*buff_ptr = '';
}
num %= 10;

if(num){
if(buffer[0])
*buff_ptr++ = ' ';
strcpy(buff_ptr, ones[num]);
}

return makeString(buffer);
}

char *makeString(char *buffer){
char *ret;
size_t len = strlen(buffer);
if(len > 1024) return NULL;

ret = malloc(sizeof(char) * len);
strcpy(ret, buffer);
}
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
post #57 of 306
Quote:
Originally Posted by rabidgnome229 View Post
Mine was tested on the low end macbook

Here is the code for the command line version if you want to test for consistency
Thanks,

Haha

Code:
$ time ./a.out $56.09                                                         
 fifty six dollars and  nine cents
real    0m0.001s
user    0m0.000s
sys     0m0.000s
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
post #58 of 306
Hmm, when's the next contest?
need to sharpen up my programming skills again =D
    
CPUMotherboardGraphicsRAM
Intel Core2Duo E6750 Asus P5K-SE x550-not a game machine yet 4x1GB Geil Ultra 4-4-4-8 
Hard DriveOptical DriveOSMonitor
WD Raptor 150GB@10000rpm + SG Barracuda 80GB Pioneer 212D 16x DVD+/-RW Windows XP SP3 --> Vista Ultimate 64bit Samsung 22" WS 226BW 
KeyboardPowerCaseMouse
Logitech G11 PowerFlower 600w Blue Thermaltake Kandalf V9000 Black Logitech MX Revolution 
Mouse Pad
Don't have a gaming mouse 
  hide details  
    
CPUMotherboardGraphicsRAM
Intel Core2Duo E6750 Asus P5K-SE x550-not a game machine yet 4x1GB Geil Ultra 4-4-4-8 
Hard DriveOptical DriveOSMonitor
WD Raptor 150GB@10000rpm + SG Barracuda 80GB Pioneer 212D 16x DVD+/-RW Windows XP SP3 --> Vista Ultimate 64bit Samsung 22" WS 226BW 
KeyboardPowerCaseMouse
Logitech G11 PowerFlower 600w Blue Thermaltake Kandalf V9000 Black Logitech MX Revolution 
Mouse Pad
Don't have a gaming mouse 
  hide details  
post #59 of 306
Quote:
Originally Posted by NeoDeGenero View Post
Hmm, when's the next contest?
need to sharpen up my programming skills again =D
You can still submit a solution to this one. Next one gets posted thursday according to DH
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
post #60 of 306
Hey guys. I'll be a little late on posting the next challenge. Worked late tonight! I'll try getting the next challenge though posted tonight.

As for the previous challenge, thanks rabidgnome229 and alawadhi3000 for participating.

rabidgnome provided the most efficient solution. Congrates!
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
BlackMesa
(14 items)
 
  
CPUMotherboardGraphicsRAM
Phenom II x6 Gigabyte XFX RF460 16gb G.Skill 
Hard DriveHard DriveOSMonitor
OCZ Vertex2 Sata II Coorsair Force GS Sata III Debian (testing) Shimian 27" 
KeyboardMouse
Filco w/ blue cherries Who needs a mouse? 
  hide details  
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
This thread is locked  
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Programming Challenge (Out-of-Date)