Previous: 6.6 Exercises
Up: 6 Pointers
Previous Page: 6.6 Exercises
- Write a program that initializes integer type variables,
data1 and data2 to the values 122 and
312. Declare pointers, ptr1 and ptr2;
initialize ptr1 to point to data1 and ptr2
to point to data2.
Swap the values of data1 and data2 values using direct
access and using
indirect access. Next, swap the values of the pointers,
ptr1 and ptr2 and print the values
indirectly accessed by the swapped pointers.
- Write a function (that returns void) which reads and indirectly
stores three
values in the calling function.
The types of the three data items are an integer,
a character, and a float.
- Write a function maxmin(float x, float * pmax, float * pmin)
where x is a
new value which is to be compared with the largest and the smallest values
pointed to by pmax and pmin, respectively.
The function should indirectly
update the largest and the smallest values appropriately.
Write a program that reads a
sequence of numbers and uses the above function to update the maximum and the
minimum until end of file, when the maximum and the minimum should be printed.
- Repeat Problem
.
using functions
get_course_data(), calc_gpr(), and
print_gpr().
- Rewrite Problem
.
as a function that finds the roots of a quadratic and returns them indirectly.
Rewrite the program to solve simultaneous equations
(Problem
.
). The
program should use a function, solve_eqns() to solve for the unknowns.
The
function must indirectly access objects in main() to store the solution values.
- Write a menu-driven program that uses the function, solve_eqns(),
of Problem 6. The
commands are: get data, display data, solve equations, print solution, verify
solution, help, and quit. Use functions to implemnent the code
for each command.
A rational number is maintained as a ratio of two integers, e.g., 20/23,
35/46, etc. Rational number arithmetic adds, subtracts, multiplies and divides
two rational numbers. Write a function to add two rational numbers.
Write a function to subtract two rational numbers.
Write a function to multiply two rational numbers.
Write a function to divide two rational numbers.
Write a function to reduce a rational number. A reduced rational number is
one in which all common factors in the numerator and the denominator have been
cancelled out. For example, 20/30 is reduce to 2/3, 24/18 is reduced to 4/3,
and so forth.
- Use the function, reduce(), of Problem 12
to implement the functions in Problems 8
through 11.
- Rewrite the program of Problem
.
to calculate the current and the power
in a resistor using a function instead to perform the calculations. One value
may be returned as a function value, the other must be indirectly stored in the
calling function.
Previous: 6.6 Exercises
Up: 6 Pointers
Previous Page: 6.6 Exercises