multidimensional arrays

im trying to fill up a [10]x[10] array using for loops and to output rows and columns that count from 1 to 100.
this is what it should look like:

1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
....

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.

thanks
Okay so. You have an array thats 10x10x

int arr[10][10];

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
Last edited on
#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];
}
}

system("pause");
return 0;

so this is what i have come up with so far
marcusg wrote:
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.

Good job so far, you almost have it. Please edit your post and use code tags, otherwise its unreadable code - http://www.cplusplus.com/articles/jEywvCM9/

I added comment :) Here is the finish -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
	}
Great!! that helped me a lot thanks for taking the time i appreciate it.

how would I print the indexes for each row and column?

how would I print the indexes for each row and column?


Could you elaborate?
0 1 2 3 4 5 6 7 8 9

-- -- -- -- -- -- -- -- -- --

0:

1:

2:

3:

4:

5:

6:

7:

8:

9:
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] << " ";
Topic archived. No new replies allowed.