The question is:
Write a program that reads 3 integer numbers, then finds and prints the: Mean - Maximum & Second Minimum.
I did this .. what's wrong??
#include <iostream>
using namespace std;
int main ()
{
double a, b, c;
cout<< "Please enter three values"<<endl;
cin>> a>> b>> c;
if
((a>b)&&(a>c))
cout<< "Maximum is a << Second Minimum is c"
<< "Mean = (a+b+c/3)"<< endl;
else
if
((b>a)&&(b>c))
cout<< "Maximum is b << Second Minimum is c"
<< "Mean = (a+b+c/3)"<<endl;
else
if ((a==b)&&(a==c))
cout<< "Maximun is a & b & c << Second Minimum is b "
<< " Mean = (a+b+c/3)"<<endl;
return 0;
}
Whatever you put within the double quotes gets printed out as text. If you want to print out the value of a variable, you need to split that out of the text portion.
e.g. cout << "The value in variable a is " << a << endl;
Also - you're not catching all situations in your conditions. What if a is smaller than both b and c? How many different ways could three values be ordered?
I've just put your code in code tags here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main ()
{
double a, b, c;
cout<< "Please enter three values"<<endl;
cin>> a>> b>> c;
if ((a>b)&&(a>c))
cout<< "Maximum is a << Second Minimum is c" << "Mean = (a+b+c/3)"<< endl;
elseif ((b>a)&&(b>c))
cout<< "Maximum is b << Second Minimum is c" << "Mean = (a+b+c/3)"<<endl;
elseif ((a==b)&&(a==c))
cout<< "Maximum is a & b & c << Second Minimum is b " << " Mean = (a+b+c/3)"<<endl;
return 0;
}
You still have the variable names within double quotes. If I'm understanding what you're trying to do, you need cout << "Maximum is " << a (Although in the case you just posted, a seems to be the smallest number, not the maximum)
You need to separate the conditions in the if statement.
if (a < b && b < c) (not a<b<c)
#include <iostream>
using namespace std;
int main ()
{
int sum =0;
int mean=0;
float a, b, c;
cout<< "Please enter three values"<<endl;
cin>> a>> b>> c;
sum = a + b + c;
mean = a+b+c/3.0;
if
((a>b)&&(a>c))
cout<< "Maximum is " << c << "Second Minimum is " << b
<< mean << endl;
else
if
((b>a)&&(b>c))
cout<< "Maximum is "<< c << " Second Minimum is "<< b
<< mean <<endl;
else
if (a<b<c)
cout<< "Maximum is "<< c << " Second Minimum is "<< a
<< mean <<endl;
else
if ((a==b)&&(a==c))
cout<< "Maximun is " << b << " Second Minimum is "<< (a < b && b < c)
<< mean <<endl;
return 0;
}
Division takes precedence over addition so a+b+c/3.0 is a+b+(c/3.0). You need to add parentheses to make it (a+b+c)/3.0.
Also you're supposed to enter 3 integers and compute the mean, which will be a floating point number. You've declared the input numbers and floats and the mean as an int - the opposite of what's required.
An easier way to do all of this is to put the 3 numbers in an array and sort it.