1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
using namespace std;
//the trick is in sending min_place and max_place as refferences in that way if u change them in the function they are automaticli
//changed in the main program, after that it is just finding the max and min elements and writing the value of i-the counter to the max_place and min_place
//and adding one because the array counter starts at 0 and not from 1
void MinAndMax(int array[],int array_lenght, int &min_place,int &max_place){
int min(array[1]);
int max(array[1]);
for(int i=0; i<array_lenght; i++){
cout << array[i] << " ";
if(array[i]>max){
max=array[i];
max_place=i+1;
}
if(array[i]<min){
min_place=i+1;
min=array[i];
}
}
}
int main(){
int array[10]={23,54,28,64187,5,8,3920,6547,6895,18};
int min_place,max_place;
MinAndMax(array,10,min_place,max_place);
cout << "The maximum element is in place : " << max_place << "\nand the minimum is in place " << min_place << endl;
return 0;
}
|