Question

In: Computer Science

Overload and test function getNum(int &) to work with a parameter of type double. 2. Overload...

Overload and test function getNum(int &) to work with a parameter of type double.

2. Overload and test function doubleNum(int) to also work with a parameter of type double.

3. Both functions for collecting input should validate such that negative values would not be allowed.

Perform the following steps:

a. Add the function prototypes at the top.

b. In main(), add a new variable of type double. You can name it "value2" to distinguish from the other variable.

c. Write a new getNum() function with a parameter of type double. A copy/paste should be fine with the corresponding modifications.

d. Add to both getNum() functions input validation such that negative values are not allowed.

e. Write a new doubleNum() function with a parameter of type double. Again copy/paste is fine but with corresponding modifications.

f. In main() add code for working with the variable of type double that resembles the same logic (statements) for the integer variable.

// This program uses 2 functions: a void function with a reference

// variable as a parameter, and a value-returning function.

#include <iostream>

using namespace std;

// Function prototypes

void getNum(int &);

int doubleNum(int);

int main()

{

int value;

// Call getNum to get a number and store it in value

getNum(value);

// Call doubleNum, passing it the number stored in value

// Assign value the number returned by the function

value = doubleNum(value);

// Display the resulting number

cout << "That value doubled is " << value << endl;

return 0;

}

/**********************************************************

* getNum *

* This function stores user input data in main's value *

* variable by using a reference variable as a parameter. *

*********************************************************/

void getNum(int &userNum)

{

cout << "Enter a number: ";

cin >> userNum;

}

/***********************************************************

* doubleNum *

* This function doubles the number it receives as an *

* argument and returns it to main thru a return statement.*

***********************************************************/

int doubleNum (int number)

{

return number * 2;

}

Solutions

Expert Solution

Code for your program is provided below. The code is explained in code comments. Screenshot of output as well as code in IDE is provided in the last for your better understanding. If you need any further clarification please feel free to ask in comments.

#################################################################################

CODE

#include <iostream>

using namespace std;

//function prototype for integer value
void getNum(int &);
int doubleNum(int);

//overloaded function prototype for double value
void getNum(double &);
double doubleNum(double);

int main()
{
    int value;  //integer variable
    getNum(value);   //take input
    value = doubleNum(value);   //call double to double it
    cout << "That value doubled is " << value << endl;   //display double number

    cout<<"\n";
    double value2;   //create double variable
    getNum(value2);   //take input
    value2 = doubleNum(value2);  //double the number
    cout << "That value doubled is " << value2 << endl;   //display it

    return 0;
}

//take input for integer
void getNum(int &userNum)
{
    do  //loop
    {
        cout << "Enter a number: ";
        cin >> userNum;
    }while(userNum<0);   //loop if number is negative
}

int doubleNum (int number)
{
    return number * 2;  //return double of the number
}

//overloaded function to take double input
void getNum(double &userNum)
{
    do
    {
        cout << "Enter a number: ";
        cin >> userNum;
    }while(userNum<0);  //loop if number is negative
}

//overloaded function to return two times of a double number
double doubleNum (double number)
{
    return number * 2.0;
}

##########################################################################

OUTPUT

###############################################################################

CODE IN IDE


Related Solutions

C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
C++ Program Overload the following operators to work with the Rational class and add test cases...
C++ Program Overload the following operators to work with the Rational class and add test cases in the driver program. Make sure your driver program now tests each of these symbols for your Rational Class. + – * / == != < <= > >= << (stream insertion operator, use the toString function) >> (stream extraction operator) Make sure you test all 12 operators Example Run (Bold is input), everything else is a print statement using the overloaded operators Enter...
Write a function that will take in an array (of type double), and will return the...
Write a function that will take in an array (of type double), and will return the array with all of the elements doubled. You must use pass-by-reference and addressing and/or pointers to accomplish this task. C++
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be...
Write a subroutine that takes an int (.long) parameter and squares it. The parameter should be passed in on the stack. The answer should be returned in eax. The subroutine should not disturb any registers except eax, ecx, and edx. (Save any registers on the stack and restore them before exiting the subroutine.) Upon entry to the subroutine, push ebp, etc., to access the parameter.
Write a function which takes one parameter int num, and prints out a countdown timer with...
Write a function which takes one parameter int num, and prints out a countdown timer with minutes and seconds separated by a colon (:). It should print out one line for each second elapsed and then pause one second before printing out the next line. A few things to note: - You can assume that calling the function usleep(1000000) makes the program pause for one second - It should count down from num minutes:zero seconds to zero minutes:zero seconds -...
string getStyle() ; int getNumOfBedRooms() ; int getNumOfBathRooms(); int getNumOfCarsGarage(); int getYearBuilt(); int getFinishedSquareFootage() ; double...
string getStyle() ; int getNumOfBedRooms() ; int getNumOfBathRooms(); int getNumOfCarsGarage(); int getYearBuilt(); int getFinishedSquareFootage() ; double getPrice(double p) ; double getTax(double t) ; void print() ; houseType(); houseType(string s, int numOfBeds, int numOfBaths, int numOfCars, int yBuilt, int squareFootage, double p, double t); private: string style; int numOfBedrooms; int numOfBathrooms; int numOfCarsGarage; int yearBuilt; int finishedSquareFootage; double price; double tax; }; Questions a.Write the definition of the member function set so that private members are set accordingy to the parameters....
I need to write a function the will take in an array (of type double), and...
I need to write a function the will take in an array (of type double), and will return the array with all of the elements doubled while using pass-by-reference and addressing and/or pointers. This is what i have so far, but im messing up in line 31 where i call the function i believe so im not returning the correct array? please help edit. #include <iostream> #include <iomanip> using namespace std; double *doubleArray ( double arr[], int count, int SIZE);...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT