Help to create code in c ++

0

Can you help me to make this code in c ++ please?:

Create a two-dimensional array or array that stores consecutive multiples of 7 (7, 14, 21, 28, ...). The number of rows and columns will be entered by keyboard. The sum of the numbers located in the four corners of the two-dimensional arrangement should be shown.

#include <iostream>
using namespace std; 

int main(){
int numeros[100][100],filas,columnas, contador=1;


cout<<"Ingrese el numero de filas: ";
cin>>filas;
cout<<"ingrese el numero de columnas: ";
cin>>columnas;
for(int i=0; i< filas; i++){
    for (int j=0;j<columnas;j++){

        numeros[i][j] = contador*7;
        contador++;
    }
} 

for(int i=0;i<filas;i++) {
for(int j=0;j<columnas;j++) {
  cout<<numeros[i][j]<<" ";
}
cout<<"\n";
}
return 0;
}

I managed to create the matrix with multiples of 7 but I can not do the sum in the corners.

Thanks for the help.

    
asked by Frank Molina 13.07.2017 в 03:01
source

2 answers

1
  

but I can not do the sum in the corners.

If the matrix has dimensions [filas]x[columnas] then the corners will be located in the coordinates:

  • (0,0)
  • (0, columns-1)
  • (rows-1,0)
  • (rows-1, columns-1)

The summation then is trivial:

int total = numeros[0][0]
          + numeros[0][columnas-1]
          + numeros[filas-1][0]
          + numeros[filas-1][columnas-1];
    
answered by 13.07.2017 в 13:26
0

Although the effersion response is correct when calculating the sum, I see another problem: the creation of the matrix is done before knowing the sizes, and is always 100x100.

So if you want 200 rows, it will give you a segment violation when you are filling the matrix in row 101 and the same with the columns, for that I would create the matrix once you know the rows and columns:

#include <iostream>
using namespace std; 

int main(){
    int filas, columnas, contador=1;

    cout<<"Ingrese el numero de filas: ";
    cin>>filas;

    cout<<"ingrese el numero de columnas: ";
    cin>>columnas;

    int numeros[filas][columnas];

    for(int i=0; i< filas; i++){
        for (int j=0;j<columnas;j++){
            numeros[i][j] = contador*7;
            contador++;
        }
    } 

    for(int i=0;i<filas;i++) {
        for(int j=0;j<columnas;j++) {
            cout<<numeros[i][j]<<" ";
        }
        cout<<"\n";
    }
    // Respuesta de eferion
    int total = numeros[0][0]
      + numeros[0][columnas-1]
      + numeros[filas-1][0]
      + numeros[filas-1][columnas-1];

    return 0;
}
    
answered by 13.07.2017 в 16:40