<< Chapter < Page Chapter >> Page >

F = (9/5)C + 32

C = (5/9)(F-32)

8. Write a C++ program to calculate the square root and the reciprocal of a user-entered number. Before calculating the square root, you should validate that the number is not negative, and before calculating the reciprocal, check that the number is not zero.

Exercise 5

1. Write and run a program that reads a positive integer value for K and then computes K! = 1*2*3*…*(K-1)*K and displays the result out.

2. Write and run a program that computes x raised to the power n by repetitive multiplication. Then modify your program to calculate x raised to the power (-n).

3. Write and run a program to tabulate sin(x), cos(x) and tan(x) for x = 5, 10, 15,…,85 degrees. Notice that you have to convert x from degrees to radians before using standard functions sin(x), cos(x), tan(x).

4. Write and run a program to read a list of real numbers and then find the number of positive values and the number of negative ones among them. The number of entries is also entered by the user.

5. Write and run a program to compute the sum of square of the integers from 1 to N, where N is an input parameter.

6. Write and run a program to compute the value of pi, using the series for approximating:

pi/4 = 1-1/3+1/5-1/7+ ... + (-1)^n/(2*n+1)

Hint: Use a while loop that terminates when the difference between two successive approximations is less than 1.0E-6.

7. The value of Euler’s number, e, can be approximated using the formula:

e = 1 + 1/1! +1/2! + 1/3! +1/4! + … +1/n!

Using this formula, write a program that approximates the value of e using a while loop that terminates when the difference between two successive approximations is less than 1.0E-6.

8. The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13,…, where the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms, that is, Fib(n) = Fib(n-1) + Fib(n-2). Using this information, write a program that calculates the nth number in a Fibonacci sequence, where n is entered into the program by the user.

9. Write and run a program that inputs an array of N real numbers, and then computes the average value of the array elements. N should be an input parameter.

10. Write and run a program that inputs an array of N real numbers, and then finds the largest element in the array. N should be an input parameter.

11. Write and run a program that inputs an integer matrix of order n and transposes it and then prints it out. Transposing a square matrix means:

a(i,j)<==>a(j,i) for all i, j.

12. The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13,…, where the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms. Write a program that computes and stores the Fibonacci sequence in an integer array F, such that F[i] will store the ith number in a Fibonacci sequence. The size of the array is an input parameter which is entered by the user.

13. Write a program that stores the following hourly rates in an array name hourly_rates: 9.5, 6.4, 12.5, 5.5, 10.5. Your program should also create two arrays named working_hours and wages, each capaple of storing five double-precision numbers. Using a for loop and a cin statement, have your program accept five user-input numbers into working_hours array when the program is run. Your program should store the product of the corresponding values in the hourly_rates and working_hours arrays in the wages array (for example, wages[1] = hourly_rate[1]*working_hours[1]) and display the output as a table consisting of three columns.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Programming fundamentals in c++. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10788/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Programming fundamentals in c++' conversation and receive update notifications?

Ask