Code
#include <stdio.h>
#include <stdlib.h>
void introducirDatos(int cantidad,int array[]);
void visualizarDatos(int cantidad,int array[]);
int main()
{
/* Cantidad */
int cantidad;
/* Introducimos la cantidad */
printf("Introduce la cantidad de valores para el array: ");
scanf("%d",&cantidad);
/* Arreglo */
int array[cantidad];
introducirDatos(cantidad,array);
visualizarDatos(cantidad,array);
return 0;
}
void introducirDatos(int cantidad,int array[])
{
for(int i=0;i < cantidad;i++)
{
printf("Introduzca valor: ");
scanf("%d",&array[i]);
}
return;
}
void visualizarDatos(int cantidad,int array[])
{
for(int i=0;i<cantidad;i++)
{
printf(" %d",array[i]);
}
}
Explanation
I will explain the change in parts:
First
The main () function has stayed like this:
int main()
{
/* Cantidad */
int cantidad;
/* Introducimos la cantidad */
printf("Introduce la cantidad de valores para el array: ");
scanf("%d",&cantidad);
/* Arreglo */
int array[cantidad];
introducirDatos(cantidad,array);
visualizarDatos(cantidad,array);
return 0;
}
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
As we can pass an arrangement without using the steps by reference, we have left the function enter Data, like this:
void introducirDatos(int cantidad,int array[])
{
for(int i=0;i < cantidad;i++)
{
printf("Introduzca valor: ");
scanf("%d",&array[i]);
}
return;
}
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 arrangement as a parameter, it has access to modify it, since a parameter step is made as reference, it could be said.
Third
In a clearer explanation of the algorithm that follows the code, we could say that it is the following:
We ask for the cantidad
of values, later, we initialize the array with said cantidad
, so we pass the array as a parameter to the functions introducirDatos
, so that the array is filled and then to the function visualizarDatos
to print the contents of the arrangement.