Multi-dimensional Arrays and cout

create myarray[3][3]
random them and i got :
1 2 3
4 5 6
7 8 9
meh ... then i cout myarray[1][4] and the number should have be zero cause it not has been set yet but it print 4 which is myarray[2][1] !!!
my concept was some thing like this
how myarray look like :
1 2 3 0 0 0 0 0 0 ...
4 5 6 0 0 0 0 0 0 ...
7 8 9 0 0 0 0 0 0 ...
0 0 0 0 0 0 0 0 0 ...

i already done some test :
cout myarray[1][5] and got 5 which is myarray[2][2]
cout myarray[1][9] and got 9 which is myarray[3][3]
then cout myarray[1][10] and i got zero !!!
is Multi-dimensional Arrays look like cycle ?
like
1 2 3 4 5 6 7 8 9 0
4 5 6 ... not tested yet
7 8 9 ... not tested yet
???

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <time.h>
#include <conio.h>
using namespace std;
int matran[3][3];
void main()
{
srand(time(NULL));
for (int i = 1; i <= 4; i++)
{
for (int j = 1; j <= 4; j++)
{
if (i != 4 && j != 4)
matran[i][j] = 1 + rand() % (9 - 1 + 1);
else
matran[i][j]=0;
cout << matran[i][j]<<"("<<i<<"-"<<j << ") ";
}
cout << endl;
}
_getch();
}

this code pointed that myarray[1][4] (matran[1][4]) value was zero !!!

create myarray[3][3]
myarray[1][4]


This is outside the valid array index. Valid index is 0-2. It's pure luck that is doesn't crash.
could somebody explain me about outside the valid array thing ?
limit myarray[3][3]
but myarray[1][4] = myarray[2][1]
did i do anything wrong ?

1(1-1) 2(1-2) 3(1-3) 4(1-4) 2(1-5)...7(1-9) and 0(1-10) < LOL
4(2-1) 2(2-2) 3(2-2)
5(3-1) 6(3-2) 7(3-3)

delete columm
my concept:
keep pushing them to the left
Ex : delete columm 1
1 2 3
4 5 6
7 8 9

2 3
5 6
8 9

Ex 2 : delete columm 2
1 2 3
4 5 6
7 8 9

1 3
4 6
7 9
Last edited on
Arrays are pointers to a specific set of memory. When you have an array of int arr[3] it is a set of 3 sequential memory slots. Array indices can be accessed with regular array notation arr[2] or with pointer notation *(arr+2). So, when you do arr[3] which is not part of the array, it is doing *(arr+3) or trying to deference the next piece of memory. Normally, your compiler will catch the error, and give you an argument out of range exception. However, since your array is two-dimensional, the next piece of memory is from the second part of the array. So, doing arr[0][4] is the equivalent of *(arr+4) or arr[1][1].
Last edited on
Normally, your compiler will catch the error, and give you an argument out of range exception.

No, the compiler will not usually catch out of bounds array access problems. This is why C++ has std::vector and std::array because using raw arrays it is easy to overflow the bounds of your array. And by the way arrays are arrays not pointers, although arrays do decay easily to pointers, there are differences between pointers and arrays.
when you declare an array, you say how many elements it has.
int i[5];

but computers start counting at zero, so valid indices are 0..4 which gives 5 elements.
so i[1] is the second item, not the first.

your loops need to start at zero.
1
2
3
4
5
6
7
8
9
10
 
for (int i = 0; i < 4; i++)
{
   for (int j = 0; j < 4; j++)
   {
      matran[i][j] = 1 + rand() % (9 - 1 + 1);
      cout << matran[i][j]<<"("<<i<<"-"<<j << ") ";
   }
   cout << endl;
}
Last edited on
In a simple array like this, the elements of each row are normally placed in consecutive locations of memory. A 2D array can be thought of as a 1D array where the compiler knows how many elements are in each row.
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
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    cout << " 2D Array\n";
    int a[3][3] =
        { 1, 2, 3,
          4, 5, 6,
          7, 8, 9 };
           
    for (int y=0; y<3; y++)
    {
        for (int x=0; x<3; x++)
            cout << setw(3) << a[y][x];
        cout << endl;
    }

    cout << "\n 1D Array\n";
    int b[9] =
        { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
           
    for (int y=0; y<3; y++)
    {
        for (int x=0; x<3; x++)
            cout << setw(3) << b[y*3 + x];
        cout << endl;
    }

    return 0;          
}


  2D Array
  1  2  3
  4  5  6
  7  8  9

 1D Array
  1  2  3
  4  5  6
  7  8  9

The two examples do the same thing, but in the first case, the compiler does the calculation y*3 + x for us.

also, as tallyman says, you can consider arrays to be pointers.

consider the declaration
int ar[3][3] = { {1,2,3},{4,5,6},{7,8,9} };

this is really an array of arrays. so ar[0] is {1,2,3} and ar[1] is {4,5,6} an array of 3 items.

with range checking turned off you can say something incorrect, such as int i = ar[1][4]

we already know that ar[1][] starts with value 4 above, the element ar[1][4] (index 4 is the 5th element from 0) has a value of 8. so ar[1][4]==8

and also
ar[0][7]==8
and also
ar[2][1]==8
Last edited on
The idea of deleting something from an array doesn't really work like this:
delete columm


Ex 2 : delete columm 2
1 2 3
4 5 6
7 8 9

1 3
4 6
7 9 

All that can really be done is to replace the contents of an element with a new value, thus after 'deleting' column 2, the array would look like this:
1 3 3
4 6 6
7 9 9


However, using C++ vectors, one can do something like that.
http://www.cplusplus.com/reference/vector/vector/erase/



Topic archived. No new replies allowed.