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 -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstdlib>

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;
            }
}
1
2
3
4
5
    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:
1
2
3
4
5
6
7
8
9
10
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.