0

can anyone help me know about in what way should I write a program so that it can give the output of the diagonal of 3×3 array??

11th Sep 2016, 4:41 PM
Paras Arya
Paras Arya - avatar
4 ответов
+ 1
#include <iostream> int main { int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int y = 0; y < 3; y++) { for (int x = 0; x < 3; x++) std::cout << arr[y][x]; std::cout << "\n"; } }
11th Sep 2016, 5:34 PM
Cohen Creber
Cohen Creber - avatar
+ 1
#include <iostream> #include <string> int main() { int x = 0, arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int y = 0; y < 3; y++) { std::cout << arr[y][x] << "\n" << std::string(" ", x + 1); x++; } } Apologies, misread. This code runs fine when I compile it on Windows (Visual Studio and Code::Blocks). I get the output: 1 5 9 If I tried to compile the code via SoloLearn, the output does not follow any particular pattern like the one I set it to do, which is a bit annoying.
11th Sep 2016, 6:23 PM
Cohen Creber
Cohen Creber - avatar
+ 1
#include <iostream> int main() { int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int y = 0, x=0; y < 3,x<3; y++,x++) { std::cout << arr[y][x]; std::cout <<"\n"; } } you can initialise many variables in a for loop I guess there's a limit of 256... I'm not sure but you won't initialise so many in a for loop :)
12th Sep 2016, 10:30 AM
Aryan Pandey
Aryan Pandey - avatar
0
it is printing the whole array #Cohen Creber not only the diagonal!
11th Sep 2016, 5:57 PM
Paras Arya
Paras Arya - avatar