stanley cho
stanley cho

Reputation: 153

how to return indexes that makes best sum in an array?

I just need to input an array and make the program return the range of index which produces highest sum.

Examples

For A = {5, -6, 7, -5, 10, -1} – the best sum consists of the values in A[2..4] for a total of 7 + -5 + 10 = 12

For A = {1, 2, 4, -6, 4, 2, 1} – the best sum consists of the values in A[0..6] (the entire array) for a total of 8

For A = {-5, 2, -3, 1, -5, 4, -2} – the best sum consists of the value in A[5] for a total of 4

It would be greatly appreciated if someone could help me return the following values

Here is my code(doesn't seem to run properly)

#include<stdio.h>

int the_resource_collection( int arr[], int len){
  int highest, start, end, i, result;
  highest = arr[len-1];// the limits of the highest value
  start = len - 1;
  end =  len - 1;// all the variables in the total indexes
  for( i=0; i<len-2; i++)
  {
    for (a=i; a<len-1; a++)
    {
        result= arr[i]+ arr[a];// try to find the total of the index
        {
            if (result > highest)
            {
                highest = result;
                start = i;
                end = a;
            }
        }
    }
  }
  return start;
  return end;
}
int main()
{
  int a[6] = {5, -6, 7, -5, 10, -1};
  int length = 6;
  printf("%d",the_resource_collection(&a[6],length));
}

Upvotes: 0

Views: 129

Answers (1)

H. Figueiredo
H. Figueiredo

Reputation: 928

Ok, you have some mistakes.

Firstly, in printf("%d",the_resource_collection(&a[6],length)); you are only passing to the function the sixth element of the array.

Also your for loops aren't considering all elements of the array. Finally you also need to save the begining and ending in a int array.

Here is how I would do it (I've tested it with the 3 inputs you've provided and the output is as expected!):

#include<stdio.h>
int * the_resource_collection( int arr[], int len){
    int highest;
    int returnvalue[2];
    int i;
    int a;
    int result;
    highest = arr[len-1];// the limits of the highest value
    returnvalue[0] = len - 1;
    returnvalue[1] = len - 1;// all the variables in the total indexes
    for( i=0; i<len; i++)
    {
        result= arr[i];
        for (a=i+1; a<len; a++)
        {
                if (result > highest)
                {
                    highest = result;
                    returnvalue[0] = i;
                    returnvalue[1] = a-1;
                }
                result+=arr[a];
        }
        if (result > highest)
        {
            highest = result;
            returnvalue[0] = i;
            returnvalue[1] = a-1;
        }
        result=0;
    }
    return returnvalue;
}
int main()
{
    int a[7] = {5, -6, 7, -5, 10, -1};
    int length = 7;
    printf("A[%d...%d]\n",the_resource_collection(a,length)[0],the_resource_collection(a,length)[1]);
}

If you don't understand what I wrote, or need anymore help, please say so.

Upvotes: 1

Related Questions