Having problem with 2D Arrays [Seek Guidance]

So guys I am new to C++ and I just cleared my basics and now I'm studying arrays
Everything goes well
The logic development
The problem solving techniques
The approach
But I dont understand how a 2D Array stores elements
For Example if i type this code below...
I get different numbers from the entered values

What's up with the [3][2] in "cin >> myarr[i][j];"
How do I store values in the rows and columns Im a noob at this please guide me I wanna learn this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int myarr[3][2];
    int i,j;
    for (i=0;i<4;i++) {
        for (j=0;j<3;j++) {
            cin >> myarr[i][j];
        }
    }
    cout << myarr[i][j];
    return 0;
}
So guys I am new to C++ and I just cleared my basics and now I'm studying arrays

If you're just starting on arrays you should be first learning single dimensional arrays (vectors would be even better).


What's up with the [3][2]

These are the sizes of the array. You have an array of 3 arrays of 2 integers. And don't forget that C++ uses zero based arrays/vectors. So your valid indexes (i and j) would be i = 0, 1, 2 and j = 0, 1. Which means that your loops are trying to access the arrays out of bounds, as is that last cout statement.




@jlb I did study 1D Arrays but the problem here is I cant calculate sum of 2D Arrays Row wise or column wise or anything at all I would love if anyone would give me a general idea
I did study 1D Arrays but the problem here is

The biggest problem is that you didn't study single dimensional arrays well enough to learn that arrays start at zero and stop at size - 1.

Your loops are horribly wrong because you are overflowing the bounds of your array.

Also you compound the problem with that last cout, there you're accessing the array even farther out of bounds.

What do you think the values of i and j are after that loop?

Do you realize that you can't print an entire array in one fell swoop? You need to iterate over the array and print each element.

I cant calculate sum of 2D Arrays Row wise or column wise or anything at all I would love if anyone would give me a general idea


How do you calculate the sum for a single dimensional array?

You need to show what you've tried, then maybe someone can point you in the correct direction.

You really, Really, REALLY should be using vectors instead of arrays.


the actual memory layout lesson:

c++ is row major.
if you stored 10 bytes in memory in an array, it is a sequential allocation of 10 bytes each one representing 1 entry.
that is
char x[10];
for(int i = 0; i < 10; i++)
x[i] = i;

memory looks like
{0 1 2 3 4 5 6 7 8 9}
a 2d array aligns it similarly.

char x[3][4];
the memory looks like
{x[0][0] x[0][1] x[0][2] x[0][3] x[1][0] x[1][1] ...etc}

the language knows how to find the correct offset in the above memory block via the [][] notation, but it is still one solid block of memory!

** arrays can be indexed with [][] notation but are NOT always one solid block of memory due to the multiple new statements required to allocate them. They are instead freestanding blocks, each block with # of columns (representing one row). This is sometimes very inefficient as it causes page faults and other issues for large amounts of data on top of the extra calls to allocate and deallocate.

this is low level stuff that is good to know IMHO.






Last edited on
Registered users can post here. Sign in or register to post.