Program code:
Li Qi #include<iostream>using namespacestd;intLargest (intList[],intlength) { intI, Max; Max= list[0]; for(i =0; i < length; i++) { if(list[i]>max) {Max=List[i]; } } returnMax;}voidMain () {intlist[5],length; inti; CIN>>length; for(i =0; i < length; i++) {cin>>List[i]; } cout<<largest (list, length);}
2. Error finding
The max variable is initialized, or the computer is assigned a value based on the previous storage, which may result in an error.
I<length, if it is i< (length-1), then the last number will not be found.
Operation Result:
All are positive:
All are negative:
There is a positive negative:
If the array length is 0.
5. Solution
If the user enters an array length of 0, it returns 0.
6. Workaround:
Li Qi # include<iostream>using namespacestd;intLargest (intList[],intlength) { intI, Max; Max= list[0]; if(Length = =0|| List = =NULL) { return 0; } for(i =0; i < length; i++) { if(list[i]>max) {Max=List[i]; } } returnMax;}voidMain () {intlist[5],length; inti; CIN>>length; for(i =0; i < length; i++) {cin>>List[i]; } cout<<largest (list, length);}
Summary: Many errors in testing are often committed, to write a good program, need to constantly test and modify the ability to exercise their code codes.
Classroom Practice Unit Test