Question

Write program(Only in C, no C++) as follow. With comment

Algorithm 1 MaxSubSeqDivideNConquer(S[..r] Input: A sequence, S of r - 1 integers Output: The sum, start and end of a maximum

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program in c/c++:

#include <stdio.h>
#include <limits.h>

// to get max from two varibale
int ToFindMaximum(int a, int b) { return (a > b)? a : b; }

// to get max from three varibale
int ToFindMaximum(int a, int b, int c) { return ToFindMaximum(ToFindMaximum(a, b), c); }

// to get max from araay
int ToFindMaximumCrossingSum(int arr[], int l, int m, int h)
{
// Include elements on left of mid.
int sum = 0;
int Sum_Of_Left= INT_MIN;
for (int i = m; i >= l; i--)
{
sum = sum + arr[i];
if (sum > Sum_Of_Left)
Sum_Of_Left= sum;
}

// Include elements on right of mid
sum = 0;
int Sum_Of_Right = INT_MIN;
for (int i = m+1; i <= h; i++)
{
sum = sum + arr[i];
if (sum > Sum_Of_Right)
Sum_Of_Right = sum;
}

// Return sum of elements on left and right of mid
return Sum_Of_Left+ Sum_Of_Right;
}

// Returns sum of Maximum sum subarray in aa[l..h]
int ToFindMaximumSubArraySum(int arr[], int low, int high)
{

if (low == high)
return arr[low];

int middle = (low + high)/2;
//divide aray
return ToFindMaximum(ToFindMaximumSubArraySum(arr, low, middle),
ToFindMaximumSubArraySum(arr, middle+1, high),
ToFindMaximumCrossingSum(arr, low, middle, high));
}

//drive class
int main()
{
int arraryint[] = {5, 9, 4, 5, 7};
int n = sizeof(arraryint)/sizeof(arraryint[0]);
int Maximum_sum = ToFindMaximumSubArraySum(arraryint, 0, n-1);
printf("maximum contiguous subsequence sum is %d ", Maximum_sum);
getchar();
return 0;
}

output

maximum contiguous subsequence sum is 30

Add a comment
Know the answer?
Add Answer to:
Write program(Only in C, no C++) as follow. With comment Algorithm 1 MaxSubSeqDivideNConquer(S[..r] Input: A sequence,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is...

    Divide and Conquer & Algorithm Design 5. (20 points) Consider the following algorithm Precondition: S is a sorted list index mystery (index low, index high, const Array S[], number x) if low S high then mid = (low + high) / 2 if x = Smid] then return mid elsif x < s[mid] then return mystery (low, mid-1, s, x) else return mystery (mid+1, high, s, x) else return 0 end What does the recursive algorithm above compute? Explain why?

  • Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array...

    Consider the following: Algorithm 1 Smallest (A,q,r) Precondition: A[ q, ... , r] is an array of integers q ≤ r and q,r ∈ N. Postcondition: Returns the smallest element of A[q, ... , r]. 1: function Smallest (A , q , r) 2: if q = r then 3: return A[q] 4: else 5: mid <--- [q+r/2] 6: return min (Smallest(A, q, mid), Smallest (A, mid + 1, r)) 7: end if 8: end function (a) Write a recurrence...

  • urgent L. Consider the following pseudocode for finding binomial coefficients: Binom(n, r) Input: integers n and r Output: n choose r if r-0 or r-n thern return 1 end else return Binom(n-1, r-1) B...

    urgent L. Consider the following pseudocode for finding binomial coefficients: Binom(n, r) Input: integers n and r Output: n choose r if r-0 or r-n thern return 1 end else return Binom(n-1, r-1) Binom(n-1, r); end running time of this algorithm. Prove your bound for the upper bound. (5 points) Rewrite the above algorithm so that it is efficient. (You have 2 choices!) Analyze the worst case time of your new algorithm. (5 points) Find the edit distance between "SPOKE...

  • 8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za...

    8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...

  • This is an assignment for my algorithm class which I have written the code partially and...

    This is an assignment for my algorithm class which I have written the code partially and only need to complete it by adding a time function that calculates the average running time. We could use any programming language we want so I am using C++. I am including the instruction and my partial code below. Thank you! Implement linearSearch(a,key) and binarySearch( a,key)functions. Part A.In this part we will calculate theaverage-case running time of each function.1.Request the user to enter a...

  • Write a program that takes as input a positive real number s and a positive real...

    Write a program that takes as input a positive real number s and a positive real number E and outputs the approximate value of Vs to within the tolerance E. In other words, the output of your program should be a number s' such that Is SIS E. (Thus the distance" between the output s' and the correct answer Vs should be at most E.) Your program should implement a very old algorithm for approximating the square root of numbers,...

  • Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231...

    Please solve using Java and comment your code for clarity. Sample 3. Input: 4 1 231 Output: 0 This sequence also does not have a majority element (note that the element 1 appears twice and hence is not a majority element). Problem Introduction Majority rule is a decision rule that selects the alternative which has a majority, that is, more than half the votes. Given a sequence of elements (1.02....,On, you would like to check whether it contains an element...

  • Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative perfo...

    Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...

  • C++ PROGRAM ONLY! For this lab you need to write a program that will read in...

    C++ PROGRAM ONLY! For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using a recursive implementation of the Euclidean algorithm) to a file. In Lab #3, you implemented this program using an iterative method. Greatest Common Divisor In mathematics, the greatest common divisor (GCD) of two or more integers (when at least one of of them is zero then the larger value is the GCD....

  • Need to code the HeapSort Algorithm in java. Most of the code is already done just...

    Need to code the HeapSort Algorithm in java. Most of the code is already done just need to code the "heapify" part of the heapsort algorithm. My heapify code is in bold below called "sink", but when I run it it gives me 2 errors with the string compare part saying: WordCountHeap.java:61: error: cannot find symbol         if(l < k && String.Compare(pq[l], pq[n]) < 0)                           ^ symbol:   method Compare(String,String) location: class String Please fix the code and the "heapify"...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT