3. Software engineering knot pair development The sum of successive maximal sub-arrays in a one-dimensional array and the determination of overflow

Source: Internet
Author: User

First, the topic requirements

Title: Returns the and of the largest sub-array in an integer array.

Requirement: The procedure must be capable of handling 1000 elements;

Each element is of the int32 type;

Enter an array of shapes, with positive numbers in the array and negative values;

One or more consecutive integers in an array make up a sub-array, each of which has a and;

The maximum value for the and of all sub-arrays. Requires a time complexity of O (n);

Results can be judged after overflow.

Second, the design idea

In our previous program, because the int type rand () randomly produces a number range of 0~32767, the resulting 1000 number is smaller and does not produce an overflow. However, we find that the maximum number of Int32 is 2147360000 or 2^31, if the result overflow will prompt overflow.

Third, the source code

1#include <iostream.h>2#include <stdlib.h>3#include <time.h>4 voidMain ()5 {6      while(1)7     {8         intlength,temp;9         int*arr=New int[length];Tencout<<"Please enter the length of the array:"; OneCin>>length; A         //cout<< "Please enter this array:"; - Srand ((unsigned) time (NULL)); -          for(intI=0; i<length;i++) the         { -Arr[i]=rand () *10000; -Temp=rand ()%2; -             if(temp==0) +             { -arr[i]=Arr[i]; +             } A             Else at             { -arr[i]=-Arr[i]; -             } -cout<<arr[i]<<"\ t"; -         } -         intresult = arr[0]; in         intsum = arr[0]; -          for(i=1; i<length;i++) to         { +             if(Sum >0) -             { theSum + =Arr[i]; *             } $             ElsePanax Notoginseng             { -sum =Arr[i]; the             } +             if(Sum >result) Aresult =sum; the         } +         if(result<2147360000|| result==2147360000) -         { $cout<<"the sum of the largest contiguous subarray in the array is:"<<result<<Endl; $         } -         Else -         { thecout<<"overflow beyond the maximum range!"<<Endl; -         }Wuyicout<<"----------------------------------------"<<Endl; the     } -}

Iv. Results

Five, the Experiment experience

We used to do experiments, always think too naïve, take it for granted that the user input must be entered according to our requirements, but the reality is not so, for the large number of overflow problem we are ill-conceived, there are other details of the problem we have many did not consider. When the input array length is large, it is likely to overflow, in the future to pay more attention!

VI. Cooperation Photos

3. Software engineering knot pair development The sum of successive maximal sub-arrays in a one-dimensional array and the determination of overflow

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.