1. Write a C++ program that converts US dollars to Canadian dollars. Here's the dialogue that should take place between the user and the machine when you run the program:
machine: This program converts US dollars to Canadian dollars.
machine: Enter the amount in US dollars:
user: x
machine: x US dollars is equal to y Canadian dollars.
NOTE: The program must handle ANY real value x as input, and output the correct converted amount. Look up the newspaper or the Web for the conversion rate.
2. Write a C++ program that asks when you are leaving for work and outputs an
appropriate message, according to the following rules:
a. If leaving before 6, ``Drive.''
b. If leaving between 6 and 8, ``Take a train.''
c. If leaving after 8, ``Take a bus.''
3. Write a C++ function that takes four arguments, H, C, r1 and r2, where
H is the total hours worked,
r1 is the hourly pay if you worked less than or equal to C hours and
r2 is the hourly pay for each hour above C. The function should
return the net pay. Use this function in a program that asks for
H, C, r1 and r2 as input and prints out the net pay.
TOPICS: FUNCTIONS, FUNCTION PROTOTYPES
4. Write a C++ program that asks the user ``How many scores
do you want to enter?''. For any integer response n,
the program should present n prompts of the form
``enter score 1:''
``enter score 2:''
............
``enter score n:''
The program should read in the n values and give the average score.
Remember, the program should work for any integer n.
TOPICS: ARRAYS, ARITHMETIC OPERATIONS
5. Write a function that accepts one, two or three arguments of type double and outputs their sum. Use this function to write a program that asks the user to input upto three numbers, and prints out their sum. The program should include a loop that lets the user repeat this computation for new input values until the user wants to end the program.
TOPICS: FUNCTION OVERLOADING, FUNCTION ARGUMENTS, ARGUMENTS by VALUE and ARGUMENTS by REFERENCE
6. Write a function with prototype void fun(double) that prints out an
appropriate message depending on the user-eneterd temperature:
greater than 90 : ``too hot''
75 to 89 : `` hot''
60 to 74 : ``mild''
35 to 60 : ``chilly''
less than 35 : ``cold''
What will happen if you entered a char for the temperature?
Now use function overloading to allow the user to input the temperature as well as a char, 'F' for Fahrenheit and 'C' for Celsius, and the function outputs the appropriate message.
TOPICS: FUNCTION OVERLOADING, FUNCTION ARGUMENTS, ARGUMENTS by VALUE and ARGUMENTS by REFERENCE
7. Suppose the interest rate on a loan goes up points
each year. Write a function rate(double& r) that increments the
interest rate for the next year. Use this function in a program that
receives user input for the principal
, initial interest rate
and the
loan period
and prints out the total due.
8. Consider the following program that gets three int values from the user and outputs them in ascending order. Write the required function prototypes and function definitions. Compile and run.
int main() { int a,b,c; PromptAndGet(a); //Get an int value, a, from the user PromptAndGet(b); PromptAndGet(c); SortAndPrint(a,b,c); // Sort the numbers in ascending order and print //Example: ``The numbers in ascending order are: 3, 7, 19'' }
TOPICS: FUNCTION OVERLOADING, FUNCTION ARGUMENTS, ARGUMENTS by VALUE and ARGUMENTS by REFERENCE
9. Write a C++ program that reads all the integers from a file ``int.txt'', stores them in an array called myintarray, and then prints the integers to the console in reverse order. (Hint: Use a for-loop on the array).
TOPICS: FILE I/O, DYNAMIC ARRAYS, MEMORY ALLOCATION
One possible Solution
Another possible Solution - uses dynamic arrays
10. Create a text file containing integers, letters and whitespaces (remember that whitespaces include space, tab and newline characters). Write a C++ program that replaces each white space by * and writes the result to another text file. The program should work for arbitrary input file lengths.
TOPICS: FILE I/O, WHITESPACES cctype.h and the older ctype.h library references
11. Write a C++ program that changes the first letter of each word in an input sentence to upper case (if they are not already upper case) and prints a period at the end of the last word unless there is already a period there.
TOPICS: STRING MANIPULATION
12. What is the following piece of code doing? Explain each line with a comment, indicating the values involved.
double *a, *b, m, n = 9.0; b=&n; m=n; *b=34.9; a=b; cout<<*a<<endl; cout<<*b<<endl;
TOPICS: POINTERS, POINTER ASSIGNMENTS