{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Cpp_notes_summary_4

# Cpp_notes_summary_4 - Chapter 4 Arrays Skipped enumerated...

This preview shows pages 1–3. Sign up to view the full content.

Chapter 4 Arrays Skipped enumerated type 4.1 Arrays #include < iostream > using namespace std ; int main () { int array1 [3]= { 3 ,2 ,1 } ; cout << array1 [0] << ” , << array1 [2] << endl ; int array2 [ 3 ] ; array2 [0]=1; array2 [1]=2; array2 [2]=3; int array3 [2][2]= {{ 1 ,2 } , { 3 ,4 }} ; cout << array3 [0][0] < < ” , << array3 [1][0] < < endl ; int array4 [ 3 ] [ 4 ] ; for ( int i =0; i < 3; i++) { for ( int j =0; j < 4; j++) { array4 [ i ] [ j ]= i j 1; } } return 0; } #include < iostream > using namespace std ; 13

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
MAT 5939-04 Pierre Garreau void printarray ( int A[ ] , int N) { for ( int i =0; i < N 1; i++) { cout << ”A[ ” << i << ”]=” << A[ i ] << ” , ” ; } cout << ”A[ ” << N 1 << ”]=” << A[N 1] << endl ; } int main () { int array1 [4]= { 1 ,2 ,3 ,4 } ; printarray ( array1 , 4 ) ; return 0; } Arrays are always passed as reference, so watch out you can change their value on purpose or by accident. Also notice that since the arrays start at zero these formulae look strange. If you are using arrays and your code doesn’t work the very first thing you should do is check your indexing. 9 times out of 10 you screwed it up at least once. #include < math . h > #include < iostream > using namespace std ; void cross product ( double A[ 3 ] , double B[ 3 ] , double cross [ 3 ] ) { cross [0]=(A[ 1 ] B[2] A[ 2 ] B [ 1 ] ) ; cross [1]= (A[ 2 ] B[0] A[ 0 ] B [ 2 ] ) ; cross [2]=(A[ 0 ] B[1] A[ 1 ] B [ 0 ] ) ; } int main () { double X[ 3 ] = { 1 . , 2 . , 3 . } ; double Y[ 3 ] = { 4 . , 5 . , 6 . } ; double Z [ 3 ] = { 7 . , 8 . , 9 . } ; cross product (X,Y, Z ) ; cout << Z[0] << ” , << Z[1] << ” , << Z[2] << endl ; return 0; } 4.2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 6

Cpp_notes_summary_4 - Chapter 4 Arrays Skipped enumerated...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online