[Solved] For loop not working properly to get inputs in an array in c [closed]


You want to create an array with a size of zero which is not allowed. You also can´t declare an array with a variable size so you have to use malloc or something else.

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("HOW MANY NUMBERS DO YOU WANT TO INPUT\n");

    int array_size;
    scanf("%d", &array_size);

    int* Array = (int *) malloc(array_size * sizeof(int));

    for(int index = 0; index < array_size; index++)
    {
        printf("inputs left: %d\n", array_size - index);
        scanf("%d", (Array + index));
    }

    for(int i = 0; i < array_size; i++)
    {
        printf("%d\n\r", *(Array + i));
    }


    free(Array);
    return 0;
}

Which gives

HOW MANY NUMBERS DO YOU WANT TO INPUT                                                                                                                                              
3                                                                                                                                                                                  
inputs left: 3                                                                                                                                                                     
1                                                                                                                                                                                  
inputs left: 2 
2                                                                                                                                                                                  
inputs left: 1                                                                                                                                                                     
3                                                                                                                                                                                  
1                                                                                                                                                                                  
2                                                                                                                                                                                  
3  

Or you use something like an std::vector.

#include <stdio.h>
#include <vector>

int main()
{
    printf("HOW MANY NUMBERS DO YOU WANT TO INPUT\n");

    int array_size;
    scanf("%d", &array_size);

    std::vector<int> Array;

    for(int index = 0; index < array_size; index++)
    {
        int Temp;
        printf("inputs left: %d\n", array_size - index);
        scanf("%d", &Temp);
        Array.push_back(Temp);
    }

    for(int i = 0; i < array_size; i++)
    {
        printf("%d\n\r", Array.at(i));
    }

    return 0;
}

Which result in the same output.

4

solved For loop not working properly to get inputs in an array in c [closed]