-1

I am trying to create an array from the integers that a user inputs. They must be inputted on the same line, e.g. 5 1 2 3 1 6. The number of integers can be any amount.

However, the first number + 1 determines the size of the array. E.g. if the user inputs the number 5 first, it must be followed by 5 other random integers thus making the length of the array 6. I'm getting confused on how to do this because there is so much user input. Thanks in advance!

Here is my code so far, though i don't think it'll be much help:

#include <stdio.h>

int main(void) {
    int arr_days[13];
    int i = 0;
    printf(" ");
    while(i < 13) {
        scanf("%d", &arr_days[i]);
        i = i + 1;
    }
    printf("%d", arr_days[0]);
    return 0;
}
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
RRr Mmm
  • 11
  • 4
  • sorry i was just editing it – RRr Mmm Apr 30 '16 at 04:44
  • Please ask a specific question. What is it about your code that you need help with? In what ways is it not doing what you want? – kaylum Apr 30 '16 at 04:45
  • The question im asking is how do i base the length of the array on the first number inputted by the user? e.g. how do i make the array length 6 if the user inputs 5 2 5 314 135 123? – RRr Mmm Apr 30 '16 at 04:47
  • Read the first number with `scanf`. Then declare a Variable Length Array (VLA) or dynamically allocate the array with `malloc`. I would recommend the latter. – kaylum Apr 30 '16 at 04:49
  • sorry im fairly new to programming, can you explain how malloc works? – RRr Mmm Apr 30 '16 at 04:50
  • 1
    That is basic C. If you don't know that then it means you have not taken the time to learn C at all on your own. Stackoverflow is not the best place to learn basic C. Please consult any C book or tutorial - there are plenty. – kaylum Apr 30 '16 at 04:53
  • It does not matter the numbers are in one line or seperat lines, `scanf("%d", ...)` inputs integers, not lines. – fluter Apr 30 '16 at 05:10

3 Answers3

0

You weren't too far off, you just need to allocate the buffer dynamically and read the first number separately so that you know how much space and how many loops to perform.

You might also want to check if arr_days is NULL after the malloc to protect against 'out of memory' problems.

#include <stdio.h>

int main(void) {
    int* arr_days;
    int i = 0;
    int n;
    printf(" ");
    // get first number to find out how many more to expect
    scanf("%d", &n);
    // create memory space to store them
    arr_days = malloc(n * sizeof(int));
    while(i < n) {
        scanf("%d", &arr_days[i]);
        i = i + 1;
    }
    printf("%d", arr_days[0]);
    return 0;
}

EDIT: I see from some comments further down that there is some confusion about scanf. This command will trigger an input request if there is no input pending, then it will process any pending input. So when you type in a bunch of numbers in response to a scanf input prompt (or pipe a text file with those numbers in to the program) each invocation of scanf("%d",...) will only extract the next single integer, leaving the rest of the input still available. If you think of it like the string is a file, scanf is reading from that file and leaving the file pointer at the end of the bit it just read ready for the next invocation to read the next bit... (which is exactly what happens if you pipe a text file!)

PeteB
  • 372
  • 1
  • 10
0

First you would need to get the number so that you know how many numbers to read and how big to allocate the array.

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

int *arr = malloc(num * sizeof(int));

Second, read each of the numbers one by one.

int I;
for (I = 0; I < num; I++) {
    scanf("%d", &arr[I]);
}

This shows as an example of how to do the task, you will need to take care of check scanf's result to make sure the input was successful, and check malloc for allocation too.

fluter
  • 13,238
  • 8
  • 62
  • 100
  • OP didn't mention anything about sorting... are you answering another question or just in the same class? :) – PeteB Apr 30 '16 at 05:00
  • Good catch @PeteB I was seeing "Sorting" instead of "Storing", my eyes were falling. ;-) – fluter Apr 30 '16 at 05:02
  • But how is that possible if they all have to be inputted on the same line? How can each number be read one by one if the num integer you have said is on the same line as the rest?? – RRr Mmm Apr 30 '16 at 05:05
  • @RRrMmm scanf will take care of that. – fluter Apr 30 '16 at 05:06
  • but you've written two scanf statements, doesnt that mean it would be on two lines? – RRr Mmm Apr 30 '16 at 05:10
  • @RRrMmm no, it will read the numbers you want, no matter how many lines are there. – fluter Apr 30 '16 at 05:11
0

If you want to set the array size based on an input then the input has to be read first. What you want is to input all numbers in a line(including size of the array), which means the numbers will be read after the end of line only(after pressing [Enter]). This case is not possible with static arrays.

You can't read all the inputs i.e. array size & integers to be put in it on the same line & build an static array. But you may build a dynamic array by implementing a dynamic array and taking all inputs in the same line.

A dynamic array is a re-sizable array or array list with random access, variable-size list data structure that allows elements to be added or removed.

A static array required the size of the array to be created so that that memory could be allocated before array elements can be added to it.

All this being said, if your array size has an upper-bound size(i.e. the maximum number of elements which you would accept is set) then you may create a static array of the maximum size and read you inputs into it.

int arr[MAX_ARRAY_SIZE ASSUMED];

This allocates an array of size MAX_SIZE_ASSUMED which means all your inputs maybe stored in the static array. But this is a bad way to implement as MAX_SIZE_ASSUMED is always the size of the array even when you may just enter one element into the array.

Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • @RRrMmm just search form dynamic implementation of arrays in C and you would get it. Also refer : [Dynamic allocation with scanf()](http://stackoverflow.com/questions/3911547/dynamic-allocation-with-scanf) – Ani Menon Apr 30 '16 at 05:12