C programming

Question: Write a program that reads integers from the keyboard until the user enters the sentinel value -999. The program should then print the minimum integer read, the maximum integer read and the average of all the integers. (Excluding the sentinel value -999)

I know how to post maxes & mins and set the sentinel value. I don't know how to tell the computer to sum up the values given and take the average since I need a generic function that would work for any number of values.

My code for everything else:

#include <stdio.h>

int main()
{

int num;
int min;
int max;
int avg;


printf("Enter a number (-999 to quit): ");
scanf("%d",&num);

//putting the first number the user entered in variable min and max
min = num;
max = num;


while(num!=-999)
{
if(num<min)
{
min = num;
}


if(num>max)
{
max = num;
}
printf("Enter another number (-999 to quit): ");
scanf("%d",&num);
}

printf("\nThe smallest number entered is %d\n",min);
printf("\nThe largest number entered is %d\n",max);



return 0;
}

  1. 👍
  2. 👎
  3. 👁
  1. The program has a good skeleton and observes many good practices such as declaring the variables before first use (or else it won't compile), initializing variables min and max after first scanf, etc.

    To calculate the total and average, you need to:
    add variables count and total.
    Count should be set to one at the same time as max=num, total should be set to num at the same time.

    Inside the while loop, increment count by one and increment total by num whenever num!=-999.

    After the execution exits the loop, the total will represent the total of the numbers, and avg(should have been declared as float) can be calculated using:

    avg=(float)(total)/count;

    Give it a try and post what you've got. You should not be far from finishing it.

    1. 👍
    2. 👎
  2. "Inside the while loop, increment count by one and increment total by num whenever num!=-999."

    Where exactly inside the loop should I do this and how?
    Do I say count++
    to increment count by one, and do I say
    total = total+num
    inside the while loop to increment total and if so where?

    This program runs but I can't get the correct mean average value:

    int main()
    {
    //Variable declarations
    int num;
    int min;
    int max;
    float avg;
    int count;
    int total;


    printf("Enter a number (-999 to quit): ");
    scanf("%d",&num);


    min = num;
    max = num;
    count = 1;
    total = num;

    until the user enters -999

    while(num!=-999)
    {
    count++;
    total = num;

    if(num<min)
    {
    min = num;
    }


    if(num>max)
    {
    max = num;
    }
    avg = total/count;
    printf("Enter another number (-999 to quit): ");
    scanf("%d",&num);
    }


    printf("\nThe smallest number entered is %d\n",min);
    printf("\nThe largest number entered is %d\n",max);
    printf("\nThe mean average value is %f\n",avg);

    //Exit program
    return 0;
    }

    1. 👍
    2. 👎
  3. Functions... how to pass value from one function to the next... using different methods

    1. 👍
    2. 👎
  4. construct a C program that reads a positive three digit integer number and determine if the number satisfy the property or not.

    1. 👍
    2. 👎

Respond to this Question

First Name

Your Response

Similar Questions

  1. Programming

    Write a pseudocode to represent the logic of a program that allows a user to enter an hourly pay rate and hours worked. The program outputs the user's gross pay

  2. CIS

    Draw a flowchart or write pseudocode to represent the logic of a program that allows the user to enter a value. The program multiplies the value by 10 and outputs the result.

  3. Programming

    Draw a flowchart or write pseudo code to represent the logic of a program that allows the user to enter values for the current year and the user’s birth year. The program outputs the age of the user this year. Preferably a

  4. Ic

    Write an application that prompts the user for two integers and then prompts the user to enter an option as follows: 1 to add the two integers, 2 to subtract the second integer from the first, 3 to multiply the integers, and 4 to

  1. java programming

    1. Time Calculator Write a program that asks the user to enter a number of seconds. • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the

  2. C++

    // Program: name of program // Author: your name // Date Written: date you began writing the program /* Abstract: Brief description of the program */ Include in-line comments in the body of your program. You can convert

  3. intro to computer programming

    Write a program that asks the user to enter the number of packages purchased. The program should then display the amount of the discount (if any) and the total amount of the purchase after the discount. Your program should use a

  4. computer science

    In this exercise you will create a program that computes the average of a collection of values entered by the user. The user will enter 0 as a sentinel value to indicate that no further values will be provided. Your program should

  1. Programming in c++

    Draw a flow-chart and Write a program that asks the user to enter students grades. The program should stop reading when the use enters ‘*’. In addition, your program should print the number of students got each grade. The

  2. Programming

    Write a pseudocode program that asks the user for an integer between 1 and 12. The program is to calculate the times table for that number. So for example, if the user enters a 2, then the program has to calculate 2x1 and display

  3. java

    Write a java application that finds the smallest of several integers. Prompt the user for the number of integers that will be input, then prompt the user for that number of integers. Evaluate the integers to determine the smallest

  4. Computers (Programming by Python)

    Problem 1: Multiplication Drill Computers can perform calculations extremely quickly and accurately, but it's often handy to calculate with the computers we carry around inside our heads. Write a program to drill you in

You can view more similar questions or ask a new question.