Question
In C++ Make a program that takes a positive integer 'n' and write an n x n matrix whose entries are listed as snail
Example: (See picture) for n=6
Ej. Para n-6 2 2 2 20 21 22 23 24 7 19 32 33 34 258 18 31 36 35 26 9 17 30 29 28 27 10 16 15 14 13 12 11
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;
int main()
{
int n;
int i, j = 0, k = 1;
cout<<"\n Enter a positive integer: ";
cin>>n;
int size = n, matrix[n][n];
while(k <= n * n)
{
for(i = j; i < size; i++)
{
matrix[j][i] = k++;
}
for(i = j + 1; i < size; i++)
{
matrix[i][size - 1] = k++;
}
for(i = size - 2; i >= j; i--)
{
matrix[size - 1][i] = k++;
}
for(i = size - 2; i > j; i--)
{
matrix[i][j] = k++;
}
j++, size = size - 1;
}
if(!n % 2)
{
matrix[(n + 1) / 2] [(n + 1) / 2] = n * n;
}
for(i = 0; i < n; i++)
{
for(int l = 0; l < n; l++)
{
cout<<matrix[i][l] <<"\t";
}
cout<<"\n";
}
return 0;
}

OUTPUT

Enter a positive integer: 6
1 2 3 4 5 6
20 21 22 23 24 7
19 32 33 34 25 8
18 31 36 35 26 9
17 30 29 28 27 10
16 15 14 13 12 11

Add a comment
Know the answer?
Add Answer to:
In C++ Make a program that takes a positive integer 'n' and write an n x...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT