Number tally in an array

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

Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 17
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Number tally in an array

 
0
  #1
Jun 28th, 2009
Writting a program which involves tallying generated numbers. I'm having problems with the tallying part. It takes a number, and finds it in the second row of an array, and tallies (add 1) to the same position in the first row. Here's my extracted code:
  1. #include <stdio.h>
  2.  
  3. void tally(int number, int output[][2]); /* variable length */
  4.  
  5. int main() {
  6. int array[4][2];
  7. array[0][1] = 4;
  8. array[1][1] = 3;
  9. array[2][1] = 2;
  10. array[3][1] = 1;
  11.  
  12. tally(2, array);
  13. tally(2, array);
  14. tally(1, array);
  15. tally(4, array);
  16.  
  17. printf("%d, %d, %d, %d\n", array[0][0], array[1][0], array[2][0],
  18. array[3][0]);
  19.  
  20. return 0;
  21. }
  22.  
  23. void tally(int number, int output[][2]) {
  24. /* This function will find a number on the second row, and tally (add 1) to its equivalent place on the first row. */
  25.  
  26. int count = 0;
  27.  
  28. while(1) {
  29. if(output[count][1] == number) {
  30. output[count][0]++;
  31. return;
  32. }
  33. count++;
  34. }
  35. }
Yes I know it has no security yet, so don't pass a number which isn't in the array.
The program seems to print out either random memory locations, or random pointers:
  1. -1074781680, -1074781672, -1074781664, -1074781656
Any Idea's? Thanks.
Last edited by Hiroshe; Jun 28th, 2009 at 11:02 pm.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Number tally in an array

 
2
  #2
Jun 29th, 2009
Your 1st half of the array array[x][0] is never initialized to zero so the values start out with garbage.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 978
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Number tally in an array

 
0
  #3
Jun 29th, 2009
When you set a pointer and don't always use every "point" in it(especially while dealing with user input), you need memset() to set a default value.
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 133
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: Number tally in an array

 
1
  #4
Jun 29th, 2009
you need memset() to set a default value.
memset() is not needed. You can do it without calling a function by using an initializer for the array:
  1. int array[4][2] = {0}; /* init all elements to 0 */
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 17
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Number tally in an array

 
0
  #5
Jun 29th, 2009
Thanks guy's. Works like a charm.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 578 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC