Help with matrix in c ++

1

Could you help me modify this code?

I spent the whole morning trying to make it comply with this statement:

  

Create a bidimensional matrix or array containing four (04) columns and the rows that you indicate by keyboard, the first three columns will have consecutive numbers starting from 1 and the 4th column will be the result of multiplying the previous three columns. The arrangement will have the following form:

     

But nothing, this is the only thing I can do:

#include <iostream>
using namespace std;

int main(){

int numeros[100][4], filas, columnas=4;

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

for(int i=0;i<filas;i++){
    for(int j=0;j<columnas;j++){
        cout<<"Digite un numero ["<<i<<"]["<<j<<"]: ";
        cin>>numeros[i][j];
    }
}   
for(int i=0;i<filas;i++){
    for(int j=0;j<columnas;j++){

        cout<<numeros[i][j];
    }
    cout<<"\n";
}

return 0;

Thanks for your help.

    
asked by Frank Molina 13.07.2017 в 01:09
source

3 answers

1

Try something like this:

#include <iostream>
using namespace std;

int main(){

  int numeros[100][4], filas, columnas=4, contador=1, multiplicacion=1;

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

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

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

  return 0;
}

Exit:

Digite el numero de filas: 5
1 2 3 6 
4 5 6 120 
7 8 9 504 
10 11 12 1320 
13 14 15 2730 

I'm using the variable contador that increases by 1 with each iteration in the columns. The variable multiplicacion will contain the result of multiplying the values for each row and is subsequently reset to 1.

    
answered by 13.07.2017 в 01:24
1
  

Create a two-dimensional array or array that contains four (04) columns and the rows that you indicate by keyboard

There they are asking you to use dynamic memory:

int filas;

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

int** matriz = new int*[filas];
for( int i=0; i<filas; i++ )
  matriz[i] = new int[4];

// Resto del codigo
// ...

// Limpieza de la memoria
for( int i=0; i<filas; i++ )
  delete[] matriz[i];
delete[] matriz;
  

The first three columns will have consecutive numbers starting from 1 and the 4th column will be the result of multiplying the previous three columns.

That is, you do not have to ask the user to enter any additional number ... they are all self-calculated:

for( int fila=0, numero=1; fila<filas; fila++ )
{
  int producto = 1;

  // Rellenamos las tres primeras columnas
  for( int columna=0; columna<3; columna++, numero++ )
  {
    matriz[fila][columna] = numero;
    producto *= numero;
  }

  // Y ahora la celda del producto
  matriz[fila][3] = producto ;
}

And well, although it does not indicate it explicitly in the exercise, it is supposed that the table must be shown:

for( int fila=0; fila<filas; fila++ )
{
  for( int columna=0; columna<4; columna++ )
    std::cout << matriz[fila][columna] << ' ';
  std::cout << '\n';
}

Although you can leave it cooler with the columns correctly aligned ... but it requires a preprocess:

#include <iomanip>
#include <cmath>

int max = std::log10(matriz[filas-1][2])+1;

for( int fila=0; fila<filas; fila++ )
{
  for( int columna=0; columna<4; columna++ )
    std::cout << std::setw(max) << matriz[fila][columna] << ' ';
  std::cout << '\n';
}

log10 allows you to calculate the number of digits of a number. example:

log10(100)  = 2
log10(999)  = 2.XXXX
log10(1000) = 3

All you have to do is add one to get the correct result ... that will be the width of all the columns. Why do I consult a cell in question? The column with the most digits will be the fourth, but since it is the last one, it does not matter how wide it is ... I am more interested in columns with the incremental values and of them the most interesting cell is the last one, since it will be the one with the the largest number of digits.

And with this you already have the problem solved.

    
answered by 13.07.2017 в 09:01
1
  

Create a two-dimensional array or array that contains four (04) columns and the rows that you indicate by keyboard .

As I understand in the statement, while the columns are fixed (4) the rows are variable. Since we know the determined size of the columns we will use a container with pre-fixed elements <array> . As for the rows, of indeterminate size, we will use a container with dynamic elements and annexes <vector> , to facilitate its use, with the relevant aliases:

#include <vector>
#include <array>

using columnas_t = std::array<int, 4>;
using tabla_t = std::vector<columnas_t>;

Once the number of rows is known, we can do the data collection operation in the usual double loop structure:

tabla_t::size_type filas{};
std::cout << "Filas: ";
std::cin >> filas;

tabla_t tabla(filas);

for (auto &fila : tabla)
{
    int producto{1};
    for (int indice = 0; indice < 3; ++indice)
    {
        std::cout << "Valor: " << indice;
        std::cin >> fila[indice];
        producto *= fila[indice];
    }
    fila[3] = producto;
}

We take advantage of the data collection to do the multiplication operation, thus saving us repeating the loop to multiply. I have prepared the sample code and you can see it working .

    
answered by 13.07.2017 в 09:09