Explore BrainMass

C++ Programming Questions

1. Using the switch statement, write a program that will request an integer from the user in the range 1 - 5. It should then output a message in this form:
You entered the number one.
{or two, or whatever was entered}
If the user entered a number other than 1 - 5, the program should output a message to that effect. Note that you need to read the value as an int, not a char. Use the form cin >> x; where x is an int variable, not cin.get().

2. Rewrite the following code with a for loop:

int inputNum;
cin >> inputNum;
Value = inputNum;
while (Value <= 10) {
cout << Value;

3. This is a two-part question.
a. Rewrite the code in the previous question using a do-while loop.
b. Under what circumstances will the do-while loop you wrote act differently than the while and for versions of this code?

4. A certain company sells three items.
Tacos: $1.67 each
Golf Bags: $158.99 each
Masking Tape Roll: $5 each
Write a program that asks the user to select one of the items from a menu (1 for tacos, 2 for golf bags, 3 for masking tape), then to enter a number to buy. The program computes the cost of the purchase, requests another item selection from the user, and so on. The user enters 0 from the item menu to quit. Once 0 has been entered, the program displays the total cost of goods purchased.

5. In a certain company, employees are paid extra for overtime using the following system. For every hour over 40 worked, the employee is paid 1.5 times hourly wages. For every hour over 50, the employee is paid 2 times hourly wages. For example, an employee who earns $10 an hour and works for 55 hours would receive:
Regular pay: 40 hrs x $10 = $400
Time-and-a-half: 10 hrs x 1.5 x $10 = $150
Double: 5 hrs x 2 x $10 = $100
Total: = $650
Write a program that prompts for the number of hours worked in a week and an hourly wage, and then calculates and displays the weekly pay.
The program should then ask the user if there is more data (have the user enter `y' for yes). If so, the process begins again with the prompt for hours worked.

7. Write a program that reads two floating-point values representing angles and displays a message stating which angle has the greater tangent value. Note that the trigonometric functions in the math library expect angles in radians, not degrees. 360 degrees = 2 x pi radians, so 45 degrees would be about 0.785 radians.

8. What is the output of the following program? Explain.
#include <iostream>
using namespace std;
void f(int i, int j) {
i = 5;
j = j + i;
cout << "f: i = " << i << endl;
cout << "f: j = " << j << endl;
int main () {
int i = 15;
int j = 30;
f(i, j);
cout << "main: i = " << i << endl;
cout << " main: j = " << j << endl;

9. Write a float function GetRadius() that prompts the user for a radius, extracts the user's response from cin, and then returns the response as its value. Note: in questions of this type, you should write a simple program to test your code. All you need in this case is a main function that calls your function with appropriate arguments.

10. Create an enumerated type with three values: POSITIVE, NEGATIVE, and ZERO. Write a function that prompts the user for an integer, reads the integer, and returns one of the enumerated values based on that number (e.g., returns NEGATIVE if the input number is < 0.). Write a main function that calls this function, and displays, using a switch statement, what category of number was entered. Example:
Please enter an integer: 6
The number you entered was positive.

11. This is a two-part question.
a. Write a function for the following formula.
Distance: computes the distance d traveled in t seconds by an object that started at rest and then accelerated at a meters per second per second.
d = at^2 /2
b. Briefly discuss your choice of parameters and their types.


Solution Summary

This solution provides a detailed, step by step explanation of the given programming questions.