C++ Code! Represent the following matrix using a two-dimensional array and write a nested for loop to initialize the elements (do not initialize the array in the declaration):
10 9 8
7 6 5
4 3 2
Here is code:
#include <iostream>
using namespace std;
int main()
{
int ROWS = 3,COLS = 3;
int arr[ROWS][COLS];
int counter = 10; // counter assign the values
// loops rows
for(int i = 0 ; i < ROWS ; i++)
{
// loops cols
for(int j = 0 ; j < COLS ; j++ )
{
arr[i][j] = counter--;
}
}
//prints the matrix
cout << "Matrix array is :" << endl;
for(int i = 0 ; i < ROWS ; i++)
{
for(int j = 0 ; j < COLS ; j++ )
{
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
Output:
Get Answers For Free
Most questions answered within 1 hours.