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