Computational Physics wiki (UCAM only) / C++ course homepage /

Simple C++ examples

What you want What it is called Examples
outputcout AddTwoNumbers.cc
Hello.cc
loopsfor HelloFor.cc
ForStatistics.cc
loops do .. while BiggestInt.cc
loopswhile twoPower.cc (has never-ending loop)
AddWhilePositive.cc
input from usercin HelloIn.cc
RandDemo2.cc
twoPower.cc
function FunctionFactorial.cc
HexagonValue.cc
RandDemo7.cc
definition #define RandDemo2.cc
pass by reference& SortReference.cc
SortReference2.cc
shared definitions#include (header file) RandDemo2.cc and rand.h
conditional if / else
HelloFor.cc
RandDemo5.cc
? (ternary operator) RandDemo2.cc
switch / case SwitchDemo.cc
CalculatorSwitch.cc
random random() (random integer between 0 and 231
RollDie.cc
RandDemo5.cc
RandDemo6.cc
srandom() (initialize/seed the generator) RandDemo2.cc (seeds using time)
RandDemo5.cc (user-supplied seed)
input from command lineargv, sscanf RandDemo4.cc
convert string to int or long intsscanf RandDemo4.cc
integer arithmetic unsigned int BiggestUnInt.cc
power (xy)pow(x,y) twoPower.cc
bit-shift (1<<i) BiggestUnInt2.cc
Examine memory bit by bit& (bit mask) BitsOut.cc
BiggestUnInt2.cc
input from fileifstream, open, getline, close ReadFile.cc
OpenFile.cc
remember variablesstatic StaticDemo.cc
arraysfloat vector[3] ScalarProduct.cc
char phone[11] CharacterArray.cc
dynamically allocate memory for vectors/ arrays/ matricesint *a, new, delete ArrayDemo1.cc
ArrayDemo2.cc
ArrayDemo3.cc
dynamically allocate double arrays/matricesdouble *a, new, delete ArrayDemo4.cc
useful warnings when compilingmakefile Makefile
Classes class SimpleClass.cc
RandDemo3.cc
convert between typescast, reinterpret_cast, static_cast CastDemo.cc (not yet finished)
The archetypical "Hello World" example program demonstrates output (printing) to the terminal. int main(), std::cout ex-01-01-hello-world.cc
Acquiring random integers. random() ex-01-02-rand.cc
Ensuring random numbers differ each time you run the program srand() ex-01-03-rand.cc
Getting random real in the range [0,1) random(), RAND_MAX ex-01-04-uniform.cc
Example Monte Carlo calculation of Pi ex-01-05-pi.cc
Rounding error ex-02-01-reciprocal-innacuracy.cc
Rounding error ex-02-02-walking-ants.cc
Overflow error ex-02-03b-redoubling-all.cc
Overflow error ex-02-03-redoubling-ints.cc
Overflow error ex-02-04-doubling-random-ints.cc
ex-02-05-explore-mult.cc
Underflow error ex-02-06-invariant-under-increment.cc
Scope examples { and } ex-02-07-scope.cc
Program that tests if numbers are prime ex-02-08-primes.cc
A "monolithic" implementation of the "Three Map" map. ex-03-01-threemap.cc
ex-03-02-squares.cc
The 'evil' statement gotoex-03-03-goto.cc
Simple functions functions ex-04-01-simpleFuncs.cc
Functions with arguments functions ex-04-02-funcsWithArguments.cc
Illustrate use (and misuse) of the stack. the stack ex-04-03-stack.cc
A better (more factorised) implementation of the "Three Map" map ex-04-04-returnToThreeMap.cc
An extension to the map program to allow multiple maps to be used function pointers ex-04-05-otherMaps.cc
Crudly decompose functions using Fourier Transforms, and use these to do fractional derivatives. ex-04-08-ft.cc
Why arrays are confusing ex-05-arraysConfusion.cc
Use arrays to rotate an image ex-05-imageTool.cc
Peer into memory ex-05-memoryPeekPoke.cc
Do naughty things with pointers ex-05-naughtyPointerArithmetic.cc
Illustrate pointer usage ex-05-pointer.cc
Play with sizeof sizeof ex-05-sizeof.cc
Find the Fourier Series of some simple functions, differentiate the Fourier Series a non-integer number of times. Use Gnuplot to show the result. sin cos gnuplot FourierSeriesNew2011.cc