Function Practice Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

A shop owner requires a program to calculate the amount of change to be


given to a customer. The price of the item sold and the amount of cash
tendered by the customer is to be input and the amount of change is to be
calculated and return.

2. A subject has an assignment and an exam. Both the assignment and
exam are marked out of 100. The assignment contributes 40% towards
the assessment of the subject and the exam 60%. A function is required
that allows the user to input and assignment mark (out of 100) and an
exam mark (out of 100) and return an overall mark in the proportions
specified.

3. Write a function named "sum_from_to" that takes two integer arguments, call
them "first" and "last", and returns as its value the sum of all the integers between
first and last inclusive. Thus, for example,
cout<<sum_from_to (4, 7) << endl; // will print 22 because 4+5+6+7 = 22
cout << sum_from_to (-3, 1) << endl; // will print -5 'cause (-3) + (-2) +(-1)+0+1 =
-5
cout << sum_from_to (7, 4) << endl; // will print 22 because 7+6+5+4 = 22
cout << sum_from_to (9, 9) << endl; // will print 9

4. Write a C++ program that contains three user defined function named input,
largest and output, and one main function.
All the inputs must be taken in input function, and all the results must be
displayed in output function that will receive one argument of type float.
The function named largest receives three floating point arguments whose values
must be given by user in the input function and then this function will find out the
largest of three nos and the result of the largest will be displayed in output
function.
In main function you just have to call input function. So there will be no
other function call in the main function.

5. Write a program that calculates the squares and cubes of the numbers
from 0 to 10 and uses tabs to print the following table of values: [8]


6. The power series representation of sin(x) and cos(x) is

sin(x)=

cos(x)=

This is an easy way to evaluate sin(x) or cos(x), by increasing the number of terms the
accuracy of the result is increased.
You are required to implement a program which takes as input:
The value to calculate sin(x) / cos(x) / or both
The number of terms to evaluate
In the end the program should produce the following output:
The value calculated by your program
The actual value using the library functions sin and cos found in the header file
<math.h>
The error in your value as compared to the actual value (calculated value original
value)
7. All years that are evenly divisible by 400 or are evenly divisible by four and
not evenly divisible by 100 are leap years. For example, since 1600 is
evenly divisible by 400, the year 600 was leap year. Similarly, since 1988 is
evenly divisible by four but not by 100, the year 1988 was also a leap year.
Using this information, write a C++ program that accepts the year as user
input, determines if the year is a leap year and displays and appropriate
message that tells the user if the entered year is or is not a leap year.
8. Write a program to work out the time period in year, months and days
between two given dates.
Exercises Deitel
9. Show the value of x after each of the following statements is performed:

x = fabs( 7.5 )
x = floor( 7.5 )
x = fabs( 0.0 )
x = ceil( 0.0 )
x = fabs( -6.4 )
x = ceil( -6.4 )
x = ceil( -fabs( -8 + floor( -5.5 ) ) )
10. A parking garage charges a $2.00 minimum fee to park for up to three
hours. The garage charges an additional $0.50 per hour for each hour or
part thereof in excess of three hours. The maximum charge for any given
24-hour period is $10.00. Assume that no car parks for longer than 24 hours
at a time. Write a program that calculates and prints the parking charges
for each of three customers who parked their cars in this garage
yesterday. You should enter the hours parked for each customer. Your
program should print the results in a neat tabular format and should
calculate and print the total of yesterday's receipts. The program should
use the function calculateCharges to determine the charge for each
customer. Your outputs should appear in the following format:
Car Hours Charge
1 1.5 2.00
2 4.0 2.50
3 24.0 10.00
TOTAL 29.5 14.50
11. An application of function floor is rounding a value to the nearest integer.
The statement
y = floor( x + .5 );
rounds the number x to the nearest integer and assigns the result to y. Write a
program that reads several numbers and uses the preceding statement to round
each of these numbers to the nearest integer. For each number processed, print
both the original number and the rounded number.
12. Function floor can be used to round a number to a specific decimal
place. The statement
y = floor( x * 10 + .5 ) / 10;
rounds x to the tenths position (the first position to the right of the decimal point).
The statement
y = floor( x * 100 + .5 ) / 100;
rounds x to the hundredths position (the second position to the right of the
decimal point). Write a program that defines four functions to round a number x
in various ways:
roundToInteger( number )
roundToTenths( number )
roundToHundredths( number )
roundToThousandths( number )

For each value read, your program should print the original value, the number
rounded to the nearest integer, the number rounded to the nearest tenth, the
number rounded to the nearest hundredth and the number rounded to the
nearest thousandth.
13. Answer each of the following questions:
What does it mean to choose numbers "at random?"
Why is the rand function useful for simulating games of chance?
Why would you randomize a program by using srand? Under what
circumstances is it desirable not to randomize?
Why is it often necessary to scale or shift the values produced by rand?
Why is computerized simulation of real-world situations a useful technique?

14. Write statements that assign random integers to the variable n in the
following ranges:

1 n 2
1 n 100
0 n 9
1000 n 1112
1 n 1
3 n 11
6.18 Write a function integerPower ( base, exponent ) that returns the value of
base exponent


For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is a
positive, nonzero integer and that base is an integer. The function integerPower
should use for or while to control the calculation. Do not use any math library
functions.

15. (Hypotenuse) Define a function hypotenuse that calculates the length of
the hypotenuse of a right triangle when the other two sides are given. Use
this function in a program to determine the length of the hypotenuse for
each of the triangles shown below. The function should take two double
arguments and return the hypotenuse as a double.
16. Write a function multiple that determines for a pair of integers whether the
second is a multiple of the first. The function should take two integer
arguments and return true if the second is a multiple of the first, false
otherwise. Use this function in a program that inputs a series of pairs of
integers.

17. Write a program that inputs a series of integers and passes them one at a
time to function even, which uses the modulus operator to determine
whether an integer is even. The function should take an integer argument
and return true if the integer is even and false otherwise.

You might also like