Answer the question
In order to leave comments, you need to log in
How to fill a matrix (two-dimensional array)?
Task: fill the matrix (two-dimensional array) with the values of the vector b1, b2,...,b81 from the center in a spiral (left-down-right-up).
Note: the figure shows the index values!
I wrote a code that just fills the matrix in the normal way:
#include <iostream>
using namespace std;
int main()
{
const int rows = 9;
const int cols = 9;
int ARR[rows][cols];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
ARR[i][j] = rand() % 10;
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << ARR[i][j] << " ";
}
cout << endl;
}
}
Answer the question
In order to leave comments, you need to log in
Find the index - the center of the matrix. Shift the index to the left by one. Then down one. Then right one. Then up two. Then left two. Etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question