the results:
// Functions
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int get_max_times(int a[]); // get the max of times in compare differently
int get_min_times(int a[]); // get the min of times in compare differently
int integer_random(int min,int max); // get a random integer in range from min to max
int main()
{
//get_max_times();get_min_times()
srand((int)time(0));
int i, count_max_all = 0, count_min_all = 0;
double count_max_average,count_min_average; //±äÁ¿1£¨2£©Í³¼Æ²éÕÒ×î´ó£¨Ð¡£©ÖµµÄ´ÎÊý£»
double iterate_number = 1000.0; //Ñ»·µÄ×Ü´ÎÊý
int min = 1, max = 20;
int a[10];
for (i=0; i<10; ++i) {
a[i] = integer_random(min,max);
}
// for (i=0; i<4; ++i){
// cout<< a[i];
// }
for (i=1; i<= iterate_number; ++i){
count_max_all += get_max_times(a);
count_min_all += get_min_times(a);
}
count_max_average = count_max_all/iterate_number;
count_min_average = count_min_all/iterate_number;
cout << "The average times to find the max in array a = " <<count_max_average <<endl;
cout << "The average times to find the min in array a = " <<count_min_average <<endl;
return 0;
}
int integer_random(int min,int max)
{
int result = rand() % max + min;
return result;
}
int get_max_times(int a[])
{
int i; // the definition of i is to control the time of iteration
int count = 0; //log the time with comparing the difference
int max = a[0]; // store the biggest interger in array
for (i=1; i<10; ++i){
if(a[i] > max){
max = a[i];
++count;
}
}
return count;
}
int get_min_times(int a[])
{
int i; // the definition of i is to control the time of iteration
int count = 0; //log the time with comparing the difference
int min = a[0]; // store the biggest interger in array
for (i=1; i<10; ++i){
if(a[i] < min){
min = a[i];
++count;
}
}
return count;
}