Will they be able to tell me if the way I am creating two functions is correct, one is called calculatedrange and the other is deleteblock , THEY ARE THE LAST TWO, it happens that in my project that I took from a book, an array is filled with numbers of 1 to 2 then a pass from 0 is made by blocks when the user types his row and column if there are equal values to the left or right they are passed to 0 and moved to the beginning of the row but they are not touched when there is only at least 1 value must be blocks greater than 2 identical values per row, but creating my two functions, one that calculates the range of the position and another function that, so to speak, eliminates (it does not eliminate, it only passes to 0), I do not know why but it gives me the impression that although it compiles it is not good haha, I don't know how to explain it, I know that a variable has its domain inside the brackets of a function outside of it, you have to recreate it because it gets lost, I understand that, but is the way I'm creating these two functions correct? Is it necessary to repeat both variables for it to work? Watching videos and documents I have never seen people do this and I don't know but that's why I think maybe I'm wrong without even realizing why?
I must create 3 functions, one that calculates the position, one that eliminates the block (that is, it passes that set of equal values to 0), one to transfer the values to 0 at the beginning of the row.
and since these functions are so dependent on each other, I don't know if it can be done in another way, because it cost me to divide these processes into 2, now into 3 since the deleteblock function I must divide it into 2 functions, one that passes the block to 0 and another that moves me to the beginning but I could not. So I wanted to ask you about this, do you think my code is ok? I still haven't created the function to transfer the values passed to 0 to the beginning for the same reason, I do. I would almost have to repeat all the variables again because otherwise the code would not work well. So my question is is there a smaller way to do these 3 functions without repeating the code so much? or is it always done like this? even to make the calculatedrange function I had to create two more variables in the function prototype just so that I didn't get an error that said insufficient variables for the function to work so I would like to hear your advice
I attach my code for you to see
#include <iostream>
using namespace std;
//prototipos de las funciones y sus variables para trabajar en ellas
//se necesitan las matrices, y dos variables de tipo int para las filas y columnas
void mostrarMatriz(int[][9],int,int);
int numeroaleatorio();
void llenarMatriz(int[][9],int,int);
void crearMatrizcopia(int[][9],int[][9],int,int);
void imprimirMatrizcopia(int[][9],int,int);
void rangocalculado(int[][9],int,int);
void eliminarbloque(int[][9],int,int);
int main(){
int Matriz[9][9];//matriz original
int Matrizcopia[9][9];//copia de la matriz original
llenarMatriz(Matriz,9,9);
mostrarMatriz(Matriz,9,9);
crearMatrizcopia(Matrizcopia,Matriz,9,9);
imprimirMatrizcopia(Matrizcopia,9,9);
rangocalculado(Matrizcopia,9,9);
eliminarbloque(Matrizcopia,9,9);
imprimirMatrizcopia(Matrizcopia,9,9);
return 0;
}
int numeroaleatorio(){
int aleatorio;
aleatorio = 1 + rand() % 2;
return aleatorio;
}
void llenarMatriz(int Matriz[9][9],int filas,int columnas){
int aleatorio;
for(int i=0 ; i<filas; i++){
for(int j=0;j<columnas; j++){
aleatorio = numeroaleatorio();
Matriz[i][j] = aleatorio;
}
}
}
void mostrarMatriz(int Matriz[9][9],int filas,int columnas){
cout<<"Matriz Original"<<endl;
cout<<endl;
for(int i=0 ; i<filas; i++){
for(int j=0;j<columnas; j++){
cout<<Matriz[i][j]<<" ";
}
cout<<endl;
}
}
void crearMatrizcopia(int Matrizcopia[9][9],int Matriz[9][9],int filas, int columnas){
for(int i=0 ; i<filas; i++){
for(int j=0;j<columnas; j++){
Matrizcopia[i][j] = Matriz[i][j];//pasar datos de la matriz original a la copia
}
}
}
void imprimirMatrizcopia(int Matrizcopia[9][9],int filas, int columnas){
cout<<endl;
cout<<"Matriz Copia"<<endl;
cout<<endl;
for(int i=0 ; i<filas; i++){
for(int j=0;j<columnas; j++){
cout<<Matrizcopia[i][j]<<" ";
}
cout<<endl;
}
}
void rangocalculado(int Matrizcopia[9][9],int filas,int columnas ){
int fc;
cout << "Insertar la fila-columna a eliminar :";
cin >> fc;
int* fila = Matrizcopia[fc/10];
int posicion = fc % 10;
int numero = fila[posicion];
}
void eliminarbloque(int Matrizcopia[9][9],int inicio,int fin){
int fc;
int* fila = Matrizcopia[fc/10];
int posicion = fc % 10;
int numero = fila[posicion];
inicio = posicion;
for ( int i=inicio-1; i>=0 && fila[i]==numero; i--, inicio--);
fin = posicion;
for ( int i=fin+1; i<9 && fila[i]==numero; i++, fin++);
if(fin>inicio)
{
if (inicio > 0)
{
for (int i=inicio-1, j=fin; i>=0; i--, j--)
{
fila[j] = fila[i];
fila[i] = 0;
}
}
else
{
for (int i=inicio; i<=fin; i++ )
fila[i] = 0;
}
}
}
If it were C I would say that it is relatively good. In C it is common to have to pass multiple parameters to functions. An example is some functions that work with text strings, where the function receives, among other things, the pointer to the start of the string and its length.
In C when there are many parameters, what is commonly done is to "put" them all inside a
struct
and pass only the struct as an argument to the function. A practical example of this pattern is in the creation of threads withpthread_create
, where the function to be executed within the thread receives a single argument, so if you need to pass multiple values to the function, you "put" them all inside astruct
and pass thestruct
.On the other hand, since it's not C but C++, those patterns are "deprecated" thanks to object-oriented programming. With this, your code would look neater inside a class and functions (now members of the class) are not required to have a "long" list of parameters, as seen below.
That's for the number of arguments question.
Regarding the question if the functions are ok. No, they are not, there are several errors.
rangocalculado
returns nothing andeliminarbloque
produces aSIGSEV
becausefc
it has no reasonable value.Note : the variable
fc
inside israngocalculado
NOT the same asfc
insideeliminarbloque
, both of them, being declared as local inside two different functions, belong to two different stackframes .Anyway, those are my comments regarding your code, I hope I have helped you and I hope that you have been more interested in getting into this world of programming, it is very interesting.