Modularity Using Functions
Question 1: (Practice)
a. Write a function named check() that has three parameters. The first parameter should accept an integer number, and the second and third parameters should accept a double precision number. The function body should just display the values of data passed to the function when it’s called.
b. Include the function written in Exercise 2a in a working program. Make sure your function is called from main(). Test the function by passing various data to it.
Question 2: (Program)
a. The time in hours, minutes, and seconds is to be passed to a function named totsec().
Write totsec() to accept these values, determine the total number …show more content…
of seconds in the passed data, and display the calculated value.
b. Include the totsec() function written for Exercise 4a in a working program. The main() function should correctly call totsec() and display the value the function returns. Use the following test data to verify your program’s operation: hours = 10, minutes = 36, and seconds = 54. Make sure to do a hand calculation to verify the result your program displays.
Question 3: (Program)
a. Write a function template named display() that displays the value of the single argument passed to it when the function is called.
b. Include the function template created in Exercise 5a in a complete C++ program that calls the function three times: once with a character argument, once with an integer argument, and once with a double-precision argument.
Question 4: (Program)
a. The volume, V, of a cylinder is given by the formula V = π r 2 L where r is the cylinder’s radius and L is its length. Using this formula, write a C++ function named cylvol() that accepts a cylinder’s radius and length and returns its volume.
b. Include the function written in Exercise 5a in a working program. Make sure your function is called from main() and returns a value to main() correctly. Have main() use a cout statement to display the returned value. Test the function by passing various data to it and verifying the returned value.
Question 5: (Program)
a.
Write a function named ReadOneChar() that reads a key pressed on the keyboard and displays the integer code of the entered character.
b. Include the ReadOneChar() function written for Exercise 10a in a working program.
The main() function should correctly call ReadOneChar() and display the integer the function returns. Test the function by passing various data to it and verifying the returned value.
Question 6: (Program)
a. Write a C++ function named fracpart() that returns the fractional part of any number passed to it. For example, if the number 256.879 is passed to fracpart(), the number
0.879 should be returned.
b. b. Include the function written in Exercise 8a in a working program. Make sure your function is called from main() and returns a value to main() correctly. Have main() use a cout statement to display the returned value. Test the function by passing various data to it and verifying the returned value.
Question 7: (Practice) For the following section of code, determine the data type and scope of all declared variables and symbolic constants on a separate sheet of paper, for example:
Variable or Constant Name
PRICE
Data Type
int
Scope global to main(), roi(), and
step()
#include using namespace std; const int PRICE; const long YEARS; const double YIELD; int main()
{
Int bondtype; double interest, coupon;
.
.
.
return 0;
}
double roi(int mat1, int mat2)
{
int count; double effectiveRate;
.
.
.
return effectiveRate;
}
int step(double first, double last)
{
int numofyrs; double fracpart;
.
.
.
return (10* numofyrs);
}
Question 8: (Practice) Define the scope of the parameter p2 and the variables a, b, c, d, e, f, m, n, p, d, q, and r in the following program structure:
#include
using namespace std; int a, b; double One(float); void Two(void); int main()
{
int c, d; double e, f;
.
. return 0;
}
double One(double p2)
{
char m, n;
.
.
}
void Two(void)
{
int p, d; double q, r;
.
.
}
Question 9: (Practice) what’s the difference between the following functions? void init1()
{
static int yrs = 1;
cout