English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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< I would be grateful if you send me the syntax

2007-01-15 01:41:48 · 4 answers · asked by mehrab f 1 in Computers & Internet Programming & Design

4 answers

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>b && a>c && a>d && a>e)
cout< else if(b>a && b>c && b>d && b>e)
cout< else if(c>a && c>b && c>d && c>e)
cout< else if(d>a && d>b && d>c && d>e)
cout< else
cout<
this will give the largest no.,similarly you can do for smallest by changing the compare sign.
all the best!

2007-01-15 02:14:22 · answer #1 · answered by ur frnd 1 · 0 0

Using STL is the easiest way

#include
#include
using namespace std;
int main()
{
int a[ ] = { 4, 5, 88, 37, 96}; //no matter how many integers
cout << max_element(a[0], a[4]) << endl; //output the maxi
cout << min_element(a[0], a[4] ) << endl; //output the minimum
return 0;
}

2007-01-15 02:19:13 · answer #2 · answered by platobungee 2 · 1 0

int nominal = all[0], answer;
// the five numbers are all[]

for (int x = 1; x < 5; x++) if (nominal > all[x]) nominal = all[x];
answer = nominal;
// answer is smallest by now.

2007-01-15 02:01:04 · answer #3 · answered by Andy T 7 · 0 0

Bubble Sort (works very slow)
void bubbleSort(int numbers[], int array_size)
{
int i, j, temp;

for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > 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 < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > 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.

2007-01-15 01:45:36 · answer #4 · answered by Rival 3 · 0 0

fedest.com, questions and answers