i need a function that initializes a 10 by 10 array

closed account (2Lyv0pDG)
i need a function that initializes a 10 by 10 array and also prints a board with 10 rows 10 columns. please somebody help me
Ok, here's an example of how to initialize a 1-D array:

1
2
3
int array[ 10 ];
for( int i = 0; i < 10; ++i )
   array[ i ] = 5; /* or whatever number you want */


You figure out how to initialize a 2D array. It is straightforward.
closed account (2Lyv0pDG)
will it be
int array [10] [10]
for( int i = 0; i < 10; i ++)
array[i] = 5;
No...
Hint: You are not actually initializing the array correctly (it is 2D not 1D now)
closed account (2Lyv0pDG)
oh it will be
int array [10][10]
for(int i =0; i<10;i++)
array[i][i]=5;
and how will i print the board someone please help
Still no.

What elements of the 2-D array will be initialized by your loop?

array[0][0], array[1][1], ... array[9][9]

What about all the other elements? array[0][1], array[0][2], ... array[0][9] ... etc.?

You will print the board using the same general traversal algorithm that you use to initialize it.
closed account (2Lyv0pDG)
i dont know what you mean concerning the printing the board
and someone please show me how to initialize a 2 dimensional [10][10] array
If one for() loop initializes a one-dimensional array, then how many for loops are needed to initialize a two-dimensional array?

I get the impression that you have no idea how even the above code works because I all but spelled it out for you.

Once you get the initialization working, printing will be easy, assuming you understand how the initialization works.

closed account (2Lyv0pDG)
you have a slick mouth jsmith dont respond to any of my posts again its obvious you think you know alot
I have 20+ years of programming experience. I think you just guaranteed you won't get any response from anyone though.
Indeed...I suggest you STFU and GTFO tallsoldia
lol, you should go "hire a coder" tallsoldia...
Lol, you just learned one of your first important programming lessons...

But before you run off and create a new account or start over on a new forum... here's a different perspective.

I'm doing a 3x3 instead of 10x10 to type less, because I'm lazy too.

You have a board:


0 1 2 3
\-------
1|a b c
2|d e f
3|g h i


To run through every possibility in that board you would need to access the following points:

(1, 1), (1, 2), (1, 3),
(2, 1), (2, 2), (2, 3),
(3, 1), (3, 2), (3, 3)

If you only needed to run through a 1D array, you would use a single for loop. For example, to print out "1 2 3 " you would use:
1
2
3
for (unsigned i = 1 ; i <= 3 ; ++i) {
  cout << i << ' ';
}


If you wanted to go down a single row or a single column, you could do this:
1
2
3
for (unsigned i = 1 ; i <= 3 ; ++i) {
  cout << array[0][i] << ' ';
}


If you wanted to print all of it you can do:
1
2
3
4
5
for (unsigned i = 1 ; i <= 3 ; ++i) {
  cout << array[0][i] << ' ';
  cout << array[1][i] << ' ';
  cout << array[2][i] << ' ';
}


But of course if you need to do more than 3 it gets annoying to type all of that up, even with copy and paste, plus you'll probably get an F. So you need to figure out a way to automatically access the different columns. Make sense?

See the pattern there? Again you have a similar problem as with the 1D array. You need to figure out how to access array[0] then array[1] then array[2], it just happens that it has a [i] stuck to the end of it.

So, try again and good luck. The reason array[i][i] did not work is because you're only iterating over (0,0), (1,1), (2,2). See? You're still missing a lot of the square.

Good luck. And try not to piss people off. Keep in mind we're just trying to help but we get frustrated if you don't do some of the work yourself. I recommend swallowing your pride and apologizing. These guys on here are very knowledgeable. We do it for fun after all, and we don't want to do work for other people. It's like if your friend asked you to help him move to a new house and he stood back and expected you to do his work for him. You'd leave.
Last edited on
guy if u use array[i][i] , this will print only diagonal elements in 2D array,
use the following code :
[code]
for(i = 0; i < 10; i++) {
for(j = 0; j < 10; j++) {
array[i][j] = value;
}
}


--
Patel
Last edited on
1
2
3
4
5
6
7
// initialize all elements to zero
int array1[3][3] = {};

// initialize each element to a hard-coded predefined value
int array2[3][3] = { { 1, 2, 3 },
                     { 4, 5, 6 },
                     { 7, 8, 9 } };
Topic archived. No new replies allowed.