Previous: 5.7 Exercises
Up: 5 Numeric Data Types and Expression Evaluation
Previous Page: 5.7 Exercises
Write a program to calculate the roots of a quadratic equation:
The program should repeatedly read the set of coefficients
,
, and
.
For each set, calculate the roots if and only if
is not less than
.
Otherwise, write a message that the roots are not real and proceed to the
next set of coefficients. The two roots of a quadratic are:
Use the sq_root() function defined in the chapter.
- Write a function to find exp(x) whose value is given by the Taylor
series:
where
is
factorial.
Write and use a function, power(x, n),
which returns the n
power of x, where n
is an integer.
Use a function, fact(), to compute the factorial.
Write a driver that reads
input values of x,
and finds exp(x). Use as many terms as needed to make values
before and after an additional term very close.
- Write a function to evaluate sin(x) using the expansion shown below.
Use it in a program to find the sine of values read until end of file.
- Write a function, cos(x), using the expansion below
and use it in a program to find the cosine of values
read until EOF.
- What are the limitations on the accuracy of the above expansions?
Write a function that returns the number of ways that r items can be taken
together out of n items. The value of combination is:
Use long integers for factorials.
- Extend the range of possible values for Problem 6
by cancelling out common factors in numerator and denominator.
- Write a program that uses Newton's method to find the roots of the equation:
Newton's method uses successive approximations. Start with a guess value for
root. The improved value of root is given by:
where
is the value of the function when
equals
, and
is the value of the function below when
equals
:
- Write a program that finds the approximate value of an integral of a
function whose four sample values
,
,
,
are specified at time instants
,
,
,
.
The user should be asked for the value of
the interval size,
, and starting instant,
.
The approximate value of an
integral from
to
is the sum of
the area under each rectangle made
up of the sample value and the inter-sample distance, i.e.:
Write a program that reads in the coefficients and the right hand side
values for two linear simultaneous equations. Solve the equations for the
unknowns and print the solution values. The equations are:
where
,
,
,
,
, and
are the coefficients to be read, and
and
are the unknowns.
To solve the equations,
multiply the first equation coefficients and right hand side by
and add the corresponding
values to those of the second equation.
The new, modified value of
will be zero, so
the second equation can be solved for
, and,
substituting the value of
in the first equation, solve for
.
Given coefficients and the right hand side of two simultaneous equations,
verify if a given set of values for
and
is correct.
If the left hand side
and the right hand side are within a small error margin the solution is assumed
to be correct. Let the margin of error be a specifiable value with an assumed
default value.
- Write a menu-driven program to solve and verify two linear equations as per
Problems 10 and 11.
Allow the following commands: get data, display data, solve equations,
display solution, verify solution, help, and quit.
Write a program to determine the current and the power consumed in an
electrical resistor (load) of 10000 ohms if it is connected to a battery of 12
volts.
Power consumed in a resistor is
, where
is the volts across
the resistor and
is the resistor value in ohms.
The current in a resistor is
given by
.
Use for loops to write a program that finds all prime numbers less than a
specified value.
- Use do...while loops to write Problem 14.
Write a program that reads a year, a month, and a day of the month. It
then determines the number of the day in the year. (Use the definition of a leap
year given in Problem
.
).
Use enumeration type for the months, and a switch
statement which uses the number of days in the year prior to the first of each
month.
- Modify Problem 16
so the program reads the day of the week on the first of
January and determines the day of the week for the specified date.
- Write a program to read the current date in the order: year, month, and day
of the month. The program then prints the date in words: Today is the nth day
of Month of the year Year. Example:
- Today is the 24th day of December of the year 2000.
- If the GCD of two numbers,
and
is 1,
they have no common divisor. Write a
program to find all pairs of numbers, in the range 2 to 20, that have no common
divisors. (Refer to Problem
.
for the definition of GCD).
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 program that repeatedly reads and adds two
rational numbers. The program should print the result in each case
as a rational
number.
- Write a program to subtract two rational numbers.
- Write a program to multiply two rational numbers.
- Write a program to divide two rational numbers.
Write a program 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. The GCD can be used to reduce a rational number.
- Modify the rational numbers programs in Problems 20
through 24 so the
result is first reduced before it is printed.
Previous: 5.7 Exercises
Up: 5 Numeric Data Types and Expression Evaluation
Previous Page: 5.7 Exercises