CSC138 Chapter 2 - 2Dv2.ppt - CSC138 Structured Programmi ng Topic 2 2D Array By Rashidah Mokhtar 0197333604 BK79 Content 2D array declaration and

CSC138 Chapter 2 - 2Dv2.ppt - CSC138 Structured Programmi...

This preview shows page 1 - 16 out of 61 pages.

CSC138: Structured Structured Programmi Programmi ng ng Topic 2: Topic 2: 2D Array 2D Array By: Rashidah Mokhtar 0197333604 BK79
Image of page 1

Subscribe to view the full document.

Content 2D array declaration and initialization Accessing and printing array components Lab 2.1 - Compile and execute simple programs using 2D array
Image of page 2
3 Two-dimensional Arrays // Declare array ref var elementType arrayName[rowSize][columnSize]; Example: int matrix[5][5]; double salary[100];
Image of page 3

Subscribe to view the full document.

Two-dimensional Array Illustration [0] [1] [2] [3] [4] [0] 7 [1] [2] [3] [4] matrix[2][1] = 7; int matrix[5][5]; 7 int array[][] = { {1, 2, 3}, {4, 5, 6 } , {7, 8, 9}, {10, 11, 12} }; 1 2 3 4 5 6 8 9 10 11 12 [0] [1] [2] [3] [4] [0] [1] [2] [3] [0] [1] [2] [3] [4] [0] [1] [2] [3]
Image of page 4
5 Declaring, Creating, and Initializing Using Shorthand Notations You can also use an array initializer to declare, create and initialize a two-dimensional array. For example, int array[4][3] = { { 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 }, { 10 , 11 , 12 } }; (a) Equivalent int array[ 4 ][ 3 ]; array[ 0 ][ 0 ] = 1 ; array[ 0 ][ 1 ] = 2 ; array[ 0 ][ 2 ] = 3 ; array[ 1 ][ 0 ] = 4 ; array[ 1 ][ 1 ] = 5 ; array[ 1 ][ 2 ] = 6 ; array[ 2 ][ 0 ] = 7 ; array[ 2 ][ 1 ] = 8 ; array[ 2 ][ 2 ] = 9 ; array[ 3 ][ 0 ] = 10 ; array[ 3 ][ 1 ] = 11 ; array[ 3 ][ 2 ] = 12 ; (b)
Image of page 5

Subscribe to view the full document.

6 Initializing Arrays with Random Values The following loop initializes the array with random values between 0 and 99 : for ( int row = 0; row < rowSize; row++) { for ( int col = 0; col < colSize; col++) { matrix[row][col] = rand() % 100; } }
Image of page 6
Input 2D array for ( int row = 0; row < rowSize; row++) { for ( int col = 0; col < colSize; col++) { cin>>arr2D[row][col]; } }
Image of page 7

Subscribe to view the full document.

8 Printing Arrays To print a two-dimensional array, you have to print each element in the array using a loop like the following: for ( int row = 0; row < rowSize; row++) { for (int col = 0; col < colSize; col++) { cout << matrix[row][col] << " "; } cout << endl; }
Image of page 8
Exercise 1. Declare a two-dimensional array name stockPrice with three rows and four columns. 2. Using for loop statement assign value in stockPrice for every each row and column as shown in table below: 3. Increase the value in stockPrice for every each column on each row as shown in table below: 2.2 2.2 2.2 2.2 4.4 4.4 4.4 4.4 6.6 6.6 6.6 6.6 2.2 3.2 4.2 5.2 4.4 5.4 6.4 7.4 6.6 7.6 8.6 9.6
Image of page 9

Subscribe to view the full document.

2D array operations using 7 basic algorithms: min, max, count, total, average, sort ( bubble), search (sequential) entire array (exclude sort) by row by column Lab 2.1 - Write programs using 2D array by applying the basic algorithms
Image of page 10
11 Summing All Elements To total all elements in the 2D array you need to use row and col for ( int i = 0; i < rowSize; i++) { for (int j = 0; j < colSize; j++) { sum = sum + matrix[ i ][ j ]; } }
Image of page 11

Subscribe to view the full document.

12 Summing Elements by Column For each column, use a variable named total to store its sum. Add each element in the column to total using a loop like this: for ( int j = 0; j < colSize; j++) { int total = 0; for ( int i = 0; i < rowSize; i++) total += matrix[i][j]; cout << "Sum for column " << i << " is " << total << endl; } 4 8 5 2 9 10
Image of page 12
Summing Elements by row for(int i=0; i<2; i++) { int totRow =0; for(int j =0; j<3; j++) totRow = totRow + age[i][j]; cout<<"\nSum by row "<<(i+1)<<":"<<totRow<<endl; } 4 8 12 1 5 6
Image of page 13

Subscribe to view the full document.

Minimum Identify the smallest value for valuePrice for column index 2 . double smallest = valuePrice[0][2]; for(int i =0 ; i <ROW; i++){ if(smallest > valuePrice[i][2]) smallest = valuePrice[i][2]; } cout<<"\nThe smallest value at column index 2 is : "<<smallest<<endl; 2.2 3.2 4.2 5.2 4.4 5.4 6.4 7.4 6.6 7.6 8.6 9.6
Image of page 14
Maximum Identify the highest value form the valuePrice.
Image of page 15

Subscribe to view the full document.

Image of page 16
  • Fall '18

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes