2

How may I count & extract a number of elements from stdin?

For instance, len() is easily available in Python, but not in C?

Im trying to compute an average, when user enters for eg: 1 3 5 7 8 10 11 -1

I've tried the following:

while (user_input != -1)
{
    total   += user_input;
    scanf ("%d", &user_input);

    //cannot work               
    average = total /  ...
}       
printf("Average = %f\n", average);  
Alok Save
  • 202,538
  • 53
  • 430
  • 533
brainsfrying
  • 241
  • 1
  • 6
  • 21

2 Answers2

1

You'll have to maintain a counter to do what you're trying to do. its just int counter = 0; and in the loop: counter++

int counter = 0;
while (user_input != -1)
{
    total   += user_input;
    counter ++;
    scanf ("%d", &user_input);
}       
average = total / counter;
printf("Average = %f\n", average);  

obviously, you'll have to check if scanf() returned atleast 1

--- EDIT --- the following program(that corresponds to the previous program) is VALID and works as required. People who do not understand how scanf() works, should stay the damn out:

#include <stdio.h>

int main(int argc, char *argv[])
{
   int total = 0;
   float average = 0.0f;
   int userinput = 0;
   int counter = -1;
   while(userinput != -1){
      counter ++;
      if(scanf("%d",&userinput) == 1 && userinput != -1){
         total += userinput;
      }
   }
   average = ((float)total/(float)counter);
   printf("Average = %f", average);
   return 0;
}

Input: 10 20 30 40 50 60 -1 Output: Average = 35

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • 2
    -1 : Clearly, this answer is no longer relevant after OPs Q edit & the comments from OP. In short it answers something which OP doesn't ask for. Also, it is not going to be useful to anyone who stumbles upon this answer in future.Please edit it to answer what the OP asks and I shall remove the downvote. – Alok Save Jan 20 '13 at 12:59
0

From your modified question & comments, it seems what you really want to do is read a line of input from the user and then extract numbers from that line of input.
The answer is you cannot do that using scanf, You need to use fgets to read the line from stdin and then you need to extract numbers from that read line. Here is a,
Quick sample program:

#include<stdio.h>
int main()
{
    char line[1024], *beg, *end;
    long readvalue;
    while (fgets(line, sizeof(line), stdin)) 
    {
        beg = line;
        for (beg = line; ; beg = end) 
        {
            readvalue = strtol(beg, &end, 10);
            if (beg == end)
                break;
                printf("[%d]",readvalue);
        }
    }
    return 0;
}

Input:

1 3 5 7 8 10 11 -1

Output:

[1][3][5][7][8][10][11][-1]
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 2
    I would give this a -1, how can it count the no. of elements IN the `scanf()`? Its only the no. of elements successfully returned IIRC – Aniket Inge Jan 20 '13 at 11:56
  • 1
    @Aniket: Have you **read** the answer? Important lesson for you, **Read** then comment and not the other way round. – Alok Save Jan 20 '13 at 11:58
  • 1
    @Aniket Not quite. "Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure" - from the documentation [here](http://pubs.opengroup.org/onlinepubs/000095399/functions/fscanf.html). –  Jan 20 '13 at 11:58
  • @Alok Save: ty. i just edited my qn. suppose that the user input was: 1 3 5 7 8 9 -1 in a single line, how may i still compute the average? – brainsfrying Jan 20 '13 at 12:05
  • @brainsfrying: Does the user enter 1 3 5 7 8 9 -1 at once? or does the user enter a single number on each iteration? – Alok Save Jan 20 '13 at 12:11
  • @AlokSave: Yes, all at once. Hence I have to compute the average from that single line of inputs. – brainsfrying Jan 20 '13 at 12:15
  • @AlokSave: Do you have a sample? As in for this case? – brainsfrying Jan 20 '13 at 12:24