C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 2nd, 2005, 03:26 AM
mwild8's Avatar
mwild8 mwild8 is offline
Spontaneously Present
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2005
Posts: 1,149 mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 116
Help adding floating point numbers! C

ive been at this problem for a while... i cant seem to get a correct result...
heres the full code:

Code:
#include <stdio.h>

void main()
{	
	double amounts[5];
	double units[5];
	double prices[5] = {9.92, 6.32, 12.63, 5.95, 10.29}; 
	double total;
	double temp;
	int i = 0;
	int j = 0;
	int m = 0;
	int n = 0;
	int p = 0;
	int q = 0;

	//Get the users input
	for (i = 0; i < 5; ++i)
	{
		printf( "Please enter number: " );  
		scanf("%lf", &units[i]);  
	}
	printf("\n");
		
		//Show the user what they entered
		for (j = 0; j < 5; ++j)
		{
			printf("You Entered: %lf\n", units[j]);
		}
		printf("\n");
		
			//add the prices and units together
			printf("Adding the Prices and Units together...\n");
			for (m = 0; m < 5; ++m)
			   for (q = 0; q < 5; ++q)
			   {
				amounts[m] = prices[m] + units[m];
				//Create total 
				total += amounts[q];
			   }
			   printf("\n");
			   printf("...Done");
			   printf("\n");
			   printf("\n");
		 		
		 			//Create ghetto *** table
		 			printf("Printing the Results in a nice table...\n");
		 			printf("\n");
		 			printf("Price\t\t Units\t\t Amount\n"); 
		 			printf("-----\t\t -----\t\t ------\n");
					
					for (p = 0; p < 5; ++p)
					{
						printf("%lf", prices[p]); 
						printf("\t");
						printf("%lf", units[p]);
						printf("\t");
						printf("%lf", amounts[p]);
						printf("\n");
					}
					printf("\n");
		 			printf("     \t\t     \t\t --------\n");
		 			printf("Total\t\t     \t\t$%lf", total);
		 			printf("\n");
		 			
		 				//end the gay C session
		 				printf("\n");
		 				printf("Press enter to exit...");
						scanf();
 }


this part:
Code:
for (m = 0; m < 5; ++m)
			   for (q = 0; q < 5; ++q)
			   {
				amounts[m] = prices[m] + units[m];
				//Create total 
				total += amounts[q];
			   }


the total is what i need to get.. the logic seems fine, im just getting an outrageous result..

any ideas? do i need to include something special?

Reply With Quote
  #2  
Old December 2nd, 2005, 06:55 AM
kingcoder kingcoder is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 66 kingcoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 8
Arrow i think problem is with logic

Quote:
Originally Posted by mwild8
ive been at this problem for a while... i cant seem to get a correct result...
heres the full code:

Code:
#include <stdio.h>

void main()
{	
	double amounts[5];
	double units[5];
	double prices[5] = {9.92, 6.32, 12.63, 5.95, 10.29}; 
	double total;
	double temp;
	int i = 0;
	int j = 0;
	int m = 0;
	int n = 0;
	int p = 0;
	int q = 0;

	//Get the users input
	for (i = 0; i < 5; ++i)
	{
		printf( "Please enter number: " );  
		scanf("%lf", &units[i]);  
	}
	printf("\n");
		
		//Show the user what they entered
		for (j = 0; j < 5; ++j)
		{
			printf("You Entered: %lf\n", units[j]);
		}
		printf("\n");
		
			//add the prices and units together
			printf("Adding the Prices and Units together...\n");
			for (m = 0; m < 5; ++m)
			   for (q = 0; q < 5; ++q)
			   {
				amounts[m] = prices[m] + units[m];
				//Create total 
				total += amounts[q];
			   }
			   printf("\n");
			   printf("...Done");
			   printf("\n");
			   printf("\n");
		 		
		 			//Create ghetto *** table
		 			printf("Printing the Results in a nice table...\n");
		 			printf("\n");
		 			printf("Price\t\t Units\t\t Amount\n"); 
		 			printf("-----\t\t -----\t\t ------\n");
					
					for (p = 0; p < 5; ++p)
					{
						printf("%lf", prices[p]); 
						printf("\t");
						printf("%lf", units[p]);
						printf("\t");
						printf("%lf", amounts[p]);
						printf("\n");
					}
					printf("\n");
		 			printf("     \t\t     \t\t --------\n");
		 			printf("Total\t\t     \t\t$%lf", total);
		 			printf("\n");
		 			
		 				//end the gay C session
		 				printf("\n");
		 				printf("Press enter to exit...");
						scanf();
 }


this part:
Code:
for (m = 0; m < 5; ++m)
			   for (q = 0; q < 5; ++q)
			   {
				amounts[m] = prices[m] + units[m];
				//Create total 
				total += amounts[q];
			   }


the total is what i need to get.. the logic seems fine, im just getting an outrageous result..

any ideas? do i need to include something special?



u dont need to add anything special

i think thing which is going wrong here is u are adding to ur total

amounts[0] when q=1,
amounts[0],amounts[1] when q=2
amounts[0],amounts[1],amount[2] when q=3


