CS 102 Lecture Notes

Week One, Tuesday: Introduction

Review of Introduction to Programming

Program Specification and Design

Basic design strategy:

  1. Specify the input and output.
  2. Design the algorithm and data structures.
  3. Translate the algorithm into a programming language such as C++.
  4. Test and debug the program.

The software life-cycle:

  1. Analysis and specification of the task.
  2. Design of the algorithms and data structures.
  3. Implementation (coding).
  4. Testing.
  5. Maintenance and evolution of the system.
  6. Obsolescence.

Algorithm Analysis

The stair counting problem: three methods. How do we know which is best? Running time analysis. Results: quadratic time, linear time, logarithmic time.

Testing and Debugging

Properties of good test data:

  1. You must know what output a correct program should produce for each test input.
  2. The test inputs should include those inputs that are most likely to cause errors.
Boundary values. Fully exercising code.

Debugging tips:

  1. Never start changing suspicious code on the hope that the change "might work better."
  2. Discover exactly why a test case is failing and limit your changes to corrections of known errors.
  3. After correcting a known error, rerun all test cases (regression testing).

C vs C++

C is a subset of C++ but there are some features of C that are rendered somewhat "obsolete" by some new features of C++. We will encounter most of these in the coming weeks and discuss them in detail later. For now, the first difference you will probably notice is the new way for dealing with the operator console. Consider the following function in C:
void menu(char* cArray[], float sfArray[], int n)
{
  char cChoice = 0;
  puts("a. Sort by name.");
  puts("b. Sort by weight.\n\n");
  printf("Enter your choice (anything else to quit): ");
  fflush(stdin);
  scanf("%c", &cChoice);
  if (cChoice == 'a')
  {
    sortByName(cArray, sfArray, n);
    showElements(cArray, sfArray, n);
  }
  if (cChoice == 'b')
  {
    sortByWeight(cArray, sfArray, n);
    showElements(cArray, sfArray, n);
  }
}
In C++ the same function would be written:
void menu(char* cArray[], float sfArray[], int n)
{
  char cChoice = 0;
  cout << "a. Sort by name." << endl;
  cout << "b. Sort by weight." << endl << endl);
  cout << "Enter your choice (anything else to quit): ";
  cin >> cChoice;
  if (cChoice == 'a')
  {
    sortByName(cArray, sfArray, n);
    showElements(cArray, sfArray, n);
  }
  if (cChoice == 'b')
  {
    sortByWeight(cArray, sfArray, n);
    showElements(cArray, sfArray, n);
  }
}


This page established August 27, 1999; last updated January 7, 2000.