2

I have an integer array like int a[50];. Now I want to store values entered by the user in the integer array as integers.
The problem is that I don't know the number of elements the user is going to input and hence I am unable to traverse the entire array.

So is there any method for the user to input the values dynamically and store it in the integer array and display it.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
Mistique
  • 47
  • 2
  • 6

4 Answers4

0

You can use realloc and make a specific input as end of the input

int readinput(int *value)
{
    int status;
    //function returns 0 on conversion -1 if exit keywoard entered
    return status;
}


int value, *table = NULL,size = 0;
while (!readinput(&value))
{
    table = realloc(table, (size++ + 1) * sizeof(int));
    if (table == NULL)
    {
        break;
    }
    table[size] = value;
}

example converting function you can find here: How to get Integer and float input without `scanf()` in c? in my answer

0___________
  • 60,014
  • 4
  • 34
  • 74
0

For this take a input from user for number of element to have in an array. Then malloc that memory and store the inputs in the array.

user7345878
  • 492
  • 2
  • 7
  • 20
0

The following solution uses the scanf() function with %d as format specifier. The while loop checks the return value so that it can detect if the conversion was successful. If anything other than a valid number is inputted the loop will break. A valid number is also beginning with space but not with any other characters.

The memory is allocated with malloc() and will be reallocated with realloc() each time the user entered a number. Note that there is no error checking about the reallocation this should be done with a temporary pointer like here.

Further this code will reallocate for every single number. You could also reallocate in bigger steps to avoid reallocation on every input. But this only matters if there is much data. In this case the speed improvement wouldn't matter.

After the memory is no longer needed you have to use free().

The user can type:

1<Enter>
2<Enter>
3<Enter>
any characters<Enter>

and will get:

Numbers entered:
1 2 3

as output.

Code:

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

int main (int argc, char *argv[])
{
   size_t idx;
   int number;
   size_t count = 0;
   int* numberArr = malloc(sizeof(*numberArr));

   printf("Enter each number separated by <Enter>,\n"
          "to abort type any other character that isn't a number!\n");
   while (scanf("%d", &number) == 1)
   {
      numberArr = realloc(numberArr, (count + 1) * sizeof(*numberArr));
      numberArr[count] = number;
      count++;
   }

   printf("\nNumbers entered:\n");
   for (idx = 0; idx < count; idx++)
   {
      printf("%d ", numberArr[idx]);
   }
   printf("\n");

   free(numberArr);

   return 0;
}
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • Hi @Mistique if this or any answer has solved your question please consider [accepting it](https://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Andre Kampling Aug 16 '17 at 06:33
0

This code should work well, you can change BUFFER_SIZE to whatever you want, after these steps array will realloc to arr size + BUFFER_SIZE.

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

#define BUFFER_SIZE 50

int main(void)
{
    int * userInputBuffer = malloc(sizeof(int) * BUFFER_SIZE);
    int userInput;
    int counter = 0;
    int reallocCounter = 1;

    while ((scanf(" %d", &userInput)) == 1)
    {
        if ((counter % BUFFER_SIZE) == 0)
        {
            userInputBuffer = realloc(userInputBuffer, (reallocCounter++ + 1) * BUFFER_SIZE * sizeof(int));
        }
        userInputBuffer[counter++] = userInput;
    }

    for (int i = 0; i < counter; i++)
    {
        printf("User input #%d: %d\n", i + 1, userInputBuffer[i]);
    }

    free(userInputBuffer);
    return 0;
}
kocica
  • 6,412
  • 2
  • 14
  • 35
  • 1
    It's tagged as `C` but you use `#include ` which is `C++`. If you test your programs you should use a plain C compiler which will fail on that. – Andre Kampling Aug 11 '17 at 08:56