change these things i think it should work

1 Code:
Original - 1 Code
    for(q=0;q<5;q++) {     for(m=0;m<5;m++)     {         amount[m]=price[m]+units[m];     }     total+=amount[q]; }



i still believe this is better one

using a single for loop

2 Code:
Original - 2 Code
    for(m=0;m<5;m++) {            amount[m]=price[m]+units[m];              total+=amount[m]; }


3 Code:
Original - 3 Code
    though most compilers(99%) would take default 0 to total very very few wont so try to initialize total also 

Reply With Quote
  #3  
Old December 2nd, 2005, 07:05 AM
jafet jafet is offline
Redpill
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2005
Posts: 1,660 jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 11 h 19 m 55 sec
Reputation Power: 150
Send a message via MSN to jafet
You didn't initialize "total" and "amounts" in the first place!

A simple debug session in my compiler did the trick.

Output:
total: 1.61318 e-307
amounts: 7.06831 e+268
7.70516 e+268
1.6211 e-307
1.69779 e-313
9.92728 e-315

The fix is to set total and amounts[] to 0.0 when the program starts.

Remember: if variables aren't assigned values, they get arbitrary ramdom values.
No matter what you're gonna do with a variable, you should always initialize it to 0.
If you do this:

int* ptr;
*ptr = 10;

You are trying to play with a random area of memory, and the OS may attempt to terminate your program.

Reply With Quote
  #4  
Old December 2nd, 2005, 07:44 AM
kingcoder kingcoder is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 66 kingcoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 8
Exclamation ?????????

Quote:
Originally Posted by jafet
You didn't initialize "total" and "amounts" in the first place!

A simple debug session in my compiler did the trick.

Output:
total: 1.61318 e-307
amounts: 7.06831 e+268
7.70516 e+268
1.6211 e-307
1.69779 e-313
9.92728 e-315

The fix is to set total and amounts[] to 0.0 when the program starts.

Remember: if variables aren't assigned values, they get arbitrary ramdom values.
No matter what you're gonna do with a variable, you should always initialize it to 0.
If you do this:

int* ptr;
*ptr = 10;

You are trying to play with a random area of memory, and the OS may attempt to terminate your program.



are u sure that array elements wont be initialized to 0
and also double data type

Reply With Quote
  #5  
Old December 2nd, 2005, 02:50 PM
mwild8's Avatar
mwild8 mwild8 is offline
Spontaneously Present
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2005
Posts: 1,149 mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 116
Quote:
Originally Posted by kingcoder
u dont need to add anything special

i think thing which is going wrong here is u are adding to ur total

amounts[0] when q=1,
amounts[0],amounts[1] when q=2
amounts[0],amounts[1],amount[2] when q=3


change these things i think it should work

1 Code:
Original - 1 Code
    for(q=0;q<5;q++) {     for(m=0;m<5;m++)     {         amount[m]=price[m]+units[m];     }     total+=amount[q]; }



i still believe this is better one

using a single for loop

2 Code:
Original - 2 Code
    for(m=0;m<5;m++) {            amount[m]=price[m]+units[m];              total+=amount[m]; }


3 Code:
Original - 3 Code
    though most compilers(99%) would take default 0 to total very very few wont so try to initialize total also 


thanks for the awesome reply!

what bothers me is that this will not work..
Code:
for(m=0;m<5;m++)
{

           amount[m]=price[m]+units[m];
 
           total+=amount[m];
}


also i would like to use i for all loops instead of having one counter for each loop.. thats just redundant and stupid! i should be able to use i over again.. maybe if i initialized it to 0 before each loop?

i write mostly PHP and i have forgotten most of the important rules

Reply With Quote
  #6  
Old December 2nd, 2005, 02:51 PM
mwild8's Avatar
mwild8 mwild8 is offline
Spontaneously Present
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2005
Posts: 1,149 mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 116
jafet and kingcoder thanks for the replies

im going to try the different loop method and initializing them to zero and see what happens

Reply With Quote
  #7  
Old December 2nd, 2005, 03:15 PM
mwild8's Avatar
mwild8 mwild8 is offline
Spontaneously Present
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2005
Posts: 1,149 mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 116
double amounts[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
double units[5] = {0.0, 0.0, 0.0, 0.0, 0.0};


that produces errors... any ideas why?

Reply With Quote
  #8  
Old December 2nd, 2005, 03:23 PM
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Apr 2004
Posts: 1,676 Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level)Dave Sinkula User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
That shouldn't produce errors, and it's easier to write like this.
Code:
double amounts[5] = {0};
double units[5] = {0};
__________________
Any advertisement triggered by IntelliTxt in this post is not endorsed by the author of this post.

Reply With Quote
  #9  
Old December 2nd, 2005, 05:59 PM
mwild8's Avatar
mwild8 mwild8 is offline
Spontaneously Present
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2005
Posts: 1,149 mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 116
Quote:
Originally Posted by Dave Sinkula
That shouldn't produce errors, and it's easier to write like this.
Code:
double amounts[5] = {0};
double units[5] = {0};


