July 22, March 21: Small Test (Week 4), Week 4 on the 21st

Source: Internet
Author: User

July 22, March 21: Small Test (Week 4), Week 4 on the 21st

Problem: function for generating the maximum value in unit test

Design Concept: Use the maximum function to generate the maximum value, and then define the function to bring the maximum value into the check to check whether it is the maximum value. Specifically, compare it with the array element in sequence to be greater than or equal to all elements. For inspection.

Program source code:

1 # include <iostream> 2 # include <vector> 3 using namespace std; 4 5 int Largest (vector <int> & list) 6 {7 int max = list [0]; 8 if (list. size () <5) 9 {10 cout <"the number of input digits is less than 5 or the array is empty"; 11} 12 else13 {14 for (int I = 0; I <5; I ++) 15 {16 if (list [I]> max) 17 {18 max = list [I]; 19} 20} 21} 22 return max; 23} 24 void Ceshi (vector <int> & list) 25 {26 cout <Largest (list) <endl; 27 for (int j = 0; j <5; j ++) 28 {29 if (list [j]> Largest (list) 30 Cout <"error! "; 31} 32} 33 void main () 34 {35 vector <int> list (5); 36 Ceshi (list); 37 list [0] = 1; list [1] = 2; list [2] = 3; list [3] = 4; list [4] = 5; 38 Ceshi (list ); 39 list [0] =-1; list [1] =-2; list [2] =-3; list [3] =-4; list [4] =-5; 40 Ceshi (list); 41 list [0] = 2; list [1] = 2; list [2] = 2; list [3] = 2; list [4] = 2; 42 Ceshi (list); 43 list [0] = 1; list [1] = 2; list [2] = 4; list [3] = 3; list [4] = 5; 44 Ceshi (list); 45 list [0] = 5; list [1] = 4; list [2] = 3; list [3] = 2; list [4] = 1; 46 Ceshi (list); 47}

Result:

Result Error Analysis: c ++ still cannot solve the null array problem. In this case, an empty array cannot be defined for the result solved by vector.

Related Article

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.