In: Computer Science
Please use c++ and follow the instruction
I have written my own code but somehow I'm confused during the process I cannot figure out how to finish this lab, I need help.
Write a program that allows user to input four floating-points values and then prints the largest. It must use functions max2 (given below) to determine the largest value. Feel free to use additional functions, but you are not required to do so. Hint: main will call max2 three times to get the work done (i.e., there should be three calls to max2 function).
double max2(double a, double
b)
{
double max;
if (a >
b)
max = a;
else
max = b;
return max;
}
Follow the format for the sample I/O below. When your code is complete and runs properly, capture the output. Copy and paste both the source code and the output for both test cases.
Test case 1:
Author: Your name
This program allows one to input four values.
It uses function max2 to determine the maximum.
Please enter first value -->
5.2<Enter>
Please enter second value -->
1.0<Enter>
Please enter third value -->
7.1<Enter>
Please enter fourth value -->
2.5<Enter>
The largest value is 7.1
Test case 2:
Author: Your name
This program allows one to input four values.
It uses function max2 to determine the maximum.
Please enter first value -->
5.2<Enter>
Please enter second value -->
7.0<Enter>
Please enter third value -->
7.0<Enter>
Please enter fourth value -->
2.5<Enter>
The largest value is 7.0
Here is my code
#include<iostream>
#include<string>
using namespace std;
int max2(double a, double b, double& max);
void displayInfo(double max);
int main()
{
double a, b, c, d;
double number;
int counter;
cout << "Please enter first value ";
cin >> a;
cout << "Please enter second value ";
cin >> b;
cout << "Please enter third value ";
cin >> c;
cout << "Please enter fourth value ";
cin >> d;
displayInfo(max);
}
// defination determine the largest value
int max2(double a, double b, double& max)
{
if (a > b)
{
max = a;
}
else
{
max = b;
}
}
// module displayInfo
void displayInfo(double max)
{
cout << "The largest value is "<< max <<
"\n";
}
Source Code:
Output:
Code in text format (See above images of code for indentation):
#include<iostream>
using namespace std;
/*function prototypes*/
double max2(double a,double b);
void displayInfo(double max);
/*main function*/
int main()
{
/*variables*/
double a, b, c, d;
double n1,n2;
/*read 4 floating point numbers*/
cout << "Please enter first value ";
cin >> a;
cout << "Please enter second value ";
cin >> b;
cout << "Please enter third value ";
cin >> c;
cout << "Please enter fourth value ";
cin >> d;
/*function calls*/
/*find max in a and b and store in n1*/
n1=max2(a,b);
/*find max in c and d and store in n2*/
n2=max2(c,d);
/*now find max in n1 and n2 and store in n1 is
maximum*/
n1=max2(n1,n2);
/*function call to display maximum*/
displayInfo(n1);
}
/*function defination determine the largest value*/
double max2(double a, double b)
{
double max;
/*check for max and assign maximum to max*/
if (a > b)
max = a;
else
max = b;
return max;
}
/*module displayInfo*/
void displayInfo(double max)
{
cout << "The largest value is "<< max <<
"\n";
}