Table Of Contents
Previous Section Next Section

Chapter 2: Starting Out on the Wrong Foot

We were all novice programmers once. Back then, we would struggle for hours to get the simplest program to compile. But we were young and foolish and made a lot of stupid mistakes. Now we are professional programmers and we don't make dumb mistakes. We make smart ones (but we call them "professional errors").

In this chapter we present a number of programs designed to remind of your early programming mistakes, thus letting you relive experiences that you might rather forget.

Program 4: Typical Initial Problem

A classic mathematical problem is to add the numbers 1 to 100. But this program seems come up with the wrong answer:

  1 /************************************************
  2  * A program to sum the numbers from 1 to 100   *
  3  * using a brute force algorithm.               *
  4  ************************************************/
  5 #include <iostream>
  6
  7 int main()
  8 {
  9     int sum;    // The running sum
 10     int count;  // The current number
 11
 12     for (count = 1; count <= 100; ++count)
 13         sum += count;
 14
 15     std::cout <<
 16         "The sum of the numbers " <<
 17         "between 1 and 100 is " <<
 18         sum << '\n';
 19     return (0);
 20 }

(Next Hint 116. Answer 51.)

Table Of Contents
Previous Section Next Section