Reputation: 29
Following are the problem, my code and my question:
Do not use an array to hold numbers that user enters in this exercise! Write a program that calculates average of positive numbers that user enters. Program asks user to enter numbers and calculates the average of entered numbers when user enters 0 as the number. The zero is not included in the average. If user enters a negative number the program must print a message telling that only positive numbers are accepted and ignore the negative number.
Here's most of the code written:
#pragma warning (disable:4996)
#include <stdio.h>
int main() {
int number;
int sum = 0;
printf("Please enter the 1st number or 0 to stop: ");
scanf("%d", &number);
int count = 0;
while (number != 0)
{
sum = sum + number;
count++;
printf("Please enter another number or 0 to stop: ");
scanf("%d", &number);
}
if (count < 0) {
printf("Only positive numbers\n");
}
if (count > 0)
{
printf("AVERAGE = %f", ((float)sum) / count);
}
}
SPECIFIC QUESTION: If a user enters a negative number, how can I not let it affect the average and prompt the user to enter a positive number?
Upvotes: 0
Views: 2372
Reputation: 163
The following code is working as desired :-
#include <stdio.h>
int main()
{
int number;
int sum = 0;
int count = 0;
printf("Please enter the numbers and enter 0 to stop ");
scanf("%d", &number);
while (number != 0)
{
if (number > 0) {
sum += number;
count++; // increase the count only when a positive number is entered
}
else {
printf("Enter only positive numbers\n");
}
printf("Please enter another number or 0 to stop: ");
scanf("%d", &number);
}
printf("AVERAGE = %f", ((float)sum) / count);
}
The problem with Rasumus' code is that it's increasing the count even when a negative number is entered, that will show erroneous average value.
Upvotes: 0
Reputation: 29
#include <stdio.h>
int main() {
int number;
int sum = 0;
int count = 0;
printf("Please enter the 1st number or 0 to stop: ");
scanf("%d", & number);
while (number != 0)
{
if (number > 0)
{
sum = sum + number;
count++;
}
else
{
printf("only positive numbers are accepted\n");
}
printf("Please enter another number or 0 to stop: ");
scanf("%d", & number);
}
if (count > 0)
{
printf("AVERAGE = %f", ((float) sum) / count);
}
if ( count == 0)
{
printf(" no numbers to calculate average.");
}
return 0;
}
Upvotes: 0
Reputation: 27
#include <stdio.h>
int main() {
int number;
int sum = 0;
printf("Please enter the 1st number or 0 to stop: ");
scanf("%d", & number);
int count = 0;
while (number != 0) {
if (number > 0) {
sum = sum + number;
count++;
} else {
printf("only positive numbers are accepted\n");
}
printf("Please enter another number or 0 to stop: ");
scanf("%d", & number);
}
if (count > 0) {
printf("AVERAGE = %f", ((float) sum) / count);
}
return 0;
}
Upvotes: 2
Reputation: 3695
You checked if the amount of times the user entered something is negative (which it can never be). Instead just make an if statement before u add the number to the sum.
#include <stdio.h>
int main() {
int number;
int sum = 0;
printf("Please enter the 1st number or 0 to stop: ");
scanf("%d", &number);
int count = 0;
while (number != 0)
{
if (number > 0) {
sum = sum + number;
} else {
printf("Only positive numbers\n");
}
count++;
printf("Please enter another number or 0 to stop: ");
scanf("%d", &number);
}
if (count > 0)
{
printf("AVERAGE = %f", ((float)sum) / count);
}
}
Upvotes: 0