Write a function that takes three int parameters a, b, c and returns the median of
the three numbers..
#include<iostream>
using namespace std;
int median(int a, int b, int c){
int temp ;
int arr[3] = {a,b,c}; //Declaring array to store three int parameters
//Sort the array in ascending order
for (int i =0; i<3; i++){
for(int j =i+1; j<3; j++){
if(arr[j]<arr[i]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr[1]; // Return the second element
}// Main
int main(){
int a, b, c;
cout<<"Enter the first element"<<endl;
cin>>a;
cout<<"Enter the second element"<<endl;
cin>>b;
cout<<"Enter the third element"<<endl;
cin>>c;
cout<<"The median is:\t"<<median(a,b,c);
}
Comments
Leave a comment