1. Title:
Returns the and of the largest sub-array in an integer array.
2. Requirements:
1) requires that the procedure be capable of handling 1000 elements;
2) Each element is of type Int32.
3. Design ideas:
When working with 1000 elements, let the user enter the desired array length themselves, see how long the runtime is processing, and then improve the problem in the source program.
When dealing with a large number overflow, the number of random functions generated is not too large, we try to let its final deposit into the array of elements close to the maximum range, and then the sum of the likelihood of a large number overflow situation.
4. Source code:
1 #include <iostream> 2 #include <stdlib.h> 3 using namespace std; 4 #include <ctime> 5 #include <cstdlib> 6 #define MAX 100000 7 int main () 8 {9 int k,t=0,o=0;10 int n =0,m=0;11 int b[max];12 int a[max];13 cout<< "Please enter the number of integers in the array:" <<endl;14 cin>>k;15 Srand ( Time (0)); i=0;i<k;i++ (int), A[i]=rand ()%100-50;19 cout<<a[i]<< ""; 20 Cin>>a[i];21}22 for (int l=1;l<k+1;l++) (i=0;i<k-l+1;i++) 26 {j=i;j<i+l;j++ (int), {j>k-1) 30 {31 break;32}33 else34 m=m+a[j];35}36 b[n]=m;37 m=0;38 n=n+1;39}40}41 int max=b[0];42 for (i=1;i<n;i++) 43 {4 4 if (Max<b[i]) max=b[i];46}47/* foR (i=0;i<n;i++), {max==b[i], {m=i;52}53}54 m=m+1;55 in T r=k;56 for (int j=0;j<k;j++), {(m>=r&&r>=0) t=t+1;62 r=r-1;63}64 if (m==0&&r>0&&j!=k-1) 65 {66 m=m+r+1;67 break;68}69 if (m==0&&r==0&&t==k) m=m +1;72 break;73}74}*/75 cout<<endl;76 cout<< "The number of the largest sub-arrays in the array is:" <<MAX<&L t;endl;77/*cout<< "These numbers are:"; m=m-1;79 if (t==0), {Bayi t=1;82}83 for (i=m;i<m+t;i+ +) * * cout<<a[i]<< "",}87 for (i=0;i<n;i++) cout<<b[i]<< " "; */89 return 0; 90}
5. Results:
6. Experience:
The focus is on large number overflow resolution, the first time we achieve a large number overflow when the program unexpectedly closed, even if it can run without prompting the error. The solution to this point, we think of the array elements randomly generated when the re-generation, you can also judge the results, and actively suggest errors to solve. But we do not really solve the large number of overflow has no specific solution, this is our shortcomings, we must pay more attention to this point in the future.
Array length requirements and large number overflow for pair programming