# print the largest and smallest number in an array

Page 1 of 1

## 2 Replies - 13509 Views - Last Post: 08 February 2009 - 06:44 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=85432&amp;s=687dd523ec13527d103f12803c8cd02b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jerryroy

Reputation: 0
• Posts: 34
• Joined: 10-November 07

# print the largest and smallest number in an array

Posted 08 February 2009 - 02:38 PM

hey i am acceptint 10 values from the user an adding it to the array what i have a problem trying to figure out how to print back the largest and smallest number this is what i have so far:
```#include <iostream>

using namespace std;

int main()
{
int num,numbers[10],sum_even,average_even,sum_odd,avg_odd,count,odd_count(0),even_count(0);
int max(0),min,max_index(0),min_index(0);
sum_even = sum_odd = min = 0;

for(count=0;count<10;count++)
{
cin >>numbers[count];

if(num < numbers[count])
{
num = numbers[count];
max = num;
max_index = count;
}
if(max > min)

if(numbers[count]%2 ==0)
{
sum_even = sum_even + numbers[count];
even_count++;
}
else
{
sum_odd = sum_odd + numbers[count];
odd_count++;
}
}
avg_odd = sum_odd/odd_count;
average_even = sum_even/even_count;

cout <<"\nSum of even numbers = " << sum_even <<" Average = "<< average_even;
cout <<"\nSum of Odd Numbers = " << sum_odd <<" Average =" << avg_odd;
cout <<"\nThe Higest Number is: " << max << "The index is: " << max_index;
cout <<"\nThe Smallest Number is: " <<min << " The index is: " << min_index;
}

```

what i have so far smallest number is zero not sure what else to do? thanks for all the help

Is This A Good Question/Topic? 0

## Replies To: print the largest and smallest number in an array

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: print the largest and smallest number in an array

Posted 08 February 2009 - 02:43 PM

Easiest option is to sort the list and then output index[0] and index[size-1]

So:

```int temp;
for(int i = 0; i < 10; i++)
{
for(int j = 1; j < 10; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

cout << "smallest is: " << arr[0];
cout << "largest is: " << arr[9];

```

### #3 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: print the largest and smallest number in an array

Posted 08 February 2009 - 06:44 PM

KYA, on 8 Feb, 2009 - 01:43 PM, said:

Easiest option is to sort the list and then output index[0] and index[size-1]

So:

```int temp;
for(int i = 0; i < 10; i++)
{
for(int j = 1; j < 10; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

cout << "smallest is: " << arr[0];
cout << "largest is: " << arr[9];

```

The bubble sort can be made a little faster by using 10-i as the new upper limit each pass through the inner loop ... since we know that each pass bubbles the next largest ... to the next top position ...

```for(int i = 0; i < 10; i++)
{
for(int j = 1; j < 10-i; j++)
{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
```

But doing a sort here ... if ALL you want is the lowest and the highest value ... is perhaps a little 'overkill' ?

You can just pass through the data ONCE ... This is enough.

BEFORE ... you pass through the data ... either on input ... or after all input and then by traversing the array (or vector) of data ...

set the FIRST value to both lowest and highest ...

Then ...
test each NEXT value and update 'lowest' and highest' if appropriate.

After ONE pass ... you are done.

You don't really need to use arrays at all ... You can do all your processing after you input each new item of data to find ...

lowest
highest
average
sumOfEvenNumbers
sumOfOddNumbers

....

Shalom,
David