Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 07-24-08   #51 (permalink)
With great difficulty
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,210

Rep: 614 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 370
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-24-08   #52 (permalink)
With great difficulty
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,210

Rep: 614 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 370
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-24-08   #53 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,485

Rep: 409 dangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven member
Unique Rep: 215
FAQs Submitted: 7
Trader Rating: 0
Default

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.
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn

Try out the latest Programming Challenge
Quote:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

CPU-Z Validation
@ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15


Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64 & Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 07-24-08   #54 (permalink)
Programmer
 
alawadhi3000's Avatar
 
intel nvidia

Join Date: Oct 2005
Location: Bahrain
Posts: 1,766

Rep: 147 alawadhi3000 is acknowledged by manyalawadhi3000 is acknowledged by many
Unique Rep: 106
Folding Team Rank: 312
Hardware Reviews: 5
Trader Rating: 28
Default

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
__________________
GeForce 8800GTS 320MB/9600GT/8800GT

3DMark06 11518/10963/13622
3DMark05 17948/17446/23147
3DMark03 36902/36240/42768
3DMark01 49944/43675/53827

Total 116312/109324/133364

System: My Rig
CPU
Q9550 @ 3.4GHz + E8400 @ 3.6GHz 1.15v 24/7
Motherboard
Intel DX48BT2
Memory
4GB OCZ Intel Extreme DDR3-1600 (2X2GB)
Graphics Card
XFX 8800GT Alpha Dog Zalman Edition
Hard Drive
500GB Seagate 7200.12 + 1TB WD Green + 320GB WD
Sound Card
SigmaTel STAC9274D
Power Supply
Antec True Power 750W
Case
Thermaltake Soprano
CPU cooling
Thermaltake Big Typhoon + CM 90CFM Fan
GPU cooling
Zalman Stock Heatsink
OS
Windows 7 X64
Monitor
ASUS VH242HL-P
alawadhi3000 is offline I fold for Overclock.net   Reply With Quote
Old 07-24-08   #55 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,485

Rep: 409 dangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven member
Unique Rep: 215
FAQs Submitted: 7
Trader Rating: 0
Default

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.
Attached Thumbnails
Programming Challenge-snapshot2.jpg  
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn

Try out the latest Programming Challenge
Quote:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

CPU-Z Validation
@ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15


Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64 & Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 07-24-08   #56 (permalink)
With great difficulty
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,210

Rep: 614 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 370
FAQs Submitted: 6
Trader Rating: 5
Default

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);
}
__________________
System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-24-08   #57 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,485

Rep: 409 dangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven member
Unique Rep: 215
FAQs Submitted: 7
Trader Rating: 0
Default

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
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn

Try out the latest Programming Challenge
Quote:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

CPU-Z Validation
@ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15


Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64 & Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline Overclocked Account dangerousHobo's Gallery   Reply With Quote
Old 07-27-08   #58 (permalink)
Programmer
 
NeoDeGenero's Avatar
 
intel ati

Join Date: Jul 2008
Location: Sydney Australia
Posts: 28

Rep: 0 NeoDeGenero Unknown
Unique Rep: 0
Trader Rating: 0
Default

Hmm, when's the next contest?
need to sharpen up my programming skills again =D

System: "The Black Thing with blue lights" per my gf ^^
CPU
Intel Core2Duo E6750
Motherboard
Asus P5K-SE
Memory
4x1GB Geil Ultra 4-4-4-8
Graphics Card
x550-not a game machine yet
Hard Drive
WD Raptor 150GB@10000rpm + SG Barracuda 80GB
Sound Card
Onboard Realtek 5.1
Power Supply
PowerFlower 600w Blue
Case
Thermaltake Kandalf V9000 Black
CPU cooling
Thermaltake MiniTyphoon
GPU cooling
Stock
OS
Windows XP SP3 --> Vista Ultimate 64bit
Monitor
Samsung 22" WS 226BW
NeoDeGenero is offline   Reply With Quote
Old 07-27-08   #59 (permalink)
With great difficulty
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,210

Rep: 614 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 370
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Old 07-31-08   #60 (permalink)
Photography nut
 
dangerousHobo's Avatar
 
amd nvidia

Join Date: Dec 2005
Location: ~/
Posts: 3,485

Rep: 409 dangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven memberdangerousHobo is a proven member
Unique Rep: 215
FAQs Submitted: 7
Trader Rating: 0
Default

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!
__________________
"UNIX was never designed to keep people from doing stupid things, because that policy would also keep them from doing clever things." - Doug Gwyn

Try out the latest Programming Challenge
Quote:
Originally Posted by Melcar
Only one reasonable way to solve this... a dance off.

CPU-Z Validation
@ 2.97-prime95 stable 16 hours @ 1.48v Proof | CPU-Z Validation @ 3.15


Getting Mouse Side Buttons to work in Linux, Compile a custom Kernel, More

System: Anomaly
CPU
Athlon 3700 SD(KACAE)0546 @3.02ghz
Motherboard
DFI UT nF4 Ultra-D
Memory
G.Skill 2x512 UTT(BH-5)
Graphics Card
evga 6800gs
Hard Drive
Maxtor 300GB + WD 250GB
Sound Card
onboard
Power Supply
Ultra 500w V-series
Case
one from Ultra
CPU cooling
Big Typhoon
GPU cooling
80mm fan mounted on
OS
Arch64 & Slackware 12.1
Monitor
Acer AL2216W 22" WS LCD
dangerousHobo is offline Overclocked Account dangerousHobo's Gallery   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 05:05 PM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.17582 seconds with 9 queries