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

New Free Tools on Dev Shed!

#1
December 2nd, 2005, 04:26 AM
 mwild8
Spontaneously Present

Join Date: Jul 2005
Posts: 1,149
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 117
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?

#2
December 2nd, 2005, 07:55 AM
 kingcoder
Contributing User

Join Date: Nov 2005
Posts: 66
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 9
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 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
```

#3
December 2nd, 2005, 08:05 AM
 jafet
Redpill

Join Date: Nov 2005
Posts: 1,660
Time spent in forums: 1 Week 6 Days 11 h 19 m 55 sec
Reputation Power: 151
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.

#4
December 2nd, 2005, 08:44 AM
 kingcoder
Contributing User

Join Date: Nov 2005
Posts: 66
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 9
?????????

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

#5
December 2nd, 2005, 03:50 PM
 mwild8
Spontaneously Present

Join Date: Jul 2005
Posts: 1,149
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 117
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
```

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

#6
December 2nd, 2005, 03:51 PM
 mwild8
Spontaneously Present

Join Date: Jul 2005
Posts: 1,149
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 117
jafet and kingcoder thanks for the replies

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

#7
December 2nd, 2005, 04:15 PM
 mwild8
Spontaneously Present

Join Date: Jul 2005
Posts: 1,149
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 117
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?

#8
December 2nd, 2005, 04:23 PM
 Dave Sinkula
Contributing User

Join Date: Apr 2004
Posts: 1,676
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.

#9
December 2nd, 2005, 06:59 PM
 mwild8
Spontaneously Present

Join Date: Jul 2005
Posts: 1,149
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 117
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?

#10
December 2nd, 2005, 07:42 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,539
Time spent in forums: 2 Months 3 Days 9 h 5 m
Reputation Power: 4106
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

#11
December 3rd, 2005, 03:01 AM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,266
Time spent in forums: 2 Months 4 Weeks 1 Day 18 h 19 m 15 sec
Reputation Power: 1827
> 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.

#12
December 3rd, 2005, 03:49 AM
 jafet
Redpill

Join Date: Nov 2005
Posts: 1,660
Time spent in forums: 1 Week 6 Days 11 h 19 m 55 sec
Reputation Power: 151
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.

#13
December 3rd, 2005, 06:15 AM
 kingcoder
Contributing User

Join Date: Nov 2005
Posts: 66
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 9

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

#14
December 3rd, 2005, 08:00 PM
 mwild8
Spontaneously Present

Join Date: Jul 2005
Posts: 1,149
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 117
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?

#15
December 3rd, 2005, 08:04 PM
 mwild8
Spontaneously Present

Join Date: Jul 2005
Posts: 1,149
Time spent in forums: 1 Week 3 Days 15 h 26 m 2 sec
Reputation Power: 117
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

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Help adding floating point numbers! C