Newbie question

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2005
Posts: 20
Reputation: Savage221 is an unknown quantity at this point 
Solved Threads: 0
Savage221 Savage221 is offline Offline
Newbie Poster

Newbie question

 
0
  #1
Mar 9th, 2007
Hello.

I'm stuck on a part in my program. Some numbers are adding up properly, others aren't. I assume the problem is that I'm using integers instead of doubles, however, how would you format the following in C?

How to describe it, it's like a chain. I have to take a percentage of an initial value and add it to the next number. Then take a percentage of the next number and add that to the next number. The numbers get pretty big, and the percentages vary, 5%, 10%, 30%. For the most part, everything works out nice and well, but theres a few numbers that are off by 1, either +1 or -1. If I try to add something like +0.5 after the statement it throws off other numbers. So, logically it seems reasonable I'd need to switch from ints to doubles, but that confuses me as to how the output will look. The output cannot be in decimal form. Can you take a double number and print it out as an integer would look?

Heres how the current int output looks:
  1. printf("%4d", solution);

The 4 space format is necessary. Is there a way to take a type double number, say, 5.32 and make it appear on the output as just "5" yet still be formatted with the 4 spaces?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Newbie question

 
0
  #2
Mar 9th, 2007
Originally Posted by Savage221 View Post
Hello.

I'm stuck on a part in my program. Some numbers are adding up properly, others aren't. I assume the problem is that I'm using integers instead of doubles, however, how would you format the following in C?

How to describe it, it's like a chain. I have to take a percentage of an initial value and add it to the next number. Then take a percentage of the next number and add that to the next number. The numbers get pretty big, and the percentages vary, 5%, 10%, 30%. For the most part, everything works out nice and well, but theres a few numbers that are off by 1, either +1 or -1. If I try to add something like +0.5 after the statement it throws off other numbers. So, logically it seems reasonable I'd need to switch from ints to doubles, but that confuses me as to how the output will look. The output cannot be in decimal form. Can you take a double number and print it out as an integer would look?

Heres how the current int output looks:
  1. printf("%4d", solution);
The 4 space format is necessary. Is there a way to take a type double number, say, 5.32 and make it appear on the output as just "5" yet still be formatted with the 4 spaces?
Yes, it is called casting. Consider the following:
  1. double d1 = 5.21;
  2. int num;
  3. num = (int)d1;
  4. printf("%.2d", (double)num);

Output = 5.00

Also, I wrote a decimal-to-integer function that allows you to convert decimal-to-integer rounding it off no matter how long the float portion of the number is.
Link to snippet: http://www.daniweb.com/code/snippet652.html
Code:
  1. int Double2Integer(char *fnum) {
  2. int integeri = atoi(fnum),i,j,k;
  3. char hex = 0x30;
  4. i=k=0;
  5. while (fnum[i] != '.') i++;
  6. j = strlen(fnum);
  7. for (;j>i;j--) {
  8. if (fnum[j] >= 5 && fnum[j] <= 9) {
  9. while (fnum[j-1] != hex)
  10. hex++;
  11. hex++;
  12. fnum[j-1] = hex;
  13. }
  14. }
  15. if (fnum[i+1] >= 5 && fnum[i+1] <= 9)
  16. integeri++;
  17. return integeri;
  18. }

The only downfall is that you have to conver the double to a
string of characters (i.e char a[] = "5.5421"). If that doesn't
answer you question, let me know.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 9th, 2007 at 2:32 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Newbie question

 
0
  #3
Mar 9th, 2007
You could also do something like this:
  1. printf("%4d", (int)ceil(solution - 0.5));
This'll print the rounded value without actually changing the double, so your other numbers should be fine.

#include <math.h> for the ceil function
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 171
Reputation: Lazaro Claiborn is an unknown quantity at this point 
Solved Threads: 13
Lazaro Claiborn's Avatar
Lazaro Claiborn Lazaro Claiborn is offline Offline
Junior Poster

Re: Newbie question

 
0
  #4
Mar 9th, 2007
Infraction's way works as well. Here is just a simple way to convert from double to char to accommodate the latter option:

  1. char dblBuf[8];
  2. sprintf(dbBuf,"%4.2f",solution);
  3. printf("%4d",Double2Integer(dbBuf));

It is good to have options if a given option is good in and of itself.

Good luck, LamaBot
Last edited by Lazaro Claiborn; Mar 9th, 2007 at 5:53 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC