Program to print an array with the same number of rows and columns in descending order

Asked

Viewed 61 times

-3

I need to know what a program would look like to print an array the same way below, where the bottom line will always subtract 1 from the top number. I really tried to make a code but didn’t know where to start, so I came to ask for a help here.

 6 5 4 3  \\ Caso o usuário digite 4 linhas
 5 4 3 2
 4 3 2 1
 3 2 1 0


 4 3 2   \\ Caso o usuário digite 3 linhas
 3 2 1 
 2 1 0

 2 1     \\ Caso o usuário digite 2 linhas
 1 0
  • Put the code to print the matrix with the zeroed values, so we can help you without doing all the work.

  • But in a few words each value corresponds to subtraction of the current row and column (starting at 1) from twice the total lines.

  • But the matrix is all provided by the user ? Or the user provides only the first value and size ?

  • Whoa, guys. Thank you so much for trying to help!

1 answer

-1


If I understand correctly, the code below solves your problem. Would that be it?

If you need help understanding the code, just let us know.

#include <iostream>
using namespace std;

int main()
{
    int squareMatrixOrder;
    cout << "Enter the square matrix order: \n";
    cin >> squareMatrixOrder;

    int startNumber = (squareMatrixOrder * 2) - 2;

    for( int row = squareMatrixOrder; row > 0 ; row-- ) {
        int currentNumber = startNumber;
        for( int column = 0; column < squareMatrixOrder; column++ ) {
            cout << currentNumber-- << " ";
        }
        startNumber--;
        cout << endl;
    }
}
  • Damn, man! That’s right, show. I just don’t understand why my teacher uses printf, scanf, and the fact that it’s in English, but otherwise the code is really what the exercise asked for.

  • Make an effort to understand the code. Search for the Cout/Cin difference for printf/scanf. And if you get used to English, it’s essential. No more, success!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.