-6

I am working on a coding problem "find the smallest number from inputs", I take inputs in the form of for loop and store them in an array. Its working fine for total inputs less than 11 but for greater than 11 it only takes 10 inputs and then breaks.

printf("HOW MANY NUMBERS DO YOU WANT TO INPUT\n");
int array_size , var1;
scanf("%d",&array_size );
var1 = array_size;
int index = 0 , array[index];
for(int index = 0; index < array_size; index++)
{
    printf("inputs left: %d\n",var1);
    var1 -= 1;
    scanf("%d",&array[index]);
}

I expect it should take as many inputs as user desire but it only takes 10 inputs and I can't seem to find the problem.

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62

2 Answers2

0
int index = 0 , array[index];

You're declaring an array of size 0 here. Array sizes must be positive.

You're not using index as declared here, to remove it and use array_size as the size:

int array[array_size];
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Even that's not enough, though. In C++, `array_size` would have to be a constant, and it would be wiser to use a `std::vector` instead of a C-style array. In C99, letting `array_size` be a non-constant is technically allowed, but it's unwise: https://stackoverflow.com/questions/22530363/whats-the-point-of-vla-anyway – jjramsey Aug 12 '19 at 12:50
0

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.

Kampi
  • 1,798
  • 18
  • 26