First time trying to make 10x10 Grid

I'm trying to make a 10x10 grid of 0's. I haven't gotten very far and already I've encountered a problem. In the second for loop, when I try to enter arrayy[0], intending the value of 0, all I get is garbage value. However, when I enter a value of 1, it outputs a column of 0's as I intended. Why is that?

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>

using namespace std;

int main()
{
    cout << "Grid\n" << endl;

    int arrayx [10];
    int arrayy [10];

    for (int i = 0; i < 10; ++i)
    {
        cout << arrayx[0];
    }

    cout << endl;

    for (int j = 0; j < 10; ++j)
    {
        cout << arrayy[0] << '\n';
    }

    return 0;
}


Ultimately, I'm trying to make a grid that can have objects move around on it, but first I need to be able to just make a simple 10x10 grid. Any tips to push me in the right direction for that are welcome, as I want to try and do this myself first before being given a working example.

Thank you.
Where are you assigning any values to the arrays? Also:
1
2
3
4
5
6
7
8
for (int i = 0; i < 10; ++i)
    {
        cout << arrayx[0]; // Print the first element 10x
    }
for (int i = 0; i < 10; ++i)
    {
        cout << arrayx[i]; //  Print elements 0 - 9 
    }


If you want a grid it is easier to use a 2D array.
I'm trying to give arrayx[10] and arrayy[10] ten elements of value 0 by printing cout << arrayx[0]; and cout << arrayy[0]; within the for loops.

If that's not the right way, I'd appreciate it if you could show me how to assign the values properly.

2D arrays are like this: arrayxy[10][10], right?

Edit:
Okay, this is my attempt at a 2D array:

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

using namespace std;

int main()
{
    cout << "Grid\n" << endl;

    int arrayxy [10][10];

    for (int i = 0; i < 10; ++i)
    {
        cout << arrayxy[0][0];
    }

    cout << endl;

    return 0;
}


I'm obviously doing something very wrong. I'm still getting garbage values.
Last edited on
http://www.cplusplus.com/doc/tutorial/arrays/
cout prints to the screen, you have to assign values to the elements or they will just hold garbage. When you declare the array
int arrayx[10];
the number inside the brackets is how many elements are in the array. you assign values with an assignment operator.
1
2
3
4
5
6
int arrayx[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// or
int arrayxy[10];
arrayxy[0] = 0;
arrayxy[1] = 0;
...

For a 2D array I find it easier to use nested for loops to initialize the values.
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>

using namespace std;

int main()
{
    cout << "Grid\n" << endl;

    int arrayxy [10][10];

    // Fill array with 0 values 
    for (int i = 0; i < 10; ++i)
    {
        for(int j = 0; j < 10; ++j)
		{
			arrayxy[i][j] = 0;
		}
    }

    // Print array as grid
    for (int i = 0; i < 10; ++i)
    {
        for(int j = 0; j < 10; ++j)
		{
			cout << arrayxy[i][j];
		}
		cout << '\n';
    }

    cout << endl;

    return 0;
}
Thanks admkrk.

It also seems to work like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
    cout << "Grid\n" << endl;

    int arrayxy [10][10];

    for (int i = 0; i < 10; ++i)
    {
        for(int j = 0; j < 10; ++j)
	{
		arrayxy[i][j] = 0;
		cout << arrayxy[i][j];
	}
	cout << '\n';
    }

    cout << endl;

    return 0;
}

Is there any difference, or is it equivalent?

Also, is it just me, or are the x and y coords backwards? As far as I knew, x was the horizontal plain and y was the vertical plain. With arrays, that seems to be the opposite. Is that the case with all of C++, or is it just limited to arrays?
Yes, that fills the array and prints it at the same time.

x and y coordinates do not have anything to do with it. Read the tutorial I linked and you will see that it is based on rows and columns. It might be easier to visualize this way.
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
#include <iostream>

using namespace std;

int main()
{
    const int ROWS = 10;
    const int COLUMNS = 10;
    cout << "Grid\n" << endl;

    int arrayxy [ROWS][COLUMNS];

    for (int i = 0; i < ROWS; ++i)
    {
        for(int j = 0; j < COLUMNS; ++j)
	{
		arrayxy[i][j] = 0;
		cout << arrayxy[i][j];
	}
	cout << '\n';
    }

    cout << endl;

    return 0;
}
Yes, rows and columns make more sense than x and y.

I was looking up how to initialize multidimensional arrays with values, and I seem to be doing it correctly, yet I am getting an error that I can't seem to solve. I try to initialize it like this:
 
int arrayrc[1][1] = {{1}, {1}};

What am I doing wrong?
That is not a 2D array, it only contains one element. To continue with rows and columns, you have one row with one column so that is just a single element.
int arrayrc [2][2] = {{1, 1}, {1, 1}};
on the other hand is valid because there are two rows and 2 columns. With one row and/or one column it only has one dimension.
In your opinion, is it best to leave the multidimensional array value assignment to the for loop? It seems to me to be the simplest way of making a grid. Initialization assignment seems too tricky to be worth doing for multidimensional arrays.
If you are filling the array with all the same values or with random numbers nested for loops are easier and shorter to write. If you need to initialize the elements to specific values there really is no way to do it with loops.
1
2
3
4
int array[4][4] = {{3, 5, 1, 9},
                   {4, 2, 7, 1},
                   {8, 2, 5, 8},
                   {1, 4, 2, 1}};

Could not be done any other way except by reading from a file. As you can imagine filling your 10 by 10 array would be pretty tedious to write out. The same can be said for 1D arrays also except you only need the one for loop.
Wow, you're a really patient person, I'd like to say thank you for your time so far. You've been very helpful!

I know I'm asking a lot of questions, but for some reason arrays confuse me. I just don't have a good grasp of how to handle them.

1
2
3
4
int array[4][4] = {{3, 5, 1, 9},
                   {4, 2, 7, 1},
                   {8, 2, 5, 8},
                   {1, 4, 2, 1}};


If you don't mind, I'd like to know how you would print this. Do you use another for loop, or just a regular cout statement?
I had a hard time with arrays at first until one day they just stopped confusing me. Printing is just like I first showed.
1
2
3
4
5
6
7
8
9
 // Print 2D array as grid
    for (int i = 0; i < ROWS; ++i)
    {
        for(int j = 0; j < COLUMNS; ++j)
		{
			cout << array[i][j];
		}
		cout << '\n'; // start a new row
    }

Using the constant variables eliminates the "magic number" and makes it easier to remember which one is which plus you only need to change one variable to change the size of the array.

You can change any element by assigning it a new value
array[2][3] = 4;
or print it with a single statement
cout << array[2][3];

To count how many 5s are in that 4 by 4 array
1
2
3
4
5
6
7
8
9
10
    int count = 0;
    for (int i = 0; i < 4; ++i)
    {
        for(int j = 0; j < 4; ++j)
		{
			if(array[i][j] == 5)
                            count++;
		}
    }
    cout << "There are " << count << " 5s in the array";
Topic archived. No new replies allowed.