Click here to Skip to main content
15,798,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
a) Write a function that will find the largest contiguous partial sum, LagestSum, within an array of real numbers. That is,
we want to sum up any number of neighboring elements from the array, starting from anywhere, find the largest possible
sum.
-3 4 2 1 -4 6 -10 0 -4 3
then the function should report return 9, because 4+2+1+(-4)+6= 9 is the largest sum of contiguous elements from
that array of numbers. The function must also provide the starting and the ending indices of the summed elements back
to its caller(indices 1 and 5 for the example above).

(b) Write a driver program that tests the function LargesttSum written in (a)

What I have tried:

C++
#include<stdio.h>
#include<stdlib.h>

int max (int num1, int num2)

{
	return (num1>num2)? num1:num2;
}
int maxSubArraySum(int a[], int size)
{
	int max_so_far=a[0];
    int curr_max=a[0];

    for(int i=1;i<size;i++)
{
 curr_max="max(a[i],curr_max+a[i]);
" max_so_far="max(max_so_far,curr_max);
}
return" max_so_far;
}

int="" main()
{
="" int="" a="" []="{-3,4,2,1,-4,6,-10,0,-4,3};
" n="sizeof(a)/sizeof(a[0]);
" max_sum="maxSubArraySum(a,n);
" printf("largest="" contiguous="" sum="" of="" elements="" from="" that="" array="" numbers="" is="" %d",max_sum);

return="" 0;
}
Posted
Updated 17-Jun-21 12:24pm
v4
Comments
Patrice T 17-Jun-21 15:19pm    
Part of source code is missing.
[no name] 17-Jun-21 15:29pm    
try it please , abit confusing
Patrice T 17-Jun-21 15:41pm    
Use Improve question to update your question.

A space after '<' may help with source code.
jeron1 17-Jun-21 15:46pm    
Is there a specific question or problem?
Patrice T 17-Jun-21 17:32pm    
What is the question ?

#include<stdio.h>
#include<stdlib.h>

int max (int num1, int num2)

{
return (num1>num2)? num1:num2;
}
int maxSubArraySum(int a[], int size)
{
int max_so_far=a[0];
int curr_max=a[0];

for(int i=1;i<size;i++)
{
curr_max="max(a[i],curr_max+a[i]);
" max_so_far="max(max_so_far,curr_max);
}
return" max_so_far;
}

int="" main()
{
="" int="" a="" []="{-3,4,2,1,-4,6,-10,0,-4,3};
" n="sizeof(a)/sizeof(a[0]);
" max_sum="maxSubArraySum(a,n);
" printf("largest="" contiguous="" sum="" of="" elements="" from="" that="" array="" numbers="" is="" %d",max_sum);

return="" 0;
}<pre="" lang="text">


check peter make a new code bro
 
Share this answer
 
Quote:
check peter make a new code bro
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900