Variable arrangement in C

3

I have made a code in which I indicate the amount of values that my arrangement will have, it introduces them well but then it does not visualize anything to me. I do not know where the fault is, I think I'm giving back 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]);
    }
}
    
asked by Mario Guiber 28.08.2017 в 20:30
source

1 answer

2

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.

    
answered by 28.08.2017 / 21:24
source