c - Print Max Subarray Values From An Array After Running Divide And Conquer Algorithm -


i have implemented solution find maximum subarray array of values. can print out full array prior running divide , conquer algorithm, cannot seem figure out how print subarray after algorithm run.

int newarray[] = {31, -41, 59, 26, -53, 58, 97, -93, -23, 84};  int arraysize = (sizeof(newarray)/sizeof(int));  printarray(newarray, 0, arraysize - 1);  int total = maxsubdiv(newarray, 0, arraysize - 1); 

this snippet of main function. using printarray function print full array prior finding maximum subarray. maxsubdiv function follows:

int maxsubdiv(int * array1, int left, int right) {     if(left == right)     {         return array1[1];     }      int middle = (left + right)/2;      return findmax(maxsubdiv(array1, left, middle), maxsubdiv(array1, middle + 1, right), leftrightcross(array1, left, middle, right));  }  int leftrightcross(int * array1, int left, int middle, int right) {     int sum = 0;      int leftsum = int_min;      for(int = middle; >= left; i--)     {         sum = sum + array1[i];         if(sum > leftsum)         {             leftsum = sum;         }     }      sum = 0;     int rightsum = int_min;      for(int = middle + 1; <= right; i++)     {         sum = sum + array1[i];         if(sum > rightsum)         {             rightsum = sum;         }     }      sum = leftsum + rightsum;      return sum; } 

the algorithm seems test well, having trouble printing out subarray contains integers of max subarray. appreciated!

struct tuple{      int begin;     int end;     int length;  };  int findmax(int left, int right, int cross) {      int max;     if(left > right && left > cross)     {         max = left;     }      else if(right > left && right > cross)     {         max = right;     }      else     {         max = cross;     }      return(left, right, cross); } 

in maxsubdiv() when compare max of 3 subarrays, return tuple (begin, end, length) of max subarray instead of length. "begin", "end" specifies range of max subarray, can leverage later print. should return (begin, end, length) leftrightcross().

e.g.,

// pesudocode if max left:    return (left_begin, left_end, left_length) if max right:    return (right_begin, right_end, right_length) if max middle:    return (middle_begin, middle_end, middle_length) 

the tuple can implemented in struct in c.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -