hello need help with an array/matrix

Great forum.
Last edited on
Comments:

1. No ";" after line 11 (Your lines)
2. do-while unnecessary
3. array[row = 3] @ line 19 is flawed syntax. You set row equal to 3, and check if array[3] is not equal to 0. array[x] is never equal to zero because it's a multi-dimensional array and should thus be implemented as an int **.
4. <= prints 0-3 elements, 4 elements total. You only have 3.
5. You can generalize the enter number expression by simply incrementing (I used arithmetic), which changed line 18.

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
#include <iostream>

int main()
{
    int row,column;
    int array[3][3]; // i put 3 because its 3 numbers across up to 3 numbers down

    for (row = 0; row < 3; row++)
        for (column = 0; column < 3; column++)
        {
            std::cout << "ENTER NUMBER " << (column + 1) + 3 * (row) << " : ";
            std::cin >> array[row][column];
        }

    for (row = 0; row < 3; row++)
    {
        for (column = 0; column < 3; column++)
        {
            std::cout << array[row][column] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << "\n\nand here you have a matrix\n";
    return 0;
}
3. array[row = 3] @ line 19 is flawed syntax. You set row equal to 3, and check if array[3] is not equal to 0. array[x] is never equal to zero because it's a multi-dimensional array and should thus be implemented as an int


thanks for the help but i was wondering if you could elaborate this statement for me im trying to understand it but you kinda lost me there. :)
Alright.

Let's examine that statement:

array[row = 3]

The first thing that happens is that you assign 3 to row. Now you have (how the computer interprets it):

array[3]

What value does that give you? It gives you a value that points to another array. However, 3 is out of bounds since you the only valid ones are 0, 1, and 2. These are the 3 elements you declared the array with.

I don't know if you know about pointers, but these are quite equivalent:

1
2
int **array_1;
int array_2[3][3];


Thus:
1
2
array_1[int]; // returns int *
array_2[int]; // returns int * 


When we put numbers inside if statements, and such number is not 0, then the expression evaluates to true. It's really hard to explain if you don't know about pointers =/.
yea....
but on another note why is it that you put ** in front of array_1 what does that do? does that have to do with the pointers?
Last edited on
Yes, that's a 2D pointer, int ** points to int *, which points to int. I recommend reading the tutorial http://www.cplusplus.com/doc/tutorial/pointers/
o ok thanks i really appreciate it.
Topic archived. No new replies allowed.