Hi:
I am learning C and I am having a little trouble with this program.
The task was to write a program that can determine the largest and smallest integers.
I am using a while loop to accumulate the numbers
the value 0 acts as a "sentinel" value that will stop the program.
The problem is when I enter 0 to stop the program, it will take 0 as the smallest number. This also happens when I do other values like -99 or others. Any help will be greatly appreciated.
Code:
//Write a program that reads an integer and determines and prints whether it is od or even
#include <stdio.h>
#include <math.h>
#include <limits.h>
int main(void)
{
int numbers;//numbers
int counter = 0;//counter of variables
int accumulator = 0;//adds all numbers up
int average;//computes average of all the numbers
int largest = 0;
int smallest = 0;
int counting = -99;//value that, when entered, will stop program.
while(numbers != 0)
{
printf("Please enter a number. Enter 0 to end.\n");
scanf("%d", &numbers);
accumulator += numbers;
if(numbers == 0)
{
printf("Thank you.\n");;
if(numbers < smallest && numbers !=0 );
{
numbers = smallest;
}//end if
}//end if
else
{
counter++;
}//end if
if(numbers % 2 == 0 && numbers != 0)
{
printf("%d is an even number.\n", numbers);
}//end if
else if(numbers % 2 != 0 && numbers != 0)
{
printf("%d is an odd number.\n", numbers);
}//end else
if(numbers < smallest && numbers != 0)
{
smallest = numbers;
}//end if
if(numbers > largest && numbers != 0)
{
largest = numbers;
}//end if
}//end while
average = accumulator/counter;
if(counter > 2)
{
printf("User made %d entries\n", counter);
}//end if
else
{
printf("User made 1 entry.\n");
}//end else
printf("Total of all entries: %d\n", accumulator);
printf("Average: %d\n", average);
printf("Largest number: %d\n ", largest);
printf("Smallest number: %d\n ", smallest);
}//end main