C++ problem, ask for help Write a program that asks the user to enter a characte
ID: 3792364 • Letter: C
Question
C++ problem, ask for help
Write a program that asks the user to enter a character c1 and three float numbers x1, x2,
x3. The program should calculate and print the result depending upon the value of c1 as
follows:
If c1 = ‘X’: the result is the maximum of x1, x2, and x3
If c1 = ‘N’: the result is the minimum of x1, x2, and x3
If c1 = ‘V’: the result is the average of x1, x2, and x3
If c1 = ’S’ : stop
The program should keep asking the user to enter and process the data until the user
enters a character ‘S’ for c1.
Explanation / Answer
#include <iostream>
#include <string> // std::string
#include <bitset>
float findMax(float x,float y,float z);
float findMin(float x,float y,float z);
float findAvg(float x,float y,float z);
int main(int argc, char *argv[]) {
float x,y,z;
char c1;
while(1){
std::cout << "enter the values of c1,x,y,z separated by spaces ";
std::cin >> c1;
std::cin >> x;
std::cin >> y;
std::cin >> z;
if(c1=='X'){
std::cout << "Maximum of x,y,z is " << findMax(x,y,z) << " ";
}else if(c1=='N'){
std::cout << "Minimum of x,y,z is " << findMin(x,y,z) << " ";
}else if(c1=='V'){
std::cout << "Average of x,y,z is " << findAvg(x,y,z) << " ";
}else{
break;
}
}
}
//This function will find the maximum of three numbers
float findMax(float x,float y,float z){
if(x>y){
if(x>z){
return x;
}else{
return z;
}
}else{
if(y>z){
return y;
}else{
return z;
}
}
}
//This function will find the minimum of three numbers
float findMin(float x,float y,float z){
if(x<y){
if(x<z){
return x;
}else{
return z;
}
}else{
if(y<z){
return y;
}else{
return z;
}
}
}
//This function will find the average of three numbers
float findAvg(float x,float y,float z){
float avg=(x+y+z)/3.0;
return avg;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.