Exercises (Function)


Computer Programming
Exercises (Function)
1. Write a function called “circarea” that finds the area of a circle. It should take an argument of type float and return an argument of the same type. Write a main() function that gets a radius value from the user, calls “circarea”, and displays the result. The area of the circle = PI*radius*radius, and P=3.14.
2. Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called “Power” that takes a double value for n and an int value for p, and returns the result as a double value. Use a default argument of 2 for p, so that if this argument is omitted, the number n will be squared. Write a main() function that gets values from the user to test this function.
3. Write a function called “zeroSmaller” that is passed two int arguments by reference and then sets the smaller of the two numbers to 0. Write a main() program to exercise this function.
4. Write a function that takes two “Distance” values as arguments and returns the larger one. Include a main() program that accepts two Distance values from the user, compares them, and displays the larger. (See the RETSTRC program for hints.)
5. Write a function called “hms_to_secs” that takes three int values—for hours, minutes, and seconds—as arguments, and returns the equivalent time in seconds (type long).Create a program that exercises this function by repeatedly obtaining a time value in hours, minutes, and seconds from the user (format 12:59:59), calling the function, and displaying the value of seconds it returns.
6. Start with the “Power” function of Exercise 2, which works only with type double.
Create a series of overloaded functions with the same name that, in addition to double, also work with types char, int, long, and float. Write a main() program that exercises these overloaded functions with all argument types.
7. Write a function called “Swap” that interchanges two int values passed to it by the calling program. (Note that this function swaps the values of the variables in the calling program, not those in the function.) You’ll need to decide how to pass the arguments. Create a main() program to exercise the function.
8. Repeat the previous exercise, but instead of two int variables, have the “Swap” function interchange two struct “time” values (struct “time” was in exercise 5, Chapter 4 sheet).
9. Write a function that, when you call it, displays a message telling how many times it has been called: “I have been called 3 times”, for instance. Write a main() program that calls this function at least 10 times. Try implementing this function in two different ways. First, use a global variable to store the count. Second, use a local static variable. Which is more appropriate? Why can’t you use a local variable?
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. 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.
13. 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.
14. Write a program that inputs three double-precision, floating-point numbers and passes them to a function that returns the smallest number.
15. An integer is said to be prime if it is divisible by only 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not.
(a). Write a function that determines whether a number is prime.
(b). Use this function in a program that determines and prints all the prime numbers between 2 and 10,000. How many of these numbers do you really have to test before being sure that you have found all the primes?
(c). Initially, you might think that n/2 is the upper limit for which you must test to see whether a number is prime, but you need only go as high as the square root of n. Why? Rewrite the program, and run it both ways. Estimate the performance improvement.
16. Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.
17. Write a function “qualityPoints” that inputs a student's average and returns 4 if a student's average is 90100, 3 if the average is 8089, 2 if the average is 7079, 1 if the average is 6069 and 0 if the average is lower than 60.
18. Write function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double.
Post A Comment
  • Blogger Comment using Blogger
  • Facebook Comment using Facebook
  • Disqus Comment using Disqus

No comments :