Creating a table in c

Thread Solved

Join Date: Oct 2008
Posts: 1
Reputation: Ungodlyrich is an unknown quantity at this point 
Solved Threads: 0
Ungodlyrich Ungodlyrich is offline Offline
Newbie Poster

Creating a table in c

 
0
  #1
Nov 9th, 2008
I wanted to create a simple table by printing lines in between my values with a header at the top and my numbers left justified. However, when I get to a ten digit number or larger, my code seems to break and my count variable goes to zero as far as I can tell, causing a bunch of space to be printed where it doesn't need to be. I was wondering what I'm doing wrong. Thank you for any help you can offer!

  1.  
  2. #include <stdio.h>
  3.  
  4. int main ( int argc, char *argv[] ) {
  5.  
  6. unsigned int y = 1;
  7. int x = 1;
  8. int limit = sizeof(int) * 8;
  9. int z, n, m, count;
  10.  
  11. printf("| x | y | \n");
  12.  
  13. for(z = 0; z < limit - 1; z++){
  14.  
  15. x = x << 1;
  16. y = y << 1;
  17. m = x;
  18. count = 0;
  19.  
  20. while(m > 0){
  21. m = m / 10;
  22. count++;
  23. }
  24.  
  25. printf("|%d", x);
  26.  
  27. for(n = 11; n >= count; n--)
  28. printf(" ");
  29.  
  30. printf("|%u", y);
  31.  
  32. for(n = 11; n >= count; n--)
  33. printf(" ");
  34.  
  35. printf("|\n");
  36.  
  37. return 0 ;
  38. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,909
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Creating a table in c

 
0
  #2
Nov 9th, 2008
You want to print a table with the powers of 2 and you want your integers to be signed and unsigned. You're making it to complicated I think, use \t to separate your output. Or use something like printf("|%d |%u |",x,y);
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Creating a table in c

 
0
  #3
Nov 9th, 2008
The upper limit of an int is 2147483647(32-bit system). When x reaches the value 1073741824, x<<1; multiples that value by 2 which yields 2147483648 which is beyond the upper limit, hence the value goes in a circle and the number -2147483648 gets stored in x and consequently in m. since you've given while(m>0) count never gets incremented and stays at zero.

It would be better to stop your table when m becomes negative.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
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