next up previous
Next: About this document ...

Worksheet 1 - PHY301, Fall 2006
Due Friday Sept. 8th, 5pm
Physics 301 home

C++ was developed by Bjarne Stroustrup of AT&T Bell Laboratories in the early 1980's, and is based on the C language. The name is a pun - "++" is a syntactic construct used in C (to increment a variable), and C++ is intended as an incremental improvement of C. Most of C is a subset of C++, so that most C programs can be compiled (i.e. converted into a series of low-level instructions that the computer can execute directly) using a C++ compiler. C is in many ways hard to categorise. Compared to assembly language it is high-level, but it nevertheless includes many low-level facilities to directly manipulate the computer's memory. It is therefore an excellent language for writing efficient "systems" programs. But for other types of programs, C code can be hard to understand, and C programs can therefore be particularly prone to certain types of error. The extra object-oriented facilities in C++ are partly included to overcome these shortcomings. There are literally hundreds of different computing languages, so that no-one knows them all. People tend to have a religous attachment to the language with which they are most familiar, leading to vacuous conflicts between opposing religious factions. Object oriented programming (OOP) discussions also tend to get heated and strewn with imprecise pontificating taking inordinate amounts of time. Nevertheless program design is very important. The larger the software project, and the more diverse the user community, the more important the design. Portability is also a major factor requiring considerable programming sophistication. Before worrying about these sophistications, we will go through some reasonably sophisticated computational tasks, by developing code that does not strictly comply with OOP and is not portable in the most rigorous sense. However as the course progresses the issues of OOP and portability will be introduced.

Though the list of possible programming languages is problematic, learning a new programming language in not a difficult task, once you have learned to program in one of them. In fact Mathematica is in many ways a programming language and many of the constructs used there appear in C,C++ and/or Fortran. In general Mathematica is less efficient for large-scale calculations, but it is very convenient and useful for smaller scale calculations and for checking the results found using your c++ codes. Another useful general purpose package is Matlab which is becoming more efficient at large scale numerical simulations, but is less useful for exact and algrebraic calculations.

For the first worksheet

Read the first lecture of the online course notes. Here are two examples which you can use as templates for this week's problems.

- 1. sample file 1 which prints out my name and ambition.

- 2. sample file 2 which squares the sin of an integer (i) and sums that over the first n integers, i=1,n. n is input from the terminal where the program executable is run.

PROBLEMS

- 1. Write a c++ program which inputs two integers from the screen. Then do the following to the two integers: Sum them, subtract them, multiply them and divide them. Then print out the two integers along with the results of the above operations. Make sure you also print out some text so you know what each output number means. Also print out a prompt so you know what to input.

- 2. Write a c++ code which calculates and prints out the sum of 1/k^2 for three cases: Where k runs over the first 3 positive integers; where k runs over the first 10 positive integers; where k runs over the first 100 positive integers. Check your answers by doing the same sums using Mathematica - also hand in the Mathematica code.

Linux commands you need to know

- (i) yppasswd - change your password (and remember it!!!)

- (ii) emacs - Use this to edit files.

- (iii) c++ or /usr/bin/c++ - Use this to compile your c++ program

e.g. c++ -o test.exe test.cc
This generates the executable and writes it onto test.exe. If you do not specify a destination file e.g. using just c++ test.cc The executable is written onto the file a.out.

- (iv) How to run an executable - Type ./test.exe (or ./a.out)



 
next up previous
Next: About this document ...
Phil Duxbury
2001-08-27