Course Web Pages
For loops, strings and arrays
The videos below demonstrate the use of for loops, strings, and arrays in a C++ program with examples from Dawson's Beginning C++ book.
The video above discusses strings as defined by the string object. Another kind of string that you will frequently encounter in the C++ world is the C-style character string.
See C++ Strings for a discussion of C-style character strings.
In C++, you can also create multi-dimensional arrays, but there's a difference between multi-dimensional arrays in C# and C++. Compare this discussion of multi-dimensional arrays in C# with this discussion of multi-dimensional arrays in C++
To help your understanding, it is useful to download the code that goes with each video. Run it and make sure you understand how it works. Then try changing it, and test to make sure that your change works.
When changing code, the generally accepted practice among professional programmers is to:
There are many books and web sites dedicated to discussions of the development process, but for now it's enough to think about how you might apply the three steps above to your programming. If you're interested in views on development in the wider community, see Extreme Programming, Refactoring and the Agile Alliance.
Add Scoring to Word Jumble
The following exercise is from Beginning C++ Through Game Programming by Michael Dawson.
Improve the Word Jumble game by adding a scoring system. Make the point value for a word based on its length. Deduct points if the player asks for a hint.
Factorials Gone Wrong
This section focuses on debugging programs. We will start off with a simple factorial program and do a step by step troubleshooting routine.
1. Writing the factorial program
Here is the code for a factorial program. Copy it into your IDE (Visual C++) and verify that it compiles.
What do you get when you enter the following values: 0, 1, 2, 9, 10?
2. Breaking the program
Run the program and enter -1. What happens? How can you change the code such that it won't exhibit this behavior?
3. Breaking the program II
Try entering some larger numbers. What is the minimum number for which your modified program from 4.2 stops working properly? (You shouldn't have to go past about 25. You may find Google's built-in calculator useful in checking factorials.) Can you explain what has happened?
4. Rewriting Factorial
Modify the given code such that negative inputs do not break the program and the smallest number that broke the program before now works. Do not hardcode answers.
5. Rewriting Factorial II
Since we know that only a small number of inputs produce valid outputs, we can alternatively hardcode the factorials of these inputs. Rewrite the program from the previous part ("Rewriting Factorial") using a switch statement to demonstrate for inputs up to 10 how you would do this. (Of course, the code for inputs above 10 would basically be the same, but you do not need to go through the work of finding all those large factorials.)
6. Further testing
Are there any other inputs we have to consider? Why or why not?