Introduction to Programming
for
Scientific Computing
These are hints for someone rusty in C/C++ programming who wants to
take the
Scientific Computing
course. Although C and C++
are quite different for some applications, this course
makes little use of the object oriented features unique to C++. The
reason to prefer C++ over C is that C++ is
strongly typed, so type errors are flagged at compile time, which can
save hours of debugging. Also, basic input and output may be simpler in
C++. The reason to prefer C is that most books start with C and get to
C++ only later. Also, formatting numerical output to line up in columns,
which is very desirable in numerical computing, is more cumbersome in
C++ than C. Take your pick.
First, you need access to a C/C++ compiler. I prefer a unix environment
where the text editor and compilor are separate programs and I have to
type "make ..." to compile. Others prefer integrated environments where the
editor and compiler are part of the same software package. For a Windows
box you can buy a compiler. I recommend Borland over Microsoft but there
is not much difference between them. Also, there are quite good
shareware compilers for Windows or Linux.
Next you need a book. I recommend "The C Programming Language" by
Kernighan and Ritchie. Another good choice is the "C Programming"
volume in the Schaum's outline series. For C++ there is the "C++
Primer" by Lippman, but you have to read part of Kernighan and Ritchie
first.
Finally, you need to get started. Here is a sequence of exercises that
you might try.
- Follow the instructions to write a program that prints:
(your name)
Scientific Computing
Courant Institute
- Write a program that prints a line:
Enter n, (enter 0 to stop):
then waits for you to type a number. If the number is a positive integer,
compute the sum of all the integers and print a line:
The sum of the first (n) integers is: (the sum)
If the user types 0, the program should type
Program ending.
If the user types a negative number, the program should type
The number you typed: (n), is negative. Please type another or 0 to stop:
The program should continue asking for n until you type 0 or ^C. This exercise
should teach you:
- Control structures: use a for loop and if - then - else or while.
- Error checking: never assume that a user parameter or a parameter from
a procedure is correct. Always test.
- Input and output formatting.
- Procedure calls. Write the program so that the actual sum is calculated by
a prodecure (int) sum( int n) .
- Write a program that reads a list of numbers from a file "smallNumbers" and writes
a file "bigNumbers". The number of numbers is n, which is not known by the program
in advance. The k-th number in "bigNumbers" is the sum numbers k through n in
"smallNumbers". The files "smallNumnbers" and "bigNumbers" should have one number
per line. The program will determine n by reading lines from "small numbers"
until there are no more numbers. Once all the "smallNumbers" numbers are read,
the program will do the adding and to get the "bigNumbers" and output the results.
Finally, use Matlab to plot the results. Start by creating a "smallNumbers"
file by hand with, say, 20 ones. This will teach you file IO, use of arrays,
and get you used to Matlab.