Writing a c program where the objective is to get the user's input a number of times, and when the user has input a specific age, the loop stops and outputs the average, smallest and largest of all the values the user just input; however, when determining the largest/smallest/average of the ages, the program must ignore the age that stops the while loop. I am only able to use a single loop. The only issue I am having is with the output. It is not ignoring the specific age and giving me wonky numbers. Here is a piece of my coding:
#include <stdio.h>
int age, avAge, minAge, maxAge, sum;
sum = 0;
//here is where I get the user input
while (age != -1){
printf("Please enter age: \n");
scanf("%d", &age);
//here is where I try to calculate the average
sum += age;
avAge = sum / age;
//here is where I placed restrictions on the ages the user can input
if (age >= 0 && age <= 100)
printf("Nice try!\n");
//here is where I try to get the largest/smallest in order
if (age < minAge)
minAge = age;
if (age > maxAge)
maxAge = age;
//here is where I output if the user inputs a 0
else ( age == -1){
printf("Smallest: %d\n", minAge);
printf("Largest: %d\n", maxAge);
printf("Average: %d\n", avAge)
return 0;
}
Please forgive my coding format, I'm on my phone since I'm running an uninstalled ubuntu on my computer. I just want an idea of what I should use to keep the program from using the 0 as the minAge. Since I can only use one loop, my options are limited I think. Please keep in mind I'm very new at c so I apologize for any ignorance on my part. Thank you.
avAge = sum of all ages / total number of agesif (age >= 0 && age <= 100)is suspicious, surely that's meant to beif (age < 0 && age >= 100)?age,minAgeandmaxAgeare all used whilst uninitialised.