3 x 3 matrix

Hi everyone,

below is my code, I am just trying to make a 2d 3 x 3 array. This code works so long as you are happy with the user inputting the 9 numbers necessary but I actually want to specify a particular set of nine numbers within the code.

 ``123456789101112131415161718192021`` ``````#include #include int main(){ int matrix[3][3]; for(int i=0; i<3; i++) { for(int j=0; j<3; j++){ std::cin>>matrix[i][j]; } } std::cout<

The set of 9 numbers is {5,7,3,1,8,4,2,7,6}. So could you please tell me what the line of code I would need to write in place of the std::cin line in order that the numbers are not input by the user.

Would greatly appreciate any help.
Last edited on
One option would be to explicitly assign each number to the appropriate array element.

Or assign them all at once.
I've only ever seen it done using for loops, do you mean do something like

 ``1234567891011121314151617`` ``````#include #include int main(){ int matrix[3][3]; matrix[0][0]=5; matrix[0][1]=7; matrix[0][2]=3 //etc. etc. std::cout<

How would I do that all at once?
 ``123456789101112131415161718`` ``````#include int main() { int matrix[3][3] = { {5,7,3}, {1,8,4}, {2,7,6} }; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { std::cout << matrix[i][j] << " "; } std::cout << std::endl; } return 0; }``````
Last edited on
Thanks for the help, this works nicely.
Topic archived. No new replies allowed.