### Array outputting zero's instead of numbers

Hey guys,

I'm getting frustrated over this problem because I've tried everything within my knowledge to fix it but it still won't give me what I want.

I have set up a function 'populate_matrix' which has for loops in it to populate the array 'matrix[100][100]'. A user is supposed to enter the desired width and height of the array, and then the function will populate it with random numbers from the 'generate' function. (I'm only concentrating on matrix A, not B at the moment).

When I run the program, the correct amount of rows and columns is outputted based on the user's choice, but instead of each element being populated with a random number, it just spits out a zero so that every element is a zero :/

There are some bits of code in there that has no purpose yet, but it's not messing with the rest of the program.

I know it's probably something stupid, but its got me stumped. Posted below is my code -

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#include #include using namespace std; int populate_matrix (int matrix[100][100], int height, int width); bool multiply_matrix (int matrix_a [100][100], int aheight, int awidth, int matrix_b [100][100], int bheight, int bwidth, int matrix_c [100][100], int& cheight, int& cwidth); void print_matrix (int matrix [100][100], int height, int width); long int seeder(); int generate (int); int main() { int number, aheight, awidth, bheight, bwidth, height, width; int matrix[100][100]; int A [100][100]; int B [100][100]; int C [100][100]; seeder(); cout << "\nMatrix A Height = "; cin >> aheight; cout << "\nMatrix A Width = "; cin >> awidth; cout << "\nMatrix B Height = "; cin >> bheight; cout << "\nMatrix B Width = "; cin >> bwidth; int populate_matrix (int matrix[100][100], int height, int width); for ( height = 0; height < aheight; height++ ) { for ( width = 0; width < awidth; width++ ) cout << matrix [height][width]; cout << endl; } } long int seeder() { long int sprout; cout << "Please input any number (no larger than 9 digits please) of your choosing." << endl; cout << "It will be used by the program to generate random numbers for the matrix.\n" << endl; cout << "Enter your chosen number - "; cin >> sprout; srand( sprout ); return sprout; } int generate(int number) { number = rand() % 100; return number; } int populate_matrix (int matrix[100][100], int height, int width) { int aheight, awidth, number, input; for ( height = 0; height < aheight; height++ ) { for ( width = 0; width < awidth; width++ ) input = generate(number); matrix[height][width] = input; } }``````
 ``12345`` `````` cin >> bwidth; int populate_matrix (int matrix[100][100], int height, int width); for ( height = 0; height < aheight; height++ )``````

The bold line is not a call of a function. It is a declaration of a function.
Last edited on
your seeder function is supposed to return a long int, but this value is not used in main.

Same poblem with populate_matrix.

This bit has problems with braces:
 ``12345678910`` ``````int populate_matrix (int matrix[100][100], int height, int width) { int aheight, awidth, number, input; for ( height = 0; height < aheight; height++ ) { for ( width = 0; width < awidth; width++ ) { input = generate(number); matrix[height][width] = input; } }``````

HTH
Last edited on
Thanks guys!

Your suggestions helped - I fixed the call/declaration function issue and added in the appropriate bracketing and things became more obvious.

I assigned new variable names at certain intervals to certain values as the program ran (which was writing over them as it ran) and it started spitting out the numbers I wanted.

Thanks once again :)
Topic archived. No new replies allowed.