Reputation:
int main()
{
srand(time(NULL));
int ourArray [3][5] = {0};
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++)
{
ourArray [i][j] = rand() % 30;
}
}
return 0;
}
This is what I've got so far, but I'm not quite sure where to go from here. What I (think) is going on here is it goes to first position of the array at 0,0 and generates a number. Then it goes to 1,1 and 2,2 and so on. I'm not sure where to go from there, and I'm sure there's a more efficient way of doing this that populates an entire row or column at a time.
Additionally, my compiler won't allow me to use cout or endl like I normally can? It's insisting I use std::cout and I was just wondering why.
Upvotes: 0
Views: 86
Reputation: 206717
What I (think) is going on here is it goes to first position of the array at 0,0 and generates a number. Then it goes to 1,1 and 2,2 and so on.
The array positions are filled in this order:
i = 0, j = 0,4
0, 0
0, 1
0, 2
0, 3
0, 4
i = 1, j = 0,4
1, 0
1, 1
1, 2
1, 3
1, 4
i = 2, j = 0,4
2, 0
2, 1
2, 2
2, 3
2, 4
There isn't any more efficient way to fill the entire array with random numbers.
In order to use cout
and endl
, you have to include iostream
in your file:
#include <iostream>
Then, you can either use them with std::
prefix, std::cout
and std::endl
. If you don't wish to use the std::
prefix, you have to indicate that you are using the std
namespace by:
using namespace std;
Upvotes: 1