I am working on a small school project, i have to
Write a program that asks the user for a series of integers one at a time. When the user enters the integer 0, the program displays the following information:
-the number of integers in the series(not including 0)
-the average of the integers
-the largest integer in the series
-the smallest integer in the series
-the difference between the largest and smallest integer in the series
My problems right now are unincluding the 0 in the entered integers(so the number of integers are 3 instead of 4, and if i enter 5, 6, 7, 0 the average will be 6 instead of 4)
I also am unsure of how to determine the largest/smallest integers, my code below is what i have so far.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
using namespace std;
int main()
{
int i, avg, diff, large, small;
int total = 0;
int numI = 0;
cout << "Press 0 to end the loop and display information." << endl;
do
{
cout << "Please enter an integer: ";
cin >> i;
numI++;
total=total+i;
} while (i!=0);
if (i>i)
{
i=large;
}
if (i<i)
{
i=small;
}
diff = large - small;
total = total + i;
avg = total / numI;
cout << "You entered 0." << endl;
cout << "Number of integers intered: " << numI << endl;
cout << "Total: " << total << endl;
cout << "Largest number: " << large << endl;
cout << "Smallest number: " << small << endl;
cout << "Average: " << avg << endl;
return 0;
}
|