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...
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 :...
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...
(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.
C++ Goals: Write a program that works with binary files. Write a program that writes and...
C++ Goals: Write a program that works with binary files. Write a program that writes and reads arrays to and from binary files. Gain further experience with functions. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of file, a pointer to an int array, and the size of the array. The function should open the specified file in binary made, write the contents into the array, and then close the file. write another...
Write a program using c++. Write a program that uses a loop to keep asking the...
Write a program using c++. Write a program that uses a loop to keep asking the user for a sentence, and for each sentence tells the user if it is a palindrome or not. The program should keep looping until the user types in END. After that, the program should display a count of how many sentences were typed in and how many palindromes were found. It should then quit. Your program must have (and use) at least four VALUE...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT