Hello, I am trying to write a program that does sum, total & average, along with comparing numbers of being the highest and lowest. I've succeeded in do so, but am trying to write it different ways concerning the loops. The While loops and For loops have been completed, but I'm having problems doing the Do-While. Any help would be appreciated.
//while loop
#include<iostream>
usingnamespace std;
int main()
{
float large, small, average, total, sum, a, b, c;
int thirdnum, x = 1;
cout << "Please enter two numbers:";
cin >> a >> b;
//comparing for high & low
if( a > b)
{
large = a;
small = b;
}
else
{
large = b;
small = a;
}
//calculate sum
sum = a + b;
//calculate total numbers entered
total = 2;
//calculate average
average = sum / total;
cout << "Do you wish to enter more numbers? Enter 1 for Yes or 2 for No:";
cin >> thirdnum ;
while (thirdnum == 1)
{
cout << "Please enter you number:";
cin >> c;
sum = sum + c;
total = total + 1;
average = sum / total;
if( c > large)
{
large = c;
}
else
{
if (c < small);
small = c;
}
cout << "Do you wish to enter another numbers? Enter 1 for Y or 2 for N to continue:";
cin >> x;
thirdnum = x;
if (x == 2)
{
thirdnum = 2;
}
}
cout << "The largest number is:" << large << "\n";
cout << "The smallest number is: " << small << "\n";
cout << "The average is:" << average << "\n";
cout << "The total number of numbers entered is:" << total << "\n";
cout << "The sum of all numbers is:" << sum << "\n";
return 0;
}
//for loop
#include<iostream>
usingnamespace std;
int main()
{
float large = 0, small = 0, sum = 0, average, total, x;
int n;
cout << "Please enter a number: ";
for (int n=0; n < 6; n++)
{
cin >> x;
if( x > large)
{
large = x;
}
else
{
small = x;
}
sum = sum +x;
//n is all the numbers
//total = n;
average = sum / n;
}
cout << "The largest number is:" << large << "\n";
cout << "The smallest number is: " << small << "\n";
cout << "The average is:" << average << "\n";
cout << "The total number of numbers entered is:" << "6" << "\n";
cout << "The sum of all numbers is:" << sum << "\n";
return 0;
}
//do-while loop
#include<iostream>
usingnamespace std;
int main()
{
float large, small, average, total, sum, no, a, b, c;
int thirdnum, x = 1;
cout << "Please enter two numbers:";
cin >> a >> b;
do {
//comparing for high & low
if( a > b)
{
large = a;
small = b;
}
else
{
large = b;
small = a;
}
//calculate sum
sum = a + b;
//calculate total numbers entered
total = 2;
//calculate average
average = sum / total;
cout << "Do you wish to enter more numbers? Enter 0 for No:";
cin >> c;
}
while (c == 0);
{
cout << "The largest number is:" << large << "\n";
cout << "The smallest number is: " << small << "\n";
cout << "The average is:" << average << "\n";
cout << "The total number of numbers entered is:" << total << "\n";
cout << "The sum of all numbers is:" << sum << "\n";
}
return 0;
}