Software Engineering class Exercises

Source: Internet
Author: User

According to the requirements, this job pair programming completed. Think and analyze the problem with teammates and finish it. Shanling County is responsible for completing the implementation of the Code,

I mainly responsible for the programming process of some small problems in the judgment and analysis.

Title: Returns the maximum number of sub-arrays in an integer array and

Requirements:

Enter a one-dimensional shape array with positive and negative numbers in the array.

One-dimensional arrays end-to-end, like one end-to-end tape.

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.

Design Ideas :

The user customizes the array length and enters the array elements sequentially, and sets a global variable to be initialized to the zero array a[n],n=10000;

1. Because the array is visible as a loop, we need to break in a suitable position and show the elements of the array as a straight strap.

(1). Set user custom array length to M, input array element value a[1]~a[m], add code to the for Loop A[m+i]=a[i];(that is, copy the input array after the last number of the array, where I is the loop variable)

Example: If the input array is 1,-2,3,-4,5 then the actual generated array is 1,-2,3,-4,5,1,-2,3,-4,5

(2). Find the appropriate position to break, this suitable position is the minimum value of the entire array, write a function to find the minimum value of the input array to make the number first.

Example: The input array is 1,-2,3,-4,5 so find the minimum element-4 position and make it in the first place, so the actual generated and computed array is (underlined part): 1,-2,3,-4,5,1,-2,3,-4,5

2. After the correct array is disconnected at the appropriate location, the first class assignment (irrespective of the end-to-end condition) is calculated

(1). From the first element of the array is not zero accumulation, set the cumulative value of S, in order to determine the first element of the array is not negative, s needs to be initialized to zero;

(2). Define another shaping variable sum stores the current maximum accumulated value s,sum to be compared with the accumulated value, so sum should be initialized to 0 (1) each time the summation to obtain a s after the judgment, if s is negative, let go of the accumulated value is negative array elements, continue to start from the next array element accumulation (2) if sum< s, assigning S to Sum,s continues to accumulate (3) so loop until the array element is exhausted.

(3). The sum of the last output is the maximum value of the sub-array.

problems that arise :

The so-called "fit" position identified by the first code is the element that accumulates to a number that causes the accumulated value to be negative, and the first bit.

However, if no value is accumulated and the accumulated value is less than 0, the result is an error.

Example: 1,2,-2,4,5 cumulative to 2 so that the previous three elements of the cumulative value of 1 is nonzero, so the calculated result is 9, and should be the result of 12

Program source Code

#include <iostream>using namespacestd;inta[10000];//Global variables (initialize array a all elements are 0)intFind (intMintA[])//find the subscript position C of the smallest value in the input array and return{    intMin,c=0I//c is initialized to zero because if the first element of the input array is the minimum value, an error C is not initializedmin=a[0];  for(i=0; i<m;i++)    {        if(a[i]<min) {min=A[i]; C=i; }    }    returnC;}intSum (intMintA[])//The value of the largest sub-array is calculated by looping the element labeled C in the array .{    inti; ints=0; intsum=0;  for(I=find (M,a); I<m+find (m,a); i++)    {        if(s<0) s=a[i];//Rounding up the Subarray and the array elements less than 0        Elses=s+a[i];//temporarily holds the accumulated value of an array element        if(sum<s) sum=s;//holds the and of the current largest sub-array    }    returnsum;}voidInputanddisplay ()//enter an array element and output the maximum value of the sub-array (an array of end-to-end loops){    inti,m; cout<<"Please enter the length of the array: \ n";//Custom Array LengthCin>>m;  for(i=0; i<m;i++) {cout<<"Please enter section"<<i+1<<"Number:"; CIN>>A[i]; A[i+m]=a[i];//copy the input array after the last number of the array} cout<<"\ n The maximum value of the Subarray and the array is:"<<sum (m,a) <<"\ n";}voidMain () {Inputanddisplay ();}

Results:

Software Engineering class Exercises

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.