# Finding the difference between largest and smallest array

Page 1 of 1

## 5 Replies - 9705 Views - Last Post: 19 June 2011 - 01:53 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=236273&amp;s=898bc7c046fdde9e583a5937fd4d6ba7&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 defcon18

Reputation: 1
• Posts: 28
• Joined: 06-June 11

# Finding the difference between largest and smallest array

Posted 19 June 2011 - 01:13 PM

I need some help I am trying to create a function that will take the largest and smallest numbers in a array and subtract the maximum - minimum of those numbers indexes. It will then display the result.

```#include <iostream>
using namespace std;

int function1 (int x[], int num) {
int max= 0;
for(int c=0; c<num; c++)
{

if(x[c]>max)
max=c;
}
int min= 0;
for(int c=0; c<num; c++)
{

if(x[c]>min)
min=c;
}
int final;
final = 0;
final= (max-min);
return final;

int main() {

int arr[10]={1,2,3,4,5,60,7,8,9,10};
int cnt=9;
int ans;
ans=function1(arr, cnt);
cout << ans << endl;
system("pause");
}
```

Is This A Good Question/Topic? 0

## Replies To: Finding the difference between largest and smallest array

### #2 ishkabible

• spelling expret

Reputation: 1747
• Posts: 5,898
• Joined: 03-August 09

## Re: Finding the difference between largest and smallest array

Posted 19 June 2011 - 01:21 PM

not sure what your algorithm is but here is how i would do it.
*sort the one array least to biggest
*sort the next array biggest to smallest
*traverse the 2 arrays finding the difference between them

that was one of the Google code questions and this was the algorithm i used to solve it.

### #3 snoopy11

• Engineering ● Software

Reputation: 1438
• Posts: 4,629
• Joined: 20-March 10

## Re: Finding the difference between largest and smallest array

Posted 19 June 2011 - 01:21 PM

your missing a brace after return final;

### #4 defcon18

Reputation: 1
• Posts: 28
• Joined: 06-June 11

## Re: Finding the difference between largest and smallest array

Posted 19 June 2011 - 01:29 PM

I've found one of the problems I typed a < instead of a > for minimum. Here's a example of what I'm trying to do, arr[7]=10, arr[1]=4. Array 7 is the biggest array while array 1 is the smallest so you subtract 7-1=6. Now that I fixed the minimum issue I still am not getting the right final value.

### #5 CTphpnwb

• D.I.C Lover

Reputation: 3746
• Posts: 13,620
• Joined: 08-August 08

## Re: Finding the difference between largest and smallest array

Posted 19 June 2011 - 01:48 PM

Proper indenting will help make your code more readable, and therefore easier to debug. Use of functions is always a good idea too.

```void max_min_difference(int arr[], int arr_size) {
int minimum, maximum;
minimum = arr[0];
maximum = arr[0];
for (int N = 1; N < arr_size; N++) {
if (arr[N] < minimum) {
minimum = arr[N];
}
if (arr[N] > maximum) {
maximum = arr[N];
}
}
cout << "Difference = "<< (maximum - minimum) << endl;
}
```

### #6 snoopy11

• Engineering ● Software

Reputation: 1438
• Posts: 4,629
• Joined: 20-March 10

## Re: Finding the difference between largest and smallest array

Posted 19 June 2011 - 01:53 PM

I think you will need four loops in total for this

The first should be something like this to find
the largest number in the array

```int max= x[0];

for (int c=0; c<=num; c++)
{

if (x[c]>max)
{
max=x[c];
}
}

```

then you need to find the index of the largest number.
```int c =0;
while (max!=x[c])
{
c++;
}

```

do the same but use a different int say z for arugments sake
and use the < less than symbol this time to find min.

```final= (c-z);