"Array max sum 2" Analysis report
First, design ideas
1. Define an array with variable number of array elements, with macro definition implementation (A[n]).
2. Use the RAND function to generate positive and negative random numbers in the array.
3. Define a two-dimensional array (B[n][n]) to store the values of each sub-array in turn.
4. Find the largest value in the two-dimensional array, which is the value of the largest sub-array.
5. The subscript of the largest value in a two-D array is the starting subscript and ending subscript for the largest subarray, such as B[2][4], which is the largest value in a two-dimensional array, that is, the subscript of the largest subarray is 2, 3, 4.
II. Development process (Knot Group development)
I am mainly responsible for program analysis, code programming, the previous program can handle the number of elements up to 500, 1000 is half the difference, if you press 1000 run the program will error. My main analysis of the two aspects:
1 The program result exceeds the range of int so error
2. Relationship to the number of elements in a two-dimensional array
For the first aspect, I let the random number be produced in a fixed small range, and the result is an error, so it's not the first question. For the second question, I replaced the two-dimensional array with a one-dimensional array, and let the one-dimensional array store each sub-array sequentially and then run successfully. So there's a problem with a two-dimensional array.
Third, the program code
#include <iostream>#include<time.h>#defineN 127293using namespacestd;voidMain () {inta[n],i,w=0, B[n]; Srand ((int) Time (0)); for(i=0; i<n;i++) A[i]=-rand ()% $+ -; for(i=0; i<n;i++) cout<<a[i]<<Endl; for(i=0; i<n;i++) {W+=A[i]; B[i]=W; } intt=b[0]; for(i=0; i<n;i++) { if(b[i]>t) T=B[i]; } cout<<"the maximum number of sub-arrays is:"<<t<<Endl; }
Iv. Results
V. Summary
when the problem of the program must be well analyzed, there may be something wrong, a list of possible problems appear. Then for each of the problems of analysis, analysis if this is really a problem, how to determine and determine how to resolve. This is the first knot to do the procedure, two people think really more than one wants to be comprehensive. A clear division of labor is also very important, a person responsible for the preparation of the program, a person responsible for code testing. In the future process of writing procedures, will pay attention to two people's communication and cooperation.
"Array max sum 2" Analysis report