Purchase Solution

C++ Programming Questions

Not what you're looking for?

Ask Custom Question

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;
++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.

Purchase this Solution

Solution Summary

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

Purchase this Solution


Free BrainMass Quizzes
Basic Computer Terms

We use many basic terms like bit, pixel in our usual conversations about computers. Are we aware of what these mean? This little quiz is an attempt towards discovering that.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Word 2010: Tables

Have you never worked with Tables in Word 2010? Maybe it has been a while since you have used a Table in Word and you need to brush up on your skills. Several keywords and popular options are discussed as you go through this quiz.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

C++ Operators

This quiz tests a student's knowledge about C++ operators.