wouldnt i have to specify a zero for all five variables in that array?

Reply With Quote
  #10  
Old December 2nd, 2005, 06:42 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 15 m 18 sec
Reputation Power: 4080
Nope. That initializes all the elements to 0.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #11  
Old December 3rd, 2005, 02:01 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,906 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 1 h 31 m 41 sec
Reputation Power: 1774
> total += amounts[q];
I still can't see where you do
total = 0;
before starting to accumulate the sum.

> that produces errors... any ideas why?
1000's of possible reasons - how about actually posting the error messages you got? This isn't www.psychicprogrammers.com

It's been a while since you posted the whole code, and it seems you've made many changes. Perhaps it would be a good idea to repost your most current best effort, and what problems you still have.

Reply With Quote
  #12  
Old December 3rd, 2005, 02:49 AM
jafet jafet is offline
Redpill
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2005
Posts: 1,660 jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level)jafet User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 11 h 19 m 55 sec
Reputation Power: 150
Send a message via MSN to jafet
When intializing variables in C and C++ (and probably C# for that matter, but I could be wrong), the program just allocates a little chunk of CPU RAM off for itself. This RAM is NOT set to 0. It is just a random string of 1's and 0's.

Why doesn't C/C++ behave like PHP in initializing all elements to 0? This is because C/C++ is a very low-level language, and interacts with the CPU directly. You're expected to do the initialization. Also, if you decide to initialize it yourself, C/C++ doesn't do redundant work initializing it before you re-initialize it again, thus making things more efficient.

A bit of RAM (and hard disk space) is unused by the system doesn't mean it is empty.

So, you must remember to init all your variables to 0, otherwise you'll get really wacky calculation results.

Reply With Quote
  #13  
Old December 3rd, 2005, 05:15 AM
kingcoder kingcoder is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Posts: 66 kingcoder User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 8
Lightbulb

Quote:
Originally Posted by mwild8
thanks for the awesome reply!

what bothers me is that this will not work..
Code:
for(m=0;m<5;m++)
{

           amount[m]=price[m]+units[m];
 
           total+=amount[m];
}


also i would like to use i for all loops instead of having one counter for each loop.. thats just redundant and stupid! i should be able to use i over again.. maybe if i initialized it to 0 before each loop?

i write mostly PHP and i have forgotten most of the important rules


if u have something called C compiler u better check it out
if that doesnt work!!!!!!!!!!

and if u use only one variable for nested for loops which needs some tasks to be specified like above one after inner for loop is executed once then i cant see how can u change
the value of i for respective iterations for outer loops without a reference

and basically if anyones asking where he is going wrong ,u must point where he is going wrong not write a code which u like or may be a better one before pointing out his her mistake
if u write mostly php then i cant understand in C rules???????????

as once i said ataleast try to run code once in ur compiler before u post

Reply With Quote
  #14  
Old December 3rd, 2005, 07:00 PM
mwild8's Avatar
mwild8 mwild8 is offline
Spontaneously Present
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2005
Posts: 1,149 mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 116
Quote:
Originally Posted by salem
> total += amounts[q];
I still can't see where you do
total = 0;
before starting to accumulate the sum.

> that produces errors... any ideas why?
1000's of possible reasons - how about actually posting the error messages you got? This isn't www.psychicprogrammers.com

It's been a while since you posted the whole code, and it seems you've made many changes. Perhaps it would be a good idea to repost your most current best effort, and what problems you still have.


Code:
        double amounts[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; 
	double units[5] = {0.0, 0.0, 0.0, 0.0, 0.0};
	double prices[5] = {9.92, 6.32, 12.63, 5.95, 10.29}; 
	double total = 0.0;


this is not working, particularly this part

Code:
        double amounts[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; 
	double units[5] = {0.0, 0.0, 0.0, 0.0, 0.0};


the program just shuts down after i enter the values, and the window should stay open because i have a scanf() at the end

is it just my ****ty compiler? or is this wrong? if you declare an array is it automatically initialized?

Reply With Quote
  #15  
Old December 3rd, 2005, 07:04 PM
mwild8's Avatar
mwild8 mwild8 is offline
Spontaneously Present
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2005
Posts: 1,149 mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level)mwild8 User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 116
Quote:
Originally Posted by jafet
When intializing variables in C and C++ (and probably C# for that matter, but I could be wrong), the program just allocates a little chunk of CPU RAM off for itself. This RAM is NOT set to 0. It is just a random string of 1's and 0's.

Why doesn't C/C++ behave like PHP in initializing all elements to 0? This is because C/C++ is a very low-level language, and interacts with the CPU directly. You're expected to do the initialization. Also, if you decide to initialize it yourself, C/C++ doesn't do redundant work initializing it before you re-initialize it again, thus making things more efficient.

A bit of RAM (and hard disk space) is unused by the system doesn't mean it is empty.

So, you must remember to init all your variables to 0, otherwise you'll get really wacky calculation results.


check out my above post in regards to initialization.

thanks for the input! definitely cleared up a lot

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Help adding floating point numbers! C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap