Thursday, July 30, 2009

Does any body know how I can compare more than 2 integers in c++ to get the largest and smallest value?

I want to compare 5 integers in c++ but i don't know how to do that .I know how to compare two values but for more than that the code does not yield any result and I just get the cout%26lt;%26lt;whatever is typed%26lt;%26lt;endl;


I would be grateful if you send me the syntax

Does any body know how I can compare more than 2 integers in c++ to get the largest and smallest value?
buddy i dnt know exactly about c++ bt try the following,may b it wll work,so here it goes





first input all the five nos from user or declare 5 nos in a,b,c,d,e,





if(a%26gt;b %26amp;%26amp; a%26gt;c %26amp;%26amp; a%26gt;d %26amp;%26amp; a%26gt;e)


cout%26lt;%26lt;a;


else if(b%26gt;a %26amp;%26amp; b%26gt;c %26amp;%26amp; b%26gt;d %26amp;%26amp; b%26gt;e)


cout%26lt;%26lt;b;


else if(c%26gt;a %26amp;%26amp; c%26gt;b %26amp;%26amp; c%26gt;d %26amp;%26amp; c%26gt;e)


cout%26lt;%26lt;c;


else if(d%26gt;a %26amp;%26amp; d%26gt;b %26amp;%26amp; d%26gt;c %26amp;%26amp; d%26gt;e)


cout%26lt;%26lt;d;


else


cout%26lt;%26lt;e;





this will give the largest no.,similarly you can do for smallest by changing the compare sign.


all the best!
Reply:it just happens when I am trying to compare 3 integers .when I compare 4 the result is correct. I will send you the code and maybe you can compile it yourself to see the result.if find out the cause let me know.


regards Report It

Reply:#include%26lt;iostream%26gt;


using namespace std;


int main()


{


int a,b,c;


cout%26lt;%26lt; "enter three integer to compare "%26lt;%26lt;endl;


cin%26gt;%26gt;a%26gt;%26gt;b%26gt;%26gt;c;


if (a%26gt;b,a%26gt;c)


cout%26lt;%26lt;a%26lt;%26lt;" is the largest"%26lt;%26lt;endl; Report It

Reply:else if (b%26gt;a,b%26gt;c)


cout%26lt;%26lt;b%26lt;%26lt;" is the largest "%26lt;%26lt;endl;





else


cout%26lt;%26lt;c%26lt;%26lt; " is thelargest"%26lt;%26lt;endl;


system("pause");


return 0;


} Report It

Reply:Using STL is the easiest way





#include %26lt;iostream%26gt;


#include %26lt;algorithm%26gt;


using namespace std;


int main()


{


int a[ ] = { 4, 5, 88, 37, 96}; //no matter how many integers


cout %26lt;%26lt; max_element(a[0], a[4]) %26lt;%26lt; endl; //output the maxi


cout %26lt;%26lt; min_element(a[0], a[4] ) %26lt;%26lt; endl; //output the minimum


return 0;


}
Reply:Bubble Sort (works very slow)


void bubbleSort(int numbers[], int array_size)


{


int i, j, temp;





for (i = (array_size - 1); i %26gt;= 0; i--)


{


for (j = 1; j %26lt;= i; j++)


{


if (numbers[j-1] %26gt; numbers[j])


{


temp = numbers[j-1];


numbers[j-1] = numbers[j];


numbers[j] = temp;


}


}


}


}





Quck sort


void quickSort(int numbers[], int array_size)


{


q_sort(numbers, 0, array_size - 1);


}








void q_sort(int numbers[], int left, int right)


{


int pivot, l_hold, r_hold;





l_hold = left;


r_hold = right;


pivot = numbers[left];


while (left %26lt; right)


{


while ((numbers[right] %26gt;= pivot) %26amp;%26amp; (left %26lt; right))


right--;


if (left != right)


{


numbers[left] = numbers[right];


left++;


}


while ((numbers[left] %26lt;= pivot) %26amp;%26amp; (left %26lt; right))


left++;


if (left != right)


{


numbers[right] = numbers[left];


right--;


}


}


numbers[left] = pivot;


pivot = left;


left = l_hold;


right = r_hold;


if (left %26lt; pivot)


q_sort(numbers, left, pivot-1);


if (right %26gt; pivot)


q_sort(numbers, pivot+1, right);


}





There are few more and all have their own advantages, disadvantages. Example: Speed vs. amount of memory used etc etc. Depending on your big O requirement use what's needed.
Reply:int nominal = all[0], answer;


// the five numbers are all[]





for (int x = 1; x %26lt; 5; x++) if (nominal %26gt; all[x]) nominal = all[x];


answer = nominal;


// answer is smallest by now.


No comments:

Post a Comment