I am making a program where I basically request the row, column of a matrix in the same data (a single entry) , ( there will ALWAYS BE TWO, example: 34) . I cannot create the logic for the program to validate the position to be deleted.
Example: when I insert the number 24, row 2 column 4, I can't make that validation correct and then go to the if to recognize the condition.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
int main(void)
{
/** MATRICES
C0 C1 C2
F0 1 2 3
F1 4 5 6
F2 7 8 9
**/
int A[][3]={{1,2,3},{4,5,6},{7,8,9}};
int filas=3,columnas=3;
int B[filas][columnas];
int fc;
for(int i=0;i<filas;i++){
for(int j=0;j<columnas;j++){
cout<<"["<<i<<"]["<<j<<"] :";
cin>>B[i][j];
}
cout<<endl;
}
for(int i=0;i<filas;i++){
for(int j=0;j<columnas;j++){
cout<<B[i][j]<<" ";
}
cout<<endl;
}
cout<<"Insertar la fila-columna a eliminar :";
cin>>fc;
filas=fc/10;
columnas=fc%10;
for(int i=0;i<filas;i++){
for(int j=0;j<columnas;j++){
if(B[i][j]==fc){
B[i][j]=0;
}
}
cout<<endl;
}
for(int i=0;i<filas;i++){
for(int j=0;j<columnas;j++){
cout<<B[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
It seems to me that you have confused with that of the matrix. Let's start from the point where you get the row and column. Before you have to take into account that for the user the rows go from 1 to 10 and the columns also go from 1 to 10. However, in our matrix the rows and columns go from 0 to 9.
NOTE: There is the case of selecting any column of row 10. For this case we will solve it by verifying the result of the division. If it is greater than 100, then we operate on 100. There will be no ambiguity in three-digit numbers since the matrix is square. Ignore the note if your matrix does not have more than nine rows and columns. The algorithm works anyway.
There are a couple of concepts that you should be clear about, the first " validate " and the second " delete ".
As for validating the input, it is already clear to us that it has to be a number between
0
and99
since, as can be seen in your code, the rows will be the tens and the columns will be the units:To validate that your data falls within the range you require, you just have to check it after reading; The most common thing in this type of program is to request the data again if the input does not match what is expected:
Collecting the data like this you can assume that once the loop is over
do
-while
the variablefc
will have the correct data.As for removing positions from an array, that's not possible. But in your code it doesn't seem like you're removing anything, but assigning zero (
0
) to the value that matches the requested row and column:With that in mind, this is enough:
In short, your code could look like this:
You can see the code working in Try it online!:
As I understood your query:
1) You want to fill a maximum dimension matrix 10x10, for your example you create a 3x3 matrix.
2) You want to enter a 2-digit number by keyboard, in which the first digit is the row and the second digit is the column, then show the initial matrix in which the row and column are changed to zero according to the 2-digit number joined.
If those are your requirements I share the code.