| | |
Number tally in an array
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
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:
Any Idea's? Thanks.
C Syntax (Toggle Plain Text)
#include <stdio.h> void tally(int number, int output[][2]); /* variable length */ int main() { int array[4][2]; array[0][1] = 4; array[1][1] = 3; array[2][1] = 2; array[3][1] = 1; tally(2, array); tally(2, array); tally(1, array); tally(4, array); printf("%d, %d, %d, %d\n", array[0][0], array[1][0], array[2][0], array[3][0]); return 0; } void tally(int number, int output[][2]) { /* This function will find a number on the second row, and tally (add 1) to its equivalent place on the first row. */ int count = 0; while(1) { if(output[count][1] == number) { output[count][0]++; return; } count++; } }
The program seems to print out either random memory locations, or random pointers:
C Syntax (Toggle Plain Text)
-1074781680, -1074781672, -1074781664, -1074781656
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
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
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
•
•
•
•
you need memset() to set a default value.
c Syntax (Toggle Plain Text)
int array[4][2] = {0}; /* init all elements to 0 */
-Tommy (For Great Justice!) Gunn
![]() |
Similar Threads
- How do i want to create random number for dynamic array variables ? (Visual Basic 4 / 5 / 6)
- Random number generation from an array without repeats (C)
- Random number generation from an array in C programming (C++)
- Frequency Counter (C)
- Number of times a number appears in an array (C++)
- counts how many times the number 5 appears in array? (C++)
- Insert a number in an array (C++)
- help with inserting a number into an array (C++)
Other Threads in the C Forum
- Previous Thread: struct usage
- Next Thread: Tabbed text file / output binary values
Views: 578 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






