#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"); }

## 5 Replies - 5256 Views - Last Post: 19 June 2011 - 01:53 PM

### #1

# 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.

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

### #2

## 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.

*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

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

Posted 19 June 2011 - 01:21 PM

your missing a brace after return final;

### #4

## 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

## 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

## 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

then you need to find the index of the largest number.

do the same but use a different int say z for arugments sake

and use the < less than symbol this time to find min.

finally subtract your two indexes.

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.

finally subtract your two indexes.

final= (c-z); return final;

This post has been edited by **snoopy11**: 19 June 2011 - 01:54 PM

Page 1 of 1