-3

I'm studying loops in class and for one of the labs, I have to figure out a way for the user to enter an unspecified number of integers to calculate the average. I know I can have the user enter the number of integers to be averaged in order for the loop to be terminated like below:

int count = 0, value = 0, sum = 0, numberofintegers = 0; 
double avg = 0;

 printf("enter the number of integers you wish to average\n");
scanf("%d",&numberofintegers);
//loop
while (count < numberofintegers)
{
printf("enter a positive integers\n");
scanf("%d",&value);
sum = sum + value;
count = count + 1;
} 

avg = (double) sum/count;

So basically I could have a user input the number of integers to be averaged in order for the loop to terminate, but there has to be another way to make the loop terminate without having the user input it?

  • you want to terminate the loop without take numberofintegers value? – Jigar Apr 20 '16 at 03:31
  • 1
    `printf("enter a positive integers\n");` The key word there is *"positive"*. Don't ask the user how many number they want to enter. Just count how many numbers are entered until the user enters a negative number. – user3386109 Apr 20 '16 at 03:33
  • @user3386109 : I would assume the user be so blunt so that he don't know how to enter negative integers. – sjsam Apr 20 '16 at 03:37
  • sorry there is more of the code which is an if/else statement which determines whether the user entered a value > 0. Anyways, it doesnt matter in this question. In this case, I want to know how to terminate the loop without the "numberofintegers" variable. –  Apr 20 '16 at 03:40
  • Terminate the loop when the user enters a negative number. See John's answer. John's answer requires -1 to terminate the loop, I would allow the user to enter any negative number to terminate the loop. sjsam's solution to terminate the loop when the user enters (non-numeric) garbage is also valid. – user3386109 Apr 20 '16 at 03:48

3 Answers3

1

Normally you'd use a predetermined "illegal" number like (say -1)

input = read_a_value();
while(input != -1)
{
    // do something with input
    input = read_a_value();
}
John3136
  • 28,809
  • 4
  • 51
  • 69
1

scanf returns the number of successful entries.

This may solve your issue.

#include<stdio.h>

int main(void)
{
    int number, total = 0, count = 0;
    char c;
    printf("Enter a number to continue, a character to exit\n");

    while (scanf("%d", &number) == 1)
    {
        total+= number;
        count++;
    }

    /* You need to handle the case where no valid input is entered */
    (count > 0) ? printf("Average : %.2f\n", (float)total / count) : printf("No valid numbers entered\n");
    /* I have casted the average to float to keep the precision*/
    while (getchar() != '\n')
        ;;
    printf("Press any key to continue..");
    getchar();
    return 0;
}

A downfall is that the scanf will continue to prompt for input if a user presses the Enter Key repeatedly. In fact you might wish to replace the scanf with fgets. Have a look here.

Community
  • 1
  • 1
sjsam
  • 21,411
  • 5
  • 55
  • 102
0

If you are sure that the user doesn't enter too many numbers, use a string. Length of string, you can choose according to you and split it using spaces to get the numbers