Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Largest Integer in a set?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
If a user enters 3 integers how do I find out which number is the largest? I'm pretty new to c++, right now I know how to use IF statements. Is there a way to find the largeste using IF? Thanks
 
Using "if"'s would not be the way to go for a large quantity of numbers but in your case with just 3 numbers it is a simple comparison

if(a<b)
{
if(b<c)
return c;

// know that b is largest
return b;
}

else
{
if(a<c)
return c;

return a;

}


That is the basics of it, though I would suggest looking into a loop because as the number of numbers you have increases, this code will grow exponentially.


Matt
 
What if I was trying to find the largest in a set of 6??? Thanks
 
You should make you own function, say,

int max(int a[], int n){
int nMax = a[0];
for (int i=1;i<n;i++)
if (a>nMax) nMax = a;
return nMax;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top