943,719 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1265
  • C RSS
Jun 28th, 2009
0

Number tally in an array

Expand Post »
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.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Jun 29th, 2009
2

Re: Number tally in an array

Your 1st half of the array array[x][0] is never initialized to zero so the values start out with garbage.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Jun 29th, 2009
0

Re: Number tally in an array

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.
Reputation Points: 888
Solved Threads: 114
Nearly a Posting Virtuoso
MosaicFuneral is offline Offline
1,270 posts
since Nov 2008
Jun 29th, 2009
1

Re: Number tally in an array

Quote ...
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 */
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jun 29th, 2009
0

Re: Number tally in an array

Thanks guy's. Works like a charm.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: struct usage
Next Thread in C Forum Timeline: Tabbed text file / output binary values





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


Follow us on Twitter


© 2011 DaniWeb® LLC