- Write a function float speed_mph(float distance, float time); where distance traveled is specified in feet and time interval is in seconds. The function should return the speed in miles per hour. A mile is 5280 feet. Show a manual trace.
- Write a program that prints out an integer and its square for all integers in the range from 7 through 17. Use a function to calculate the square of an integer. Show a manual trace.
- Write a program to sum all input numbers until end of file. The program should keep a count of the numbers entered and compute an average of the input numbers. Show a manual trace for the first three numbers.
- Write a function float max(float n1, float n2); that returns the greater of n1 and n2. Write a function float min(float n1, float n2); that returns the lesser of n1 and n2. Write a program that reads in numbers and uses the above functions to find the maximum and the minimum of all the numbers. The end of input occurs when zero is typed. Zero is a valid number for determining the maximum and the minimum. Use debug statements to ensure that the maximum and the minimum are updated correctly.
- Write a program that generates a table of equivalent Celsius (C) and Fahrenheit (F) temperatures from 0 to 212 degrees F. The table entries should be at five degree (F) intervals. Use a function to convert degrees F to C. The conversion between the two is given by:
C = (F - 32) * 5.0 / 9.0
- Write a program that uses a function to determine if a given year is a leap year. A year is a leap year if it is divisible by 400; or if it is divisible by 4 and it is not divisible by 100.
- Modify the pay calculation program of Figure 3.2 so that a function print_data() prints out the input data as well as the pay. The function print_data() should return the number of items it writes to the output.
- Write a function, int factors(int n), where n is a positive integer. The function prints the smallest integer factors of n, excluding 1 and itself. For example, if n is 120, then factors(n) will print 2, 2, 2, 3, 5. The function returns TRUE if n has no factors and FALSE otherwise.
- Write a program that reads a positive integer and tests if it is a prime number by using factors() from Problem 10
- Write a function int gcd(int n, int m); that returns the greatest common divisor (GCD) of non-negative integers n and m. A GCD may be obtained as follows: if m is zero, then GCD is n; otherwise, replace current n by the current m and replace current m by (n % m). Repeat until mbecomes zero and GCD is found.
- Assume that C does not have a divide operator. Write a function int_divide() with two integer arguments that returns an integer quotient when the first argument is divided by the second argument.
- Assume that C does not have a modulus operator. Write a function modulus() with two integer arguments that returns the remainder when the first argument is divided by the second.
- Write a program that prints the accumulated value of an initial investment invested at a specified annual interest and compounded annually for a specified number of years. Annual compounding means that the entire annual interest is added at the end of a year to the invested amount. The new accumulated amount then earns interest, and so forth. If the accumulated amount at the start of a year is acc_amount, then at the end of one year the accumulated amount is:
acc_amount = acc_amount + acc_amount * annual_interest
- Use a function that returns the accumulated value given the amount, interest, and years. The prototype is:
float calc_acc_amt(float acc_amount, float annual_interest, int years);
- Modify the function in Problem 15 so that the interest may be compounded annually, monthly, or daily. Assume 365 days in the year. Hint: Use an argument to specify annual, monthly, or daily compounding of interest. If interest is not to be compounded annually, the annual interest must be converted to monthly (i.e., interest / 12) or daily interest (i.e., interest / 365). The interest must then be compounded each year, each month, or each day as the case may be.
- Write a function that calculates the factorial of an integer n. Use a driver to test the function for values of n from 1 to 7. Factorial of a positive integer, n, is given by the product of positive integers from 1 through n. Use a variable that stores the value of the cumulative product. The cumulative product is multiplied by a new value of an integer each time a loop is executed:
cum_prod = cum_prod * i;
- The initial value of the cumulative product should be 1 so the first multiple accumulates correctly.
- Write a function, float pos_power(float base, int exponent); which returns the value of base raised to a positive exponent. For example, if base is 2.0 and exponent is 3, the function should return 8.0. If the exponent is negative, the function should return 0.
- Write a function, neg_power(), which returns base raised to a negative exponent.
- Modify the functions in Problems 18 and 19 to write a function float power(float base, int exponent); which returns an exponent power of base, where exponent may be positive or negative. If the exponent is zero, it should return 1.
- Write a function int weight(int n); where n is a positive integer. The function returns the weight of the most significant digit, i.e., the highest power of ten which does not exceed n. For example, if n is 2345, weight(n) returns 1000. Assume n is less than 10000.
- Write a function, int sig_dig_value(int n); that returns the integer value of the most significant digit of a positive integer n less than 10000. For example, if n is 2345, sig_dig_value(n) returns integer 2.
- Write a function, int suppress_msd(int n); that returns an integer value of a positive integer after the most significant digit is removed. For example, if n is 2345, suppress_msd(n) returns 345.
- Use Problems 22 and 23 to write a function, print_dig_int(int n); that prints successive integer values of digits of a positive integer n. Each digit value is printed on a separate line. For example, if n is 2345, print_dig_int(n) prints 2 on one line, 3 on the next, 4 on the next, and 5 on the last line.
- Write a function print_dig_float(float x); that writes the value of each digit of a floating point number x. For example, if x is 2345.1234, then print_dig_float(x) will print integer values of digits 2, 3, 4, 5, 1, 2, 3, and 4 in succession.
- Write a macro to evaluate the sum of the squares of two parameters. Make sure the macro can be called with any argument expressions. Write a program that reads two values and uses the above macro to print the sum of the squares.