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. 
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test 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...
For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT