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?
#include <iostream>
usingnamespace 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.
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.
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.
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.
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:
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.
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.
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";