Question

In: Computer Science

create a C++ program where you have 2 functions with two parameters. Limit the first function...

create a C++ program where you have 2 functions with two parameters. Limit the first function allowed input to values of numbers from 1-10 and from 5 to 20 for the second function. have each function add their two-parameter together then add the functions final values together. Show error message if wrong input is entered   Ask the user if they wish to continue the program (use loop or decision for this question).

Solutions

Expert Solution

#include <iostream>
using namespace std;

//function1 that adds their parameters
int function1(int num1, int num2)
{
    return num1 + num2;
}


//function2 that adds their parameters
int function2(int num1, int num2)
{
    return num1 + num2;
}

int main()
{
    //for 4 numbers provided by user
    int num1, num2, num3, num4;
    //user choice for continue
    char choice;
    
    //if user selects y it will continue, or else at least 1 time execution
    do
    {
        //for number1
        do{
            cout << "Enter number 1 for function 1 : ";
            cin >> num1;
            if(num1 <= 1 || num1 >= 10)
            {
                cout << "Number must be from 1 to 10" << endl;
            }
        }while(num1 <= 1 || num1 >= 10);
        
        //for number2
        do{
            cout << "Enter number 2 for function 1 : ";
            cin >> num2;
            if(num2 <= 1 || num2 >= 10)
            {
                cout << "Number must be from 1 to 10" << endl;
            }
        }while(num2 <= 1 || num2 >= 10);
        
        //for number3
        do{
            cout << "Enter number 1 for function 2 : ";
            cin >> num3;
            if(num3 <= 5 || num3 >= 20)
            {
                cout << "Number must be from 5 to 20" << endl;
            }
        }while(num3 <= 5 || num3 >= 20);
        
        //for number4
        do{
            cout << "Enter number 2 for function 2 : ";
            cin >> num4;
            if(num4 <= 5 || num4 >= 20)
            {
                cout << "Number must be from 5 to 20" << endl;
            }
        }while(num4 <= 5 || num4 >= 20);
        
        //function call for adding first two numbers
        int result1 = function1(num1, num2); 
        
        //function call for adding third and fourth numbers
        int result2 = function1(num3, num4); 
        
        //adds a result of both
        int result = result1 + result2;
        
        //prints a result
        cout << "Result : " << result << endl;
        
        //takes user choice to continue
        cout << "Do you want to continue??(y/n) : ";
        cin >> choice;
        
    }while(choice == 'y');
    
    return 0;
}

Please refer below screenshot of code for indentation.

Sample output :


Related Solutions

Requirements: In this assignment, you are going to create two switch functions. The first function is...
Requirements: In this assignment, you are going to create two switch functions. The first function is going to be called switchVal and the second is going to be called switchRef. The goal of this assignment is to demonstrate the understanding of what pass-by-reference (Links to an external site.) and pass-by-value (Links to an external site.) do when working with functions that you create. The two functions that you will modify need to accept two parameters and inside them they need...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Create a c++ program to compute the product of two integers. call create the following functions:...
Create a c++ program to compute the product of two integers. call create the following functions: 1. getNum - to accept only positive numbers && will call computeProd. 2.computeProd - to compute the product of the numbers & will call the function displayProduct. 3. displayProduct - to display the product. Call the function getNum in the main function. Compute the product w/o using the multiplication operator(*). using #include <iostream> only
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
How to use a Java program that calls for two functions. The first function is asking...
How to use a Java program that calls for two functions. The first function is asking the user for a two integer numbers, then display the sum of the numbers. The second method is asking the user for two floating point numbers, then display the product of the numbers. Call these methods addNumbers and multiplyNumbers respectively. Call the program AddAndMultiply
Create a function powerTo which receives two parameters, a floating point number (the first parameter) and...
Create a function powerTo which receives two parameters, a floating point number (the first parameter) and an integer (the second parameter). If the second parameter is nonnegative, the function returns the value of the first parameter raised to the power of the second parameter. Otherwise, if the second parameter is negative the function returns 0. For example powerTo(2,3)=8, and powerTo(2,-3)=0
C++ Write the definition of a function minMax that has five parameters. The first three parameters...
C++ Write the definition of a function minMax that has five parameters. The first three parameters are integers. The last two are set by the function to the largest and smallest of the values of the first three parameters. The function does not return a value. The function can be used as follows: int a=31, b=5, c=19 big, small; minMax(a,b,c,&big,&small); /* big is now 31 */ /* small is now 5 */ **ONLY THE FUNCTION
Design and implement a function with two input parameters, A and B. The functions then calculates...
Design and implement a function with two input parameters, A and B. The functions then calculates the result of the floor division of A over B (A//B). You are not allowed to use the floor division operator. Look at here: https://simple.wikipedia.org/wiki/Division_(mathematics) - For instance the function for 20 and 6 will return 3.
Please C++ create a program that will do one of two functions using a menu, like...
Please C++ create a program that will do one of two functions using a menu, like so: 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 1 Enter Catalan number to calculate: 3 Catalan number at 3 is 5 1. Do Catalan numbers 2. Do Fibonacci numbers (recursive) 0. Quit Enter selection: 2 Enter Fibonacci number to calculate: 6 Fibonacci number 6 is 8 Create a function of catalan that will take a parameter and return...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT