Posted by E on Saturday, October 2, 2010 at 7:49pm.
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.
"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;
}
Functions... how to pass value from one function to the next... using different methods
Related Questions
Programming - Write a pseudocode program that asks the user for an integer ...
object oriented programming 1 - write a c++ program that calculates the area of ...
programming - creat a logic for a program that continuously prompts the user for...
java - Write a java application that finds the smallest of several integers. ...
C Programming - Write a program that prompts the user to enter a temperature, ...
computer science - Write an Assembly program to determine the price of a car ...
Programming in c++ - Write a program in c++ that inputs a number [1-12] from the...
Computer programming - Sorting is a common operation used in programming. ...
programming MIPS ask - Write a MIPS assembly language program that will cover ...
Computing - Write a program that displays the following menu: 1 = Square 2 = ...
For Further Reading