- Finish the recurse.cpp program. You will need to write the recurse.h file yourself (be sure it's in the include directory).
If you want to try an optional argument, write the prototype (in the .h) file following this basic syntax:
returnType functionName(type arg1, type optionArg = defaultValue);
Do not change the function header that I gave you in the .cpp file.
- Write a C++ program called guess. It should guess the number that the user is thinking
of.
First have it prompt the user for the upper range (N).
Next, write a recursive function called guessNumber to guess the number from 1-N,
using the binary search algorithm:
- guess the number at the middle of the range.
- if it matches, print the answer and return the number of tries it took.
- if it does not match:
- if the number is less than the guess, try the bottom half of the range.
- if the number is greater than the guess, try the top half of the range.
Here is a sample dialog:
I will guess a number from 1 to N. Please enter N: 10
Is it 5? Please enter y or n: n
Is it higher than 5? Please enter y or n: y
Is it 8? Please enter y or n: n
Is it higher than 8? Please enter y or n: n
Is it 6? Please enter y or n: y
I guessed your number in 3 tries!
- Write a C++ program called perms that implements the LinuxPermissions Java
program from HW1.
Note: use the C++ string class, and write and use the functions specified
in the Java write-up.
- Write a program called playStr that prompts the user for a string (until they
enter the word exit),
stores it into a character array,
and calls the following functions:
- print1(): takes a C-style string and prints the string
one letter per line, using a for-loop (counting loop) and array indices.
- print2(): takes a C-style string and prints the string
one letter per line, using a while loop that
uses array indices and looks for the EOS character.
- print3(): takes a C-style string and prints the string
one letter per line, using pointer arithmetic and a while loop that checks the contents of the
pointer for the EOS character.
- print4(): takes a string object and prints the string
one letter per line, using a for-loop (counting loop) and the method that take a substring.
Look up the C++ string class here:
cplusplus string class
Write and call the functions first, formating the output to match this sample for the string
"abc"; note that each entered string is numbered and each letter is numbered in the output:
String 1: abc
print1()
1. a
2. b
3. c
print2()
1. a
2. b
3. c
print3()
1. a
2. b
3. c
print4()
1. a
2. b
3. c
Next, look up how to compare two C-Style strings and add the input loop. Be sure to follow
the standard structure of a while loop that gets input.
After the end of the input loop, prompt for one more string, store it in an object of class
string and call the 4 functions with that object.
- Copy the readMatrix.c code to create a
new program called readFile that just reads the data and prints each line.
Take out all the references to the matrix and fix the bug related to reading the file.
- NOT assigned:
Setup the C program called readMatrix with readMatrix.c.
Add the code to print the rows and columns from main. Remember to use dot-notation.
- Write a C++ program called rentMovie that uses the Movie class to
create and print a few movies. This is hard-coded; NO prompting.
Add the code in Movie.cpp for the insertion (<<) function (the empty function is there)
to print the instance variables so you can use it in your main, something like this:
Movie m;
cout << m << endl;
- Write a C++ program called cardGame that uses the PlayCard and CardDeck classes.
- First create and print a few cards. Use the overloaded operators
to compare cards and print the results. Fix any errors in the operator functions.
Note: This first part is hard-coded; NO prompting.
- Next, create 2 cards and then prompt the user for the values by calling promptCard(). Print
which is the high card and which is the low card, something like this:
High Card: Ace of Diamonds
Low Card : 2 of Hearts
If the cards are equal, print something like this:
The Cards are Equal: Ace of Diamonds
Class Files:
- Complete the C++ program called testParams that we covered in lecture.