The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Help adding floating point numbers! C
Discuss Help adding floating point numbers! C in the C Programming forum on Dev Shed. Help adding floating point numbers! C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 2nd, 2005, 03:26 AM
|
 |
Spontaneously Present
|
|
|
|
|
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?
|

December 2nd, 2005, 06:55 AM
|
|
Contributing User
|
|
Join Date: Nov 2005
Posts: 66
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 8
|
|
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
|

December 2nd, 2005, 07:05 AM
|
|
|
|
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.
|

December 2nd, 2005, 07:44 AM
|
|
Contributing User
|
|
Join Date: Nov 2005
Posts: 66
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 8
|
|
?????????
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
|

December 2nd, 2005, 02:50 PM
|
 |
Spontaneously Present
|
|
|
|
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 
|

December 2nd, 2005, 02:51 PM
|
 |
Spontaneously Present
|
|
|
|
jafet and kingcoder thanks for the replies
im going to try the different loop method and initializing them to zero and see what happens 
|

December 2nd, 2005, 03:15 PM
|
 |
Spontaneously Present
|
|
|
|
|
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?
|

December 2nd, 2005, 03:23 PM
|
 |
Contributing User
|
|
|
|
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.
|

December 2nd, 2005, 05:59 PM
|
 |
Spontaneously Present
|
|
|
|
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?
|

December 2nd, 2005, 06:42 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|
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
|

December 3rd, 2005, 02:01 AM
|
 |
Contributed User
|
|
|
|
> 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.
|

December 3rd, 2005, 02:49 AM
|
|
|
|
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.
|

December 3rd, 2005, 05:15 AM
|
|
Contributing User
|
|
Join Date: Nov 2005
Posts: 66
Time spent in forums: 1 Day 3 h 59 m 52 sec
Reputation Power: 8
|
|
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
|

December 3rd, 2005, 07:00 PM
|
 |
Spontaneously Present
|
|
|
|
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?
|

December 3rd, 2005, 07:04 PM
|
 |
Spontaneously Present
|
|
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|