Course Web Pages

Related Links

Free eBooks


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. 

for loops

 counter.cpp


strings

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.

 string_tester.cpp


arrays

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

 heros_inventory.cpp


for each

 for_each.cpp


Word Jumble

 word_jumble.cpp


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.

Coding tip

When changing code, the generally accepted practice among professional programmers is to:

  • Make sure the code works correctly "as is" by developing and running appropriate tests. 
  • Make each change to your code as small as possible.
  • Thoroughly test each small change to make sure it works and make sure you haven't broken anything.

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.

Exercises

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 problem is from an MIT course made available through MIT's OpenCourseware

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.

code from mit problem 

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?