Question
Asked Dec 8, 2019
2 views

Write the following in C++

Write three functions, float getNum(), float add(float, float), void outSum(float); that asks user for two numbers, finds the sum of two numbers, and displays the sum repeatly in a main program until the user enter “0” for either one of the numbers.
check_circle

Expert Answer

Step 1

The C++ program and approach are given below:

Approach:

  • Declaring the essential headers and the namespaces.
  • Defining the three functions “float getNum()”, “float add(float x, float y)” and “void outSum(float s)”.
  • Inside the “float getNum()” function, variable num is declared which stores the two numbers entered by the user. The getnum() function allows the user to enter two numbers. When the user enters the first number it stores in the num variable than it stores to the getnum(). And it again asks the user to enter the second number.
  • If-else statement will check whether the number is greater than zero or not.
  • Inside the “float add(float x, float y)”, sum of x and y is returned.
  • The “void outSum(float s)” function will display the sum.
  • Inside the main() function while loop will execute until the statement is true(num!=0).
  • Inside the while loop, the outSum() is called and add() function is called as the function parameter for the outSum() The getNum() function is called two times as the function parameter for add() function(as add() takes two arguments).
Step 2

Program:

//essential headers

#include <iostream>

//using namespace

using namespace std;

//getNum() to take input from user

float getNum()

{

    //declaring the variables

    float num;

    //prompts the user to enter the number

    cout<<"Enter a number:\n";

    cin>>num;

    //if statement to check the entered number is greater than zero

    if(num != 0)

    {

        return num;

    }

    else

        exit (0);

}

//add() function will return the sum of two arguments

float add(float x, float y)

{

    //returns the sum

    return x+y;

}

//outSum() will displays the sum 

void outSum(float s)

{

    //di...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Compiler returning errors. Screenshot of File.txt as well as compiler errors included.  //import...

A: Given program code is working perfectly and there is no error in this code. The error is in the file...

question_answer

Q: List and describe four vulnerability intelligence sources. Which seems the most effective? Why?

A: Sources of Vulnerability IntelligenceSecurity VendorsSoftware VendorsGovernment and public organizat...

question_answer

Q: Given the follwing businessrulea, identify primary keys, foreign keys, attributes, cardinalities.  A...

A: Primary keys: University: U_NameSchools: S_NameDepartments: D_IDProfessors: P_IDCourses: C_ID

question_answer

Q: construct a pda that accepts the language defined by the grammar s → abSb|λ.

A: Constructing PDA:There are two states q and f. q is the start state and f is the final state. Here i...

question_answer

Q: What is pseudo code?

A: Pseudocode is used in algorithm and programming based fields to outlines or represents the implement...

question_answer

Q: Python Coding-While Loops  (In PyCharm Community edition)   Create a while loop that SUMS up ALL the...

A: Prompt user to enter initial numberPrompt user to enter ending numberUsing while loop sums up all th...

question_answer

Q: What are the three aspects of infornation security management

A: Information securities also known as infosec, it is the way to control security risk like unauthoriz...

question_answer

Q: Find the range of salaries (min, max) of each department in the following table:EMP(EID, ENAME, SAL,...

A: EMP is the name of the table Employee.EMP table has four rows named EID, ENAME, SAL, and DEPT. Find ...

question_answer

Q: What is the difference between using direct and indirect addressing? Give an example.

A: Click to see the answer