Introduction to C++
Jonah Warrenjonah AT parsons DOT edu
http://www.feedtank.com/2005/cpp
Homework 4
(Please email me if you have any questions.)- Skim over Chapter 5. Make sure it all makes sense to you.
- Read Chapter 6. We should have gone over this in class today.
- Review class code here.
- Create a program that asks the users for two numbers. Your program should then
add together all of the integers that fall between those two integers as well as those integers
themselves. Print out this sum.
Here is an example output:
Enter a start #: 3 Enter an end #: 7 The sum of #s between 3 and 7 is: 25
- Create a program that uses nested for loops to make your own unique pattern.
Use, if... else ifs. Play around.
Here is a very simple example using only if...else:
#include <iostream> int main() { using namespace std; const int dim = 20; for(int y=0;y<dim;y++) { for(int x=0;x<dim;x++) { if ((x+y)%6 == 0) cout << "."; else cout << "*"; } cout << endl; } }
- Create a program that takes in a word from the user that consists of lowercase and
uppercase characters. Have the program print out that same word with the capitalization reversed.
That is, all uppercase letters should be lowercase, and all lowercase letters should be uppercase.
Take a look at this chart.
Here is an example output:
Give me a word: PARSONSdesignANDtechnology Your word with the letters reversed is: parsonsDESIGNandTECHNOLOGY
BONUS!: Alter this program so that it reads in an entire line (with spaces), not just a word. Look up how to use cin.get() in the book to do this.
- Create a program that allows the user to play a simple ASCII game. The board should
be a matrix. In the example below, the matrix is 5x5. Make it so there are two objects located in the
matrix, the player (below represented by a 'o') and the goal (below represented by a 'g'). Repeatedly
ask the user to enter a direction to move the player (either 'n', 's', 'e', or 'w'). After the user
enters a direction, move the player one spot in that direction, and reprint the board. Have the game
end when the user reaches the goal. For your first version, it is OK if the player can move off the matrix.
The starting position for the player and goal will be hardcoded (will never change).
Here is an example output:
The current game board is: ..... ....o ...g. ..... ..... Which way (type 'n','s','e' or 'w'):w The current game board is: ..... ...o. ...g. ..... ..... Which way (type 'n','s','e' or 'w'):w The current game board is: ..... ..o.. ...g. ..... ..... Which way (type 'n','s','e' or 'w'):w The current game board is: ..... .o... ...g. ..... ..... Which way (type 'n','s','e' or 'w'):s The current game board is: ..... ..... .o.g. ..... ..... Which way (type 'n','s','e' or 'w'):e The current game board is: ..... ..... ..og. ..... ..... Which way (type 'n','s','e' or 'w'):e You got it!!
Add additional features to this game. Some suggestions you could implement:- Check to see that when the user moves in a given direction, the player is still within the matrix. If the player were to move off the matrix in the next move, print out an error message and keep the player in the same spot.
- Allow the user to type in 'q' to quit the program.
- Create another spot in the matrix that the player must avoid.
- Whatever you want!
- Zip up questions 4, 5, 6 and 7. Post them to your website.