CS 102 Lecture Notes
Week One, Tuesday: Introduction
Review of Introduction to Programming
- Data types: int, float, char, etc.
- Decision and loop structures: if(), switch(), while(), for(), etc.
- Function calls and the procedural paradigm.
- Recursion.
- Arrays and strings.
- Pointers.
- Abstract data types and the C "struct" data structure.
- Data modeling and nested structs.
- File I/O.
Program Specification and Design
Basic design strategy:
- Specify the input and output.
- Design the algorithm and data structures.
- Translate the algorithm into a programming language such as C++.
- Test and debug the program.
The software life-cycle:
- Analysis and specification of the task.
- Design of the algorithms and data structures.
- Implementation (coding).
- Testing.
- Maintenance and evolution of the system.
- 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:
- You must know what output a correct program should produce for each test input.
- The test inputs should include those inputs that are most likely to cause errors.
Boundary values. Fully exercising code.
Debugging tips:
- Never start changing suspicious code on the hope that the change "might work better."
- Discover exactly why a test case is failing and limit your changes to corrections
of known errors.
- 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.