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

Complete function PrintPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than...
Complete function PrintPopcornTime(), with int parameter bagOunces, and void return type. If bagOunces is less than 3, print "Too small". If greater than 10, print "Too large". Otherwise, compute and print 6 * bagOunces followed by " seconds". End with a newline. Example output for ounces = 7: 42 seconds #include <stdio.h> void PrintPopcornTime(int bagOunces) { } int main(void) { int userOunces; scanf("%d", &userOunces); PrintPopcornTime(userOunces); return 0; } 2. Write a function PrintShampooInstructions(), with int parameter numCycles, and void return...
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.
Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s...
Rewrite the function so the formal parameter x is an IO pointer-to int parameter. The function’s prototype is changed as shown below. Please explain code with comments //-------------------------------------------------- void NextPrime(int *x) //-------------------------------------------------- { bool IsPrime(const int x); }
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
acos() Prototype double acos(double x); To find arc cosine of type int, float or long double,...
acos() Prototype double acos(double x); To find arc cosine of type int, float or long double, you can explicitly convert the type to double using cast operator. int x = 0; double result; result = acos(double(x)); int x = 0; double result; result = acos(double(x)); acos() Parameter The acos() function takes a single argument in the range of [-1, +1]. It's because the value of cosine is in the range of 1 and -1. Parameter Description double value Required. A...
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 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 -...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT