Question

In: Computer Science

Write down the C++ Program

 Write a function, which accept three integer values as arguments find the largest of three and then return the largest value to main program. Write a main program which will call the function by passing three integer values and print the value returned by the function.?

Solutions

Expert Solution

1. First we create a function that takes the value as a parameter.

void findLargestNumber(int, int, int); // Prototype
void findLargestNumber(int num1, int num2, int num3)
{
}

2. After creating the function now we implement the logic to compare given value/integers. The Logic is given below

   if (num1 == 0 || num2 == 0 || num3 == 0)
    {
        cout << "No one must be zero";
    }
    else if (num1 > num2 && num1 > num3)
    {
        cout << num1 << " is largest number";
    }
    else if (num2 > num1 && num2 > num3)
    {
        cout << num2 << " is largest number";
    }
    else if (num3 > num1 && num3 > num2)
    {
        cout << num3 << " is largest number";
    }

3. Now we implement this into our function.

void findLargestNumber(int num1, int num2, int num3)
{
    if (num1 == 0 || num2 == 0 || num3 == 0)
    {
        cout << "No one must be zero";
    }
    else if (num1 > num2 && num1 > num3)
    {
        cout << num1 << " is largest number";
    }
    else if (num2 > num1 && num2 > num3)
    {
        cout << num2 << " is largest number";
    }
    else if (num3 > num1 && num3 > num2)
    {
        cout << num3 << " is largest number";
    }
}

4. After that now we call this function in our main program and pass the value through the user.

// Main Function
int main()
{
    int firstNumber, secondNumber, thirdNumber;
    cout << "Enter First Number: ";
    cin >> firstNumber;
    cout << "Enter Second Number: ";
    cin >> secondNumber;
    cout << "Enter Third Number: ";
    cin >> thirdNumber;
    findLargestNumber(firstNumber, secondNumber, thirdNumber);
    getch();
}
// Our Logical Function which compare integers
void findLargestNumber(int num1, int num2, int num3)
{
    if (num1 == 0 || num2 == 0 || num3 == 0)
    {
        cout << "No one must be zero";
    }
    else if (num1 > num2 && num1 > num3)
    {
        cout << num1 << " is largest number";
    }
    else if (num2 > num1 && num2 > num3)
    {
        cout << num2 << " is largest number";
    }
    else if (num3 > num1 && num3 > num2)
    {
        cout << num3 << " is largest number";
    }
}

5. That's all and the final program looks like this.

#include <iostream>
#include <conio.h>
using namespace std;
void findLargestNumber(int, int, int); // Prototype
// Main Function
int main()
{
    int firstNumber, secondNumber, thirdNumber;
    cout << "Enter First Number: ";
    cin >> firstNumber;
    cout << "Enter Second Number: ";
    cin >> secondNumber;
    cout << "Enter Third Number: ";
    cin >> thirdNumber;
    findLargestNumber(firstNumber, secondNumber, thirdNumber);
    getch();
}
// Our logical function
void findLargestNumber(int num1, int num2, int num3)
{
    if (num1 == 0 || num2 == 0 || num3 == 0)
    {
        cout << "No one must be zero";
    }
    else if (num1 > num2 && num1 > num3)
    {
        cout << num1 << " is largest number";
    }
    else if (num2 > num1 && num2 > num3)
    {
        cout << num2 << " is largest number";
    }
    else if (num3 > num1 && num3 > num2)
    {
        cout << num3 << " is largest number";
    }
}

#include <iostream>
#include <conio.h>
using namespace std;
void findLargestNumber(int, int, int);
int main()
{
    int firstNumber, secondNumber, thirdNumber;
    cout << "Enter First Number: ";
    cin >> firstNumber;
    cout << "Enter Second Number: ";
    cin >> secondNumber;
    cout << "Enter Third Number: ";
    cin >> thirdNumber;
    findLargestNumber(firstNumber, secondNumber, thirdNumber);
    getch();
}
void findLargestNumber(int num1, int num2, int num3)
{
    if (num1 == 0 || num2 == 0 || num3 == 0)
    {
        cout << "No one must be zero";
    }
    else if (num1 > num2 && num1 > num3)
    {
        cout << num1 << " is largest number";
    }
    else if (num2 > num1 && num2 > num3)
    {
        cout << num2 << " is largest number";
    }
    else if (num3 > num1 && num3 > num2)
    {
        cout << num3 << " is largest number";
    }
}

Related Solutions

Write down the C++ Program To Find Factorial.
Write a function, which accepts an integer value as an argument, finds the factorial of that integer value, and then returns the factorial value to the main program. Write a main program that will call the function by passing an integer value and print the factorial value returned by the function. 
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem:...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem: a2 + b2 = c2 The user will enter values for a and b, and you will calculate c. All of this code will go in just one source file.
Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage...
C program help 1. Write a program to compute the Mileage given by a vehicle. Mileage = (new_odometer – old_odometer)/(gallons_gas) // illustrating how ‘for’ loop works. 2. How to initialize an array of size 5 using an initializer list and to compute it’s sum How to initialize an array of size 5 with even numbers starting from 2 using ‘for’ loop and to compute it’s sum 3. Program to compute the car insurance premium for a person based on their...
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
Using C++ Write One one single program with two or more functioncallsWrite a C++...
Using C++ Write One one single program with two or more function callsWrite a C++ function, smallest Index, that takes as parameters an int array and its size and returns the index of the smallest element in the array. Also the program should test the function.Write another function that prompts the user to input a string and outputs the string in uppercase letters. You must use a character array to store the string.
2. Write a program C++ that asks the user for a number (not necessary to force...
2. Write a program C++ that asks the user for a number (not necessary to force any particular requirements). Write a function with the following signature: double square(double x) that returns the square of the user's number (x * x). 3. Write a C++ program that asks the user for an integer. Write a function that returns 1 of the number is even, and 0 if the number is odd. Use this function signature: int isEven(int x). 4. Write a...
C++ Write a program that takes a string and integer as input, and outputs a sentence...
C++ Write a program that takes a string and integer as input, and outputs a sentence using those items as below. The program repeats until the input string is "quit". If the input is: apples 5 shoes 2 quit 0 the output is: Eating 5 apples a day keeps your doctor away. Eating 2 shoes a day keeps your doctor away.
in c++ pleaseStatistics are often calculated with varying amounts of inputdata. Write a program...
in c++Statistics are often calculated with varying amounts of input data. Write a program that takes any number of non-negative integers as input, and outputs the average and max. A negative integer ends the input and is not included in the statistics.Ex: When the input is 15 20 0 5 -1, the output is:10 20You can assume that at least one non-negative integer is input.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT