functions what am i doing wrong? Please help
May 16, 2015 at 11:50pm UTC
i need to write a function that reoders 4 numbers inputed by a user in descend order. this is the code i have so far but it keeps on giving me weird output PLease Help.
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include <iostream>
using namespace std;
int sotin();
int main()
{
sotin();
return 0;
}
int sotin()
{
int a=0,b=0,c=0, d=0;
int max1, max2, max3,max4;
cout <<"Please enter 4 values: " <<endl;
cin>>a>>b>>c>>d;
if (a>b && b>c && c>d)
{
max1=a;
max2=b;
max3=c;
max4=d;
}
if (b>a && a>c && c>d)
{
max1=b;
max2=a;
max3=c;
max4=d;
}
if (c>a && a>b && b>d )
{
max1=c;
max2=a;
max3=b;
max4=d;
}
if (d>a && a>b && b>c)
{
max1=d;
max2=a;
max3=b;
max4=d;
}
cout<<"the first largest number is: " << max1<<endl;
cout<<"The second largest number is: " << max2<<endl;
cout<<"The third largest number is: " << max3<<endl;
cout<<"The fourth largest number is: " << max4<<endl;
return 0;
}
May 17, 2015 at 12:35am UTC
First of all, I would use
else if
after the first
if
Second, why not check it this way:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//first largest
if (a>b && a>c)
{
max1=a;
}
else if (b>a && b>c)
{
max1=b;
}
else if (c>a && c>b)
{
max1=c;
}
//second largest
if ((a>b || a>c) && max1!=a)
{
max2=a;
}
//etc....
Last edited on May 17, 2015 at 5:40pm UTC
Topic archived. No new replies allowed.