Program Unit Testing

Source: Internet
Author: User

Topic finding the maximum value in an array

Idea: First design a function to find the largest value in the array, and then call it by the main function.

Code implementation

#include <iostream>
using namespace Std;

int largest (int a[],int length)
{
int i,max=a[0];
if (A==null | | length==0)
{
return 0;
}
for (i=1;i<length;i++)
{
if (A[i]>max)
{
Max=a[i];
}
}
return Max;
}
void Main ()
{
int j,n,a[1000];
cout<< "Please enter array length:";
cin>> N;
while (n<=0 | | n>1000)
{
cout<< "Input error, please re-enter:";
cin>>n;
}
cout<< "Please input array:";
for (j=0;j<n;j++)
{
cin>>a[j];
}
cout<< "Maximum Value" <<largest (a,n) <<endl;
}

Test Cases

(1) Array length: 1 array value: 5 Test no bug;

(2) Array length: 5 array Value: 1 2 3 5 8 test without bugs;

(3) Array length: 6 array Value: 2 2 9 8 10 10 Test no bug;

Summary: In programming, you need to take into account that the computer can not find the array is empty or the array length of 0 and other cases of the maximum value, it needs to be coupled with conditional judgment, in case of error. In the test, you need to take into account all the circumstances, such as only one array value, there are two identical maximum array values and so on, in the continuous testing to strengthen the program, so that the program can handle a variety of situations, enhance the robustness of the program.

Program Unit Testing

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.