Reputation: 11
This program is supposed to end when the user enters 0 and then show the count, sum, average, min and max. I am able to figure out the sum count and average but my min and max isn't working.
int main()
{
int number = 0;
int count = 0;
int sum = 0;
int average;
int min = 0;
int max = 0;
do {
printf("Enter a number: ");
scanf_s("%d", &number);
if (number > 0)
{
count = count + 1;
sum += number;
min = number;
max = number;
}
average = sum / count;
if (number < min)
{
min = number;
}
else if (number > max)
{
max = number;
}
} while (number != 0);
printf("count: %d\n", count);
printf("Sum: %d\n", sum);
printf("average: %d\n", average);
printf("Minimum: %d\n", min);
printf("Maximum: %d\n", max);
system("pause");
}
Upvotes: 1
Views: 3472
Reputation: 4641
int main()
{
int number = 0;
int count = 0;
int sum = 0;
int average;
int min = INT_MAX; // don't forget to include limits.h
int max = INT_MIN;
do {
printf("Enter a number: ");
scanf_s("%d", &number);
if (number > 0)
{
count = count + 1;
sum += number;
if (number < min)
{
min = number;
}
if (number > max)
{
max = number;
}
}
else if (number < 0)
{
printf("Negative value entered...skipping");
}
} while (number != 0);
printf("count: %d\n", count);
printf("Sum: %d\n", sum);
average = sum / count;
printf("average: %d\n", average);
printf("Minimum: %d\n", min);
printf("Maximum: %d\n", max);
system("pause");
}
You need to do two things:
min = number;
and max = number
from the check number > 0
. This is because, you are overriding the variables with the number value. This will lead to loss of previous min
and max
values, if any. int min = 0;
and int max = 0
, use the upper and lower
limit for integer data type. That is present in limits.h
. You can use INT_MAX
(2147483647) and INT_MIN
(–2147483648).else if (number > max)
, it should be if (number > max)
. For every number you need to check for both min and max values.if (number > 0)
should instead be if (number != 0)
. This is because you want the program to end when user enters
0
, so for it to accept negative numbers that condition has to be
changed. average
every single time. Instead
you can calculate after coming out of the loop.number != 0
. The reason for this is that you don't want to calculate min and max when number == 0
.Upvotes: 2
Reputation: 182
Set the first number that scanf reads as initial value of min and initial value of max;This will always work whatever the numbers are.
if (count==1)
{
min=number;
max=number;
}
Upvotes: 1
Reputation: 357
First, initialize min and max with proper values.
int min = INT_MAX;
int max = INT_MIN;
Second, update min only when the input number less than current min. Update max only when the input number is greater than the current max.
Upvotes: 3
Reputation: 1612
You used:
if (number > 0)
{
count = count + 1;
sum += number;
min = number;
max = number;
}
Here, don't use:
min = number;
max = number;
Because, when number is greater than 0, min and max value will be set to the input number so the if and else if statement below it, will not work.
Upvotes: 1