13.07.2015 Views

BABAR C++ Course Running the Examples - HEPHY

BABAR C++ Course Running the Examples - HEPHY

BABAR C++ Course Running the Examples - HEPHY

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ArraysExample Code and a TestA collection of elements of same typefloat x[100]; // like REAL*4 X(100) in F7• first element of array is x[0]• last element of array is x[99]Initializing array elementsfloat x[3] = {1.1, 2.2, 3.3};float y[] = {1.1, 2.2, 3.3, 4.4};• can let <strong>the</strong> compiler calculate <strong>the</strong> dimensionMulti-dimensions arraysfloat m[4][4]; // like REAL*4 M(4,4) in F7int m [2][3] = { {1,2,3}{4,5,6} };• elements appear row-wise• Fortran elements appear column-wise• Thus m[0][1] in C/<strong>C++</strong> is M(2,1) in FortranMultiplying matrices (ch2/mat3by3.C)float m[3][3], m1[3][3], m2[3][3];// Code that initializes m1 and m2 ...// m = m1 * m2double sum;for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {sum = 0.0;for (int k = 0; k < 3; k++) {sum += m1[i][k] * m2[k][j];}m[i][j] = sum;}}• If you understand this code, <strong>the</strong>n you know enoughC/<strong>C++</strong> to code <strong>the</strong> algorithmic part of your code• At <strong>the</strong> beginning of this session, <strong>the</strong> above codewould probably have been gibberish• If you can not understand this code, <strong>the</strong>n I’m goingtoo fast :-(• royal pain to interface C/<strong>C++</strong> with Fortran<strong>BABAR</strong> <strong>C++</strong> <strong>Course</strong> 29 Paul F. Kunz<strong>BABAR</strong> <strong>C++</strong> <strong>Course</strong> 30 Paul F. Kunz

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!