943,945 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1285
  • C RSS
Mar 9th, 2007
0

Newbie question

Expand 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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Savage221 is offline Offline
20 posts
since Oct 2005
Mar 9th, 2007
0

Re: Newbie question

Click to Expand / Collapse  Quote originally posted by Savage221 ...
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.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007
Mar 9th, 2007
0

Re: Newbie question

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
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Mar 9th, 2007
0

Re: Newbie question

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.
Reputation Points: 11
Solved Threads: 13
Junior Poster
Lazaro Claiborn is offline Offline
171 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: can you help me in this program
Next Thread in C Forum Timeline: Project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC