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 in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for...
Please Write C++ PROGRAM : That will write a program that initially prompts the user for a file name. If the file is not found, an error message is output, and the program terminates. Otherwise, the program prints each token in the file, and the number of times it appeared, in a well formatted manner. To accomplish all this, do the following: - Open the file - the user must be prompted and a file name input. DO NOT hardcode...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
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...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant...
Write Program in C: Write a program that: program starts; declares and initializes to 7.25% constant float variable NJSALES_TAX; declares and initializes to 1000 an integer variable total; declares and initializes to zero a float variable grand_total; prompt and input on new line total; calculate grand_total to equal total plus (total*NJSALES_TAX); if grand_total <= 1000 print on new line “Grand total is less than or equal to 1000 it is $” and the grand_total to two decimal places; else if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT