The number of the largest sub-arrays in the array (1000)

Source: Internet
Author: User

Members: Lin Yanlu, Zhang

Topic:

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

Requirements:

Requires that the procedure be capable of handling 1000 elements;

Each element is of the int32 type;

Enter an array of shapes with positive and negative numbers in the array.

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.

(I am mainly responsible for program analysis, code programming; Zhang is responsible for code review, Code test plan.) )

Ideas:

By first seeking a single-dimensional array of the largest subarray of the and, on this basis we have expanded. Earlier in the program we fixed the length of the array, the default is 5, and this time, according to the topic to expand to 1000, the definition of the value type int32. Let the program randomly produce 1000 numbers, and use the same method to find the maximum value of the subarray.

Just read the topic feel not difficult, so the change is not much place, through a few tests down but found a lot of problems (in the Running Results section has analysis instructions).

Source:

#include <iostream.h> #include <stdlib.h> #define AMOUNT 1000int Main () {long int arr[amount];    long int arrcopy[amount];    long int max[amount];    int i,j;    int n;    int start,rear;//The beginning of the subarray, ending int mount=amount;            for (i=0;i<mount;i++) {try{n=rand ()%2;    if (n==0) {Arr[i]=rand (); arrcopy[i]=arr[i];max[i]=arr[i];    } else {Arr[i]=-rand (); arrcopy[i]=arr[i];max[i]=arr[i]; }}catch (long int e) {cout<< "Long Inter=ger exception!"    <<endl;}    } cout<< "Array:" <<endl;    for (i=0;i<mount;i++) {cout<<arr[i]<< ""; if ((i+1)%10==0) {Cout<<endl;}} for (j=0;j<mount-1;j++) {for (i=j+1;i<mount;i++) {try {arr[j]=arr[j]+arr[i];if (Max[j]    <arr[j]) {max[j]=arr[j];            rear=i+1;} } catch (long int e) {cout<< "Long Inter=ger exception!"    <<endl; }}} for (i=0;i<mount;i++) {if (Max[0]<maX[i]) {max[0]=max[i];    start=i+1;}     } cout<<endl;    cout<< "from" <<start<< "number" <<arrCopy[start-1]<< "Start" <<endl;    cout<< "to" <<rear<< "number" <<arrCopy[rear-1]<< "End" <<endl;    cout<< "sub-arrays and the maximum values are:" <<max[0]<<endl;                     return 0;}

Operation Result:

(1) run several times (because 1000 number shows 100 lines, so only to intercept part of the display results), it can be seen that the randomly generated 1000 numbers are all positive, no negative numbers.

(2) Modify the code, by randomly generating 0 and 1, to control the generation of positive and negative numbers, to avoid only positive or negative occurrences.

The problem is found after running: because there are 1000 numbers and the values are large, we do not directly see whether the results are correct.

(3) Perfect The program, the sub-array location to find out, and then check.

The range of values found is still large, and the resulting range of randomly generated and final additions is beyond the range of the Int32 data type (that is, if the value is out of bounds), while the C + + itself is not error-aware during debugging, and is caught with exception handling:

Try

{

...

}

catch (long int e)

{

cout<< "Long Inter=ger exception!" <<endl;

}

Summarize:

The length of the array we did in the previous test is generally small, and the assignment of each element is not more than two digits, or even fixed, is to facilitate directly find a and is the largest sub-array, which can not test the program where there is insufficient, both the number and the size of the value. We found the problem by randomly generating 1000 of data. Because the concept of time complexity has been completely forgotten, the program is not considered.

In the commissioning of my harvest is very large, the above are adjusted a lot of times to change a defect, because there are many data, loops and nesting is more complex, start and rear two parameters in the beginning to add a frequent error, the number is changed to small, after the use of single-step debugging in the end to see the error in which step, And with exception handling to indicate whether we have the value out of bounds and other software debugging without considering the defects.

The knot on the development of the first cooperation is not good, because the change is not very small, so the discussion is still relatively small, personal subjective views are more, later or more exchange it.

The number of the largest sub-arrays in the array (1000)

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.