简体   繁体   中英

simple 2D array matrix in c++

im absolut new to cpp, I tried to code a simple 2D array matrix. Here is what I'm talking about:


#include <iostream>
#include <string>

using namespace std;


void printTable()
{

    int tabelle [10][10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int i, j;
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10; j++)
        {
            printf("%d   ", tabelle[i][j]);
        }
        printf("\n");
    }
}


int main(int argc, char* argv[])
{
    printTable();


    return 0;
}

This is how it looks like:

1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0

And this is how it should look like:

1 2 3 4 5 6 7 8 9 10
2 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0
6 0 0 0 0 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0

Thx for any advise:D

That's not how array assignments work in C++. Think of 2D arrays as a 1D array of 1D arrays.

int tabelle[10][10] = {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, {2, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {3, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {4, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {5, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {6, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {7, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {8, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {9, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {10, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
  • your code is working correctly, 2d array is set of one dimension array.
  • here 2d array tabelle[10][10]'s first one dimension array tablelle[0] is initialized with elements.
  • One more thing partially initialized array fills the rest of the element with
    zero, that is why all other element are zero.

There are several different methods to initialize your 2D array, with the same result:

    int tab[4][4] = {{1,2,3,4},{2},{3},{4}};
    int bat[4][4] = {1,2,3,4,2,0,0,0,3,0,0,0,4,0,0,0};

The thing to keep in mind is that this kind of array is automatically allocated on the stack by the compiler and is stored as a linear sequential array in memory (see the second method above).

Also, for C++, the array you create in a function by automatic allocation in this manner has a scope limited to that function; after the function call ends, the array is gone. If you want to create an array in a function and pass it back to the main function for use, it has to be dynamically (heap) allocated; you then pass a pointer to that array back to main, and then you have to manage that memory by hand (assuming you didn't use a smart pointer to make it).

Note: You can also initiate your array as follows, for very large arrays:

   myarray[100][100] = {};    //sets all elements to 0
   for (int i = 0; i < 100; i++) {
       myarray[0][i] = i + 1;
       myarray[i][0] = i + 1;
   }

Never do this:

    myarray[100][100];

As this creates an array full of junk values loaded with whatever random bits happen to have been previously stored in that memory location, and you might forget to overwrite them later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM