1

I am trying to solve an SPOJ problem. I am stuck here.

For the input, it asks me the following to take as input

Next line contain n elements, ai (1<=i<= n) separated by spaces.

I can use a loop and input each element given separately by the user through scanf. But as per the problem criteria, I am assuming that we need to take the input through scanf at once in a single line. Like scanf("%d %d %d", &a1 &a2 e.t.c).

But the range is like over 10^6, I am not sure how we can dynamically input multiple values through scanf in a single line.

user3129609
  • 57
  • 1
  • 2
  • It doesn't make sense for an application to ask the user to pass 10^6 inputs through `scanf`. You probably want to pass them in a file (so the user inputs only the file name). – barak manos Aug 06 '16 at 09:30

2 Answers2

1

You can run your iteration as you say, because scanf does not care what kind of whitespace separates integer inputs.

So: for (i = 0; i < n; ++i) scanf("%d", &array[i]); will work for inputs of the type:

3 2 1 2 3 8

as well as the type

3
2
1
2
3
8
Andrey Mishchenko
  • 3,986
  • 2
  • 19
  • 37
0

Does not matter whether you input the numbers in a single line, this will work as scanf ignores the white spaces

int arr[1000001];  // Take an array to store the inputs

for(i=1;i<=n;i++)
{
    scanf("%d",&arr[i]);
}
Kaustav Ray
  • 744
  • 6
  • 20
  • 1
    `i` should probably start at `0`. – Andrey Mishchenko Dec 26 '13 at 03:09
  • Is that a compulsion ? – Kaustav Ray Dec 26 '13 at 03:10
  • It is a reflection of the fact that you are wasting the zeroth slot of the array if you write 1-based-indexing code in a 0-based-indexing language. – Andrey Mishchenko Dec 26 '13 at 03:11
  • I am not able to understand that how I am wasting ! I know c is 0 based indexing language. Can you give me any link or any other help ? – Kaustav Ray Dec 26 '13 at 03:16
  • Yes,it does not ! But there is no wastage ! I am just using another indexing ! if n==3, I am using arr[1], arr[2], arr[3] – Kaustav Ray Dec 26 '13 at 03:21
  • That piece of memory is "wasted" because you allocate it but never use it. – Andrey Mishchenko Dec 26 '13 at 03:22
  • http://stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c Theres is nothing like memory wastage ! – Kaustav Ray Dec 26 '13 at 03:34
  • Yes, that's right. The 3rd element of the array is located at `arr[2]`. If `n = 3` then `arr` is initialized as `int arr[n]`, and you don't access `arr[3]` because there is no `arr[3]`. – Andrey Mishchenko Dec 26 '13 at 03:34
  • Yes, exactly It is according to your indexing `arr[3]` is not there ! But according to my indexing `arr[3]` is there. That means different indexing does not mean that there is memory wastage ! And also there is nothing like existence or non-existence of arr[3]. it is simply depends on your indexing technique ! – Kaustav Ray Dec 26 '13 at 03:37
  • If, for `n = 3`, you write `int arr[4]; for (i = 1; i <= 3; ++i) scanf("%d", &arr[i]);`, the code will work, but `arr[0]` will never be used. How is that not a waste of the memory allocated for `arr[0]`? *The memory is still allocated, and available to you, but you never make reference to it.* – Andrey Mishchenko Dec 26 '13 at 03:41
  • Who said that there is arr[0] exist in the memory ? It is indexing which is opted by standard. – Kaustav Ray Dec 26 '13 at 03:46
  • ..................... `arr[0]` is the first element of the array. If the array is one more more elements long, then `arr[0]` exists in memory as far as the C standard is concerned. – Andrey Mishchenko Dec 26 '13 at 03:48
  • Yes, I agree with you. But actually is there any location known as `arr[0]` exist in the memory. it is simply a memory location and it can be accessed using `arr[0]` indexing or `arr[1]` – Kaustav Ray Dec 26 '13 at 03:51
  • Sorry, no clue what you mean. I'm done with this discussion though, I think I've made as much sense as I'm able to. – Andrey Mishchenko Dec 26 '13 at 03:51