5 Replies - 1691 Views - Last Post: 19 June 2011 - 01:53 PM Rate Topic: -----

#1 defcon18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • 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;
Was This Post Helpful? 0
  • +
  • -

#4 defcon18  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • 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;
}

Was This Post Helpful? 0
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 710
  • View blog
  • Posts: 2,033
  • 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.

finally subtract your two indexes.

final= (c-z);
return final;


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

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1