#include <stdio.h> int main() { int size; int i, j; scanf("%d", &size); for (i = 1; i <= size; i++) { for (j = size * size - (size - 1); j < size * size; j++) { printf("%d ", j); } printf("%dn", j); } return 0; } 

Must output when entering 2

3 4 2 1

at 4

13 14 15 16 9 10 11 12 5 6 7 8 1 2 3 4

I just get all the last lines in a row like this

13 14 15 16 13 14 15 16 13 14 15 16 13 14 15 16

I can not figure out how to get the 2nd, 3rd and final one.

  • and why at 2 the reverse order and at 4 straight lines? - sercxjo
  • Sorry, typo. This is how it should output: I enter 2 displays a square 3 4 2 1 I enter 3 displays a square 7 8 9 4 5 6 1 2 3 I enter 4 displays a square 13 14 15 16 9 10 11 12 5 6 7 8 1 2 3 4 - Tania
  • and for some reason I enter the form in the form of a square (here on the forum, but saved as strings) ((( - Tania
  • correct in your question to keep the formatting in this forum, add spaces to the beginning of the line and there should be an empty line before the block of such lines 3 4 2 1 7 8 9 4 5 6 1 2 3 so? - sercxjo
  • Yes, yes. Corrected in the question. - Tania

2 answers 2

write first in general form (I will designate n = size for short)

 nn-n+1 ... nn ... 2n+1 ... 3n n+1 ... 2n 1 ... n 

This shows that the outer loop by i is needed from n to 1 in the reverse order, and the inner one from i * nn + 1 to i * n in the forward order

  • Thank you very much)) Earned !! And now it became clear)) - Tania
  • nice :) - sercxjo 4:34 pm

By "square in reverse order", as I understood, was meant a matrix with elements arranged in descending order from maximum to 1.

 #include <stdio.h> int main() { int size, max, first; int i, j; scanf("%d", &size); max = size*size; for(i=1; i<=size; i++) { first=max-size+1; for(j=1; j<=size; j++) printf("%d\t", first++); max-=size; printf("\n"); } getch(); return 0; } 
  • - Well, the answer is wrong, and you should not give full answers to study tasks. It is not necessary that a person accomplish the task but that he learns. - sercxjo pm
  • Well, immediately someone would have written that it was wrong. After all, in my message I made a note about how I understood the essence of the question. It turned out not quite right. And the topstarter made clarifications in the comments. Now it turns out true: 7 8 9 4 5 6 1 2 3 - Mikola