The development of software engineering pair The sum of 2 consecutive maximal sub-arrays in a one-dimensional array

Source: Internet
Author: User

First, the topic requirements

Title: Returns the and of the largest sub-array in an integer array. Requirements: The program must be able to handle 1000 elements, each of which is of type int32, an array with positive numbers and negative numbers, and one or more consecutive integers in the array that make up a subarray, each of which has one sum, and the maximum value of all the sub-arrays. Requires a time complexity of O (n) and can be judged when the result overflows. Second, the design idea in the maximum continuous sub-array is also used in the dynamic method, for the new requirements, you can increase the number of random generation multiples, if the result overflow output overflow hint typeface, where the int type rand () randomly generated number range is 0~32767,int32 maximum number is 2147360000. Third, the Code
#include <iostream.h>#include<stdlib.h>#include<time.h>voidMain () { while(1)    {        intlength,temp; int*arr=New int[length]; cout<<"Please enter the length of the array:"; CIN>>length; //cout<< "Please enter this array:";Srand ((unsigned) time (NULL));  for(intI=0; i<length;i++) {Arr[i]=rand () *10000; Temp=rand ()%2; if(temp==0) {Arr[i]=Arr[i]; }            Else{Arr[i]=-Arr[i]; } cout<<arr[i]<<"\ t"; }        intresult = arr[0]; intsum = arr[0];  for(i=1; i<length;i++)        {            if(Sum >0) {sum+=Arr[i]; }            Else{sum=Arr[i]; }            if(Sum >result) Result=sum; }        if(result<2147360000|| result==2147360000) {cout<<"the sum of the largest contiguous subarray in the array is:"<<result<<Endl; }        Else{cout<<"overflow beyond the maximum range!"<<Endl; } cout<<"----------------------------------------"<<Endl; }}

Iv. Testing

No overflow when input array length is 1000

Overflow occurs when input array length is 2000

V. Summary

Previously did not consider the memory overflow situation, now consider overflow to increase the scope, overflow is a common problem, in the future need to pay attention to this situation, for overflow can not solve only hint overflow, in the production of random number directly multiplied by 10000 resulting in the number of four bits are 0000, there is a defect to be further improved.

Six, work photo

The development of software engineering pair The sum of 2 consecutive maximal sub-arrays in a one-dimensional array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.