Im struggling with the for loops to fill it up and then to print it out.
i need a for loop with a nested for loop to fill it and a for loop with a nested for loop to print it out, so a total of four loops needed. Any advice would be greatly appreciated.
Now. As you said you need nested loops. The outer one, should run 10 times and increase the rows every 10 columns, which means the inner should also run 10 times and increase the columns.
For the print out thingy, literally the exact same loop, but just have a cout instead.
Get started a bit on this and write a bit of code and get started, then post your code here if you get stuck on a specific task, and please use code tags for your code - http://www.cplusplus.com/articles/jEywvCM9/
P.S. Use Google/Youtube to your advantage, plenty of threads/videos that will help you out!
Goodluck!
Edit: If anyone that joins this thread decides to give this guy the code he needs, Do not! He has to work for it :D
#include <iostream>
#include <iomanip>
using namespace std;
using std::setw;
int main()
{
int i = 0;
int j = 0;
int myArray[10][10];
for (int i = 1; i < 10; i++)
{
for (int j = 1; j < 10; j++)
{ myArray[i][j] = ; what do i set my array equal to here?
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << myArray[i][j];
}
}
int i = 0;
int j = 0;
int myArray[10][10];
for (int i = 1; i < 10; i++)
{
for (int j = 1; j < 10; j++)
{ myArray[i][j] = ; what do i set my array equal to here?
}
}
Since you have 10 elements per row, multiply your current row(i) by 10.
Now with that value, add that with (j + 1). You would add 1 with j so it would start with 1, and end with 10.
int i = 0; // Remove this, you already created int i inside the loop, thats enough :)
int j = 0; // same as above
int myArray[10][10];
int filler = 0; // This will fill the array
for (int i = 0; i < 10; i++) // Start at 0 not 1.
{
for (int j = 0; j < 10; j++) // Same as above.
{
filler++; // Increase filler each loop. So you fill the array with 1,2,3 etc.
myArray[i][j] = filler;
}
}
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
std::cout << myArray[i][j] << " ";
}
std::cout << std::endl;
}
To get the column indexes, before printing the grid, output the indexes. std::cout << "0 1 2 3 4 5\n\n"; //Make sure this spacing matches the spacing in the array
For rows, add this to line 19 (from code above). std::cout << i << myArray[i][j] << " ";