I have made a code in which I indicate the number of values that my array will have, it enters them correctly but then nothing is displayed. I don't know where the error is, I think I'm returning something wrong.
Code:
//Prototipos de funciones
int introducirDatos(int cantidad,int array[]);
void visualizarDatos(int cantidad,int array[]);
int main()
{
int cantidad,array[cantidad];
introducirDatos(cantidad,array);
visualizarDatos(cantidad,array);
return 0;
}
int introducirDatos(int cantidad,int array[])
{
printf("Introduce la cantidad de valores para el array: ");
scanf("%d",&cantidad);
for(int i=0;i<cantidad;i++)
{
printf("Introduzca valor: ");
scanf("%d",&array[i]);
}
return array[cantidad];
}
void visualizarDatos(int cantidad,int array[])
{
for(int i=0;i<cantidad;i++)
{
printf(" %d",array[i]);
}
}
Code
Explanation
I will explain the change in parts:
First
The main() function has been left like this:
That is, in the main function the initialization of the array will be done, and from there it will be passed as a parameter to the other functions.
Second
Since we can pass an array without needing to use steps by reference, we have left the insertData function, like this:
That is, the insertion of the data in the array will be the only thing that is done in the procedure. Since it is a procedure, it is not necessary to return anything, since as we have passed the array as a parameter, it has access to modify it, since the parameter is passed as a reference, it could be said.
Third
In a clearer explanation of the algorithm that the code follows, we could say that it is the following: