First, team members:
Tan Wei, Chen Zhili
Second, the project name:
Sum of successive maximal sub-arrays in an array
Third, our design ideas:
Set Sum[i] is the contiguous subarray with the end of the first element and the largest. For element I, all the lengths of the sub-arrays ending with the elements in front of it have been evaluated, then the contiguous subarray ending with the element I and their sum largest is either the i-1 element ending with the sum of the elements, the contiguous subarray with the largest of them, or only the element I, i.e. sum[i] = max ( SUM[I-1] + arr[i], arr[i]). The choice can be made by judging whether Sum[i-1] + arr[i] is greater than arr[i], which is actually equivalent to judging whether sum[i-1] is greater than 0. So I and Chen Zhili follow this idea programming as follows:
Four, the code:
#include <iostream.h>void main () {while (1) {int length;int *arr=new int[length];cout<< "Please enter this array length:";cin> >length;cout<< "Please enter the array:"; for (int i=0;i<length;i++) {cin>>arr[i];} int result = Arr[0];int sum = arr[0];for (i=1;i<length;i++) {if (sum > 0) {sum + = Arr[i];} Else{sum =arr[i];} if (sum >result) result = sum;} cout<< "The sum of the largest contiguous subarray in the array is:" <<result<<endl;cout<< "----------------------------------------" <<endl;}}
V. Operation and Testing:
Tested when all negative, full positive, all is 0, there is a negative and a variety of special cases, etc., are running correctly.
Vi. Summary:
Although this is not the first time to do the project, but the process of obvious feeling to solve the problem more thoughtful than a person to consider the overall, efficiency, improve a lot of mutual learning and cooperation more smoothly.
Seven, work photo
The development of software engineering pair The sum of successive maximal sub-arrays in an array