C++ Write an overloaded function max() that takes either two or three parameters
ID: 3890947 • Letter: C
Question
C++ Write an overloaded function max() that takes either two or three parameters of type double and returns the largest value Specifications: Use two functions called max) that are overloaded to solve the problem. The program should prompt for 2 inputs, call the appropriate max () function, and print the maximum value. The second part of the program should prompt for 3 inputs, call the appropriate max ) function, and again, print the maximum value. Print the results with two digits after the decimal point. As an example, if you execute the program with the following underlined inputs, the output will be: ->main.o Enter x:9.7 Enter y: -4.5 The maximum value is: 9.70 Enter x: 13.2 Enter y:23.4 Enter z: 44.2 The maximum value is 44.20 Develop your I/'O diagram and pseudocode, debug your code, and submit to the Grader ProgramExplanation / Answer
//Following program is in C#(C sharp). Overloading is defined in FindMax class which is a standard class for almost //all computer languages
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ApplicationForFindingLargestNumber
{
class Program
{
public static void Main(string[] args)
{
//basic declarations
double input1, input2, input3;
FindMax obj = new FindMax();
//1st Part of Program to find max of 2 numbers
Console.Write("Enter x: ");
double.TryParse(Console.ReadLine(),out input1);
Console.Write("Enter y: ");
double.TryParse(Console.ReadLine(), out input2);
Console.WriteLine("The maximum value is: "+ String.Format("{0:0.00}", obj.max(input1, input2)));
//2nd Part of Program to find max of 3 numbers
Console.Write("Enter x: ");
double.TryParse(Console.ReadLine(), out input1);
Console.Write("Enter y: ");
double.TryParse(Console.ReadLine(), out input2);
Console.Write("Enter z: ");
double.TryParse(Console.ReadLine(), out input3);
Console.Write("The maximum value is: " + String.Format("{0:0.00}", obj.max(input1, input2,input3)));
Console.ReadLine();
}
}
class FindMax
{
//max function for 2 inputs
public double max(double input1, double input2)
{
if (input1 > input2)
{
return input1;
}
return input2;
}
//max function for 3 inputs
public double max(double input1, double input2, double input3)
{
return max(max(input1, input2), input3);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.