I would like to know how to print an int array of an unknown size in C (not C++). I don't know the number of user inputs, but I know that it is at most 1000. The user inputs a bunch of integers. I need to print out the integers that have an even position. My plan is to store the integers that have an even position in an int array and print it. But I'm stuck as I have no idea about the size.
-
4How is user inputting the numbers? count while inserting into array.kiran Biradar– kiran Biradar2021-08-18 06:16:58 +00:00Commented Aug 18, 2021 at 6:16
-
2If you only have a int* you can't extract any length. You will have to store the length somewhere else.NoNae– NoNae2021-08-18 06:20:29 +00:00Commented Aug 18, 2021 at 6:20
-
2You either need to know the number of elements or have a sentinel value marking the end (like the trailing 0 byte in a string).Shawn– Shawn2021-08-18 06:30:58 +00:00Commented Aug 18, 2021 at 6:30
-
Does the instructor require you to use an array for this, because the problem doesn't really need one. You could just print out every other integer on the fly after each one has been input.Ian Abbott– Ian Abbott2021-08-18 09:02:29 +00:00Commented Aug 18, 2021 at 9:02
-
In real life: how do you fit an object of unknown size in your car? How many papers do you put in the printer before printing an unknown document? You try to find out the size in advance.Lundin– Lundin2021-08-18 09:38:47 +00:00Commented Aug 18, 2021 at 9:38
1 Answer
For statically allocated arrays, you can divide the total size (in bytes) by the size of one element (in bytes):
int array[512];
size_t arraySize = sizeof(array) / sizeof(*array) // = 512
For dynamically allocated arrays, you'll have to manually keep track of the size. That means, storing the size when the array is created and updating that value if the array is reallocated.
From the question and comments, I gathered that you would like to read the integers from stdin (until EOF, I presume). There are several way of doing this:
1. Allocate an array of the maximum size and print every other element
int array[1000];
size_t size = 0;
while (scanf("%d", &array[size]) != EOF)
++size;
for (size_t idx = 0; idx < size; idx += 2)
printf("%d\n", array[idx]);
2. Allocate half of the maximum size and store every other element
Since not all elements are used in the output, we may as well store only the ones we need. Use the * in the scanf format specifier to discard the second integer.
int array[500];
size_t size = 0;
while (scanf("%d%*d", &array[size]) != EOF)
++size;
for (size_t idx = 0; idx < size; ++idx)
printf("%d\n", array[idx]);
3. Allocate the minimum amount of memory
Every time we need to store another integer, we increase the size of the array by 1. This is space efficient, but not very fast.
int *array = NULL;
size_t size = 0;
int n;
while (scanf("%d%*d", &n) != EOF)
{
array = realloc(array, ++size * sizeof *array);
array[size - 1] = n;
}
for (size_t idx = 0; idx < size; ++idx)
printf("%d\n", array[idx]);
free(array);
4. Not storing an array at all
Noticing that the output doesn't depend on having read the entire array, we may as well print the first of every two integers. This won't look pretty in the terminal if the program is interactive (i.e. the user types the integers while the program is running), but will work fine if it receives its input from stdin.
int n;
while (scanf("%d%*d", &n) != EOF)
printf("%d\n", n);
For all code, the usual warnings about using scanf and checking the return value of realloc apply.