Basic exercise: to manage a table >> unexpected values

Hi,
I am learning C++ on this website.
First, I would like to thank the authors for the work here: the tutorials are well done.

I am doing some tutorials to understand the basic functions, and am stuck with the tables (which are called "arrays" here as far as I understand).

My little exercise below:
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
#include <iostream>

using namespace std;

int main()
{
    Init:
        // table initialisation
        int tab1[5][5];
        int x=0;
        int y=0;
        int v=0;
        for (int y=1;y<6;y=y+1)
        {
            for (int x=1;x<6;x=x+1)
            {
                v=v+1;
                tab1[y][x]=v;
            }
        }
    HomeScr:
        // table display
        cout << "***** Table *****\n";
        for (int y=1;y<6;y=y+1)
        {
            for (int x=1;x<6;x=x+1)
            {
                v=tab1[y][x];
                cout << "[" << x << "/" << y << ":";
                if (v<10) cout << "0" << v;
                if (v>9) cout << v;
                cout << "] ";
                if (x>4) cout << "\n";
            }
        }
        // input
        cout << "\nInput (column row new value): ";
        cin >> x >> y >> v;
        int b=0;
        if (x<1||x>5||y<1||y>5) b=1;
        if (b==0)
        {
            tab1[y][x]=v;
            cout << "\n";
            goto HomeScr;
        }
        if (b==1)
        {
            cout << "Process terminated.";
        }
    return 0;
}

Dead simple. The objective is to display a 5x5 table with 0-99 values, and to request input and update the table accordingly (input = column number (space) row number (space) new value).

The problem is that some numbers display unexpected values.
First, the initialisation: the value 5/4 should display "20", but does not. Same for value 4/5 and 5/5.
Second, when I perform 1 input, several values are modified, with unexpected results.

That doesn't make any sense. I have tried to re-write the code several times, manage the values the tidiest way possible, have double-checked all instructions, the issue remains... There is something I am missing here, but I can't find what.

I use Code::Blocks 13.12.

I don't know how to add screenshots here or to copy/paste the display of my console to illustrate my problem.
Last edited on
Array is start from 0, so declaration array[5] will have an index from 0 to 4
in your loop array at index 0 is never touch, and you are try to access array at index 5 which is never exist thats the unexpected result come from
That works!

Thank you very much for your reply. That was it.

I have modified the code to manage 0-4 instead of 1-5 for the table ranges, and it now displays the values as expected. I still can't get used to have table slots starting at 0 and not 1.

The behavior of the previous version still makes little sense to me, as the issue should have affected all the values of the last column if I understood well, but it did not... Anyway, I understand now how I should manage that. Thank you.
Topic archived. No new replies allowed.