-3

I'm in a beginner C course and I was wondering if there's a way to input integers straight across and averages them together? I'm trying to make my program nice and tidy as possible.

I want to input integers straight across like:

Enter the temperatures and Enter 00 when finished:

 60 80 97 42

 Average is: 69.75

I don't want to input integers like shown below:

Enter the temperatures and Enter 00 when finished: 75

Enter the temperatures and Enter 00 when finished: 80

Enter the temperatures and Enter 00 when finished: 46

Enter the temperatures and Enter 00 when finished: 91

Average is: 73

soumya
  • 3,801
  • 9
  • 35
  • 69
Nejoii
  • 11
  • define "straight across" – Alyssa Haroldsen Jul 27 '15 at 22:09
  • 1
    Show us what you've tried so far, what you thought it should do and what is actually happening – MoMo Jul 27 '15 at 22:10
  • 3
    Read the entire line and use some appropriate function to split the values (like strtok). – jpw Jul 27 '15 at 22:10
  • `0` and `00` will be distinguished? – BLUEPIXY Jul 27 '15 at 22:13
  • You did not enter `00` in the first example you said you like. – Weather Vane Jul 27 '15 at 22:34
  • 1
    1) read white-space using `fgetc()` (if code encounter `\n`, stop reading) if non-white-space read, use `ungetc()` 2) `if scanf("%d", &num) == 0` quit because non-number read. 3) add `num` to running total. Go to step 1. Divide total by count of numbers read. Note: no limit to line length. See http://stackoverflow.com/a/27155807/2410359 – chux - Reinstate Monica Jul 27 '15 at 22:35
  • possible duplicate of [Scan multiple integers without knowing the actual number of integers](http://stackoverflow.com/questions/27147393/scan-multiple-integers-without-knowing-the-actual-number-of-integers) – jpw Jul 27 '15 at 22:44

6 Answers6

2
#include <stdio.h>
#include <string.h>

int main(void){
    char input[64];
    double ave = 0.0, value;
    int count = 0;

    printf("Enter the temperatures and Enter 00 when finished:\n");
    while(1){
        if(1==scanf("%63s", input)){
            if(strcmp(input, "00") == 0)
                break;
            if(1==sscanf(input, "%lf", &value))
                ave += (value - ave) / ++count;
        }
    }
    if(count)
        printf("Average is: %g\n", ave);
    else
        printf("Input one or more values\n");

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

I believe that you shall change your terminating condition from enter 00 to something like enter x. So, your code shall look like this::

int n;
int sum = 0, count = 0;
while(scanf("%d", &n)) {
    sum = sum + n;
    count++;
}
printf("%lf", double(sum/count));

scanf returns the number of successfully taken inputs. Since n is declared as int, so everytime you enter some integer value, scanf will return 1 and if you enter some value which is not of type int like if you enter x (which is a char) scanf will return 0, because x is not an integer, and this way you can calculate the average.

user007
  • 2,156
  • 2
  • 20
  • 35
0

Using the scanf function any white space character is seen as the end of input for each integer. Thus using scanf within a loop you can continuously input values within the same line.

If you want it to work for a different number of entries each time you must modify the code to use a while loop and have a dynamically allocated array, since the size is unknown. Then check for an escape sequence like 00.

All the values are stored into an array where you can do the averaging calculations

#include <stdio.h>

#define NUM_OF_ENTRIES 5

int main()
{
    printf("Enter numbers: ");
    int i = 0;
    int value_set[NUM_OF_ENTRIES];
    for (i = 0; i < NUM_OF_ENTRIES; i++ )
    {
        scanf("%d", &value_set[i]);
    }
E-rap
  • 13
  • 4
0

Code can use scanf("%d", &number) to read an integer. The trouble is that "%d" first scans and discards leading white-space which includes '\n' before scanning for an int. '\n' is needed to know when to stop as OP wants "input integers straight across". So instead code should look for white-space one character at a time first. Upon finding the end-of-line '\n', scanning is complete.

With this approach there are no practical limits to the count of numbers.

#include <ctype.h>
#include <stdio.h>

double Line_Average(void) {
  double sum = 0;
  unsigned long long count = 0;

  while (1) {
    int ch;
    while (isspace(ch = fgetc(stdin)) && ch != '\n')
      ;
    if (ch == '\n' || ch == EOF) {
      break; // End-of-line or End-if file detected.
      }
    ungetc(ch, stdin); // Put back character for subsequent `scanf()`

    int data;
    if (scanf("%d", &data) != 1) {
       break; // Bad data
    }
    sum += data;
    count++;
  }
  return sum/count;
}

// sample usage
puts("Enter the temperatures");
double Average = Line_Average();
printf("Average is: %.2f\n", Average);
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

One possibility:

  double sum = 0;
  double val;
  size_t count = 0;

  char follow;

  while( scanf( "%lf%c", &val, &follow ) == 2 )
  {
    sum += val;
    count++;
    if ( follow == '\n' )
      break;
  }

  printf( "average = %f\n", sum/count );

This will read each number plus the character immediately following, until it sees a newline or a non-numeric string. It's not perfect; if you type a number followed by a space followed by a newline, then it won't break the loop. But it should give you some ideas.

John Bode
  • 119,563
  • 19
  • 122
  • 198
-1

since u did not post the code. i have given a sample code... from here u can build what u require with some tweaks

#include<stdio.h>

main()
{
   int one, two, thr, four, five, avg;
    printf("\nEnter the temperatures and Enter 00 when finished:");
    scanf ("%d %d %d %d %d", &one, &two, &thr, &four, &five);
    avg=(one+two+thr+four+five)/5;
    printf("Average value is %d", avg);
}
  • This does not work if there are not exactly 5 values entered. Another problem is that if nr 5 is the 00 value, you're still using it for the average calculation. – Marleen Schilt Oct 29 '15 at 10:21