/***************************************************** Knowledge Points: bool type, namespace, input and output topic requirements: Use a function to find the maximum and minimum values in an array of integers ****** ***********************************************/#include<stdlib.h>#include<iostream>using namespacestd;namespaceCjj//define the namespace as CJJ{ //int *arr: Defines an integer array//int count: Determines the number of cycles//bool Ismax: set bool value intGetmax_min (int*arr,intCountBOOLIsmax) { inttemp = arr[0]; inti =1; for(i;i<count;i++) { if(Ismax)//find the maximum value { if(temp>arr[i]) Temp=Arr[i]; } Else //Find minimum Value { if(temp<arr[i]) Temp=Arr[i]; } } returntemp; }}intMainvoid){ intarr[4] = {3,4,1,5}; BOOLIsmax =false;//initial bool ValueCIN >>Ismax; cout<< Cjj::getmax_min (arr,4, Ismax) <<Endl; System ("Pause"); return 0;}
Operation Result:
Input:1 output:1 input:0 output:5
Use a function to find the maximum and minimum values in an array of integers