Answer to Question #205160 in C++ for shuvo

Question #205160

Write a function that takes three int parameters a, b, c and returns the median of

 the three numbers..


1
Expert's answer
2021-06-10T01:36:56-0400
#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);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog