🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

Lecture #3_CCS 104 -Two-Dimensional Array.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

2-Dimensional Array 1 Copyright © 2022 HNU-Department of Computer Studies CCS 104 Two-Dimension al Array CCS 104 2-Dimensional Array Introduction An array of arrays is known as 2D array. The two dimensional (2D) array in C progra...

2-Dimensional Array 1 Copyright © 2022 HNU-Department of Computer Studies CCS 104 Two-Dimension al Array CCS 104 2-Dimensional Array Introduction An array of arrays is known as 2D array. The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns. Example: numbers columns 2 Dimension/size: 5X3 rows CCS 104 Copyright © 2022 HNU-Department of Computer Studies 2-Dimensional Array Introduction Declaration: data type array name 012 [row-size][column-size] 012 Example: 0 1 2 int numbers 3 4 1 2 3 0 4 Accessing each element: array name [row-index][column-index] CCS 104 3 Copyright © 2022 HNU-Department of Computer Studies 2-Dimensional Array Introduction Accessing each element: array name [row-index][column-index] 7 Example1: 9 0 numbers = 1; 012 3 numbers = 9; 1 numbers = 3; 4 0 2 012 numbers = 5; 1 1 numbers = 7; 2 3 3 5 4 Example2: Displaying the contents of a 2D array 4 for(row = 0; row < 5; row++){ for(col = 0; col < 3; col++){ printf("%d\t", arr2D[row][col]); if(col == 2) printf("\n"); } } CCS 104 Copyright © 2022 HNU-Department of Computer Studies 2-Dimensional Array Introduction Initializing a 2D array: data type array name [row-size][column-size] = {{elements is row1}, {elements in row 2}, ….{elements in row n}} Example1: int array_2D = {{4, 6}, {2, 0}, {7, 5}}; 0 1 2 0 2 7 5 Problem: array_2D 01 4 6 Copyright © 2022 HNU-Department of Computer Studies Write a program that initializes a 3 X 2 array (1st row: 4, 6; 2nd row: 2, 0; 3rd row: 7, 5). The program should display all the contents of the array. It should display the square of each element of the array. CCS 104 5 2-Dimensional Array Introduction Problem: Write a program that initializes a 3 X 2 array (1st row: 4, 6; 2nd row: 2, 0; 3rd row: 7, 5). The program should display all the contents of the array. It should display the square of each element of the array. Outputs: Resulting Array: 012 01 4 6 2 0 7 5 All elements: 4 6 20 75 6 Square of all elements: 16 36 40 49 25 CCS 104 Copyright © 2022 HNU-Department of Computer Studies IntroductionSolution: 2-Dimensional Array 7 Copyright © 2022 HNU-Department of Computer Studies CCS 104 2-Dimensional Array Activity (Think-Pair-Share) Solve the following problem: Write a program that initializes a 3 X 4 array with integers (it is up to you what values to include). The program should perform the following tasks: a) Displays all elements b) Asks the user to input a row and the program displays all the elements of that row c) Asks the user to input a column and the program displays all the elements of that column d) Computes and displays the sum of all the numbers and the average of all the numbers. Studies 8 CCS 104 Copyright © 2022 HNU-Department of Computer 2-Dimensional Array Activity (Think-Pair-Share) Illustration: Initial values: All elements: Row #: 2 3, 2, 1, 6, 0123 0 3 2 1 6 8, 9, 7, 5, 1 8 9 7 5 4, 2, 1, 8 4 2 1 8 2 Column #: 4 Sum: 56 6 outputs: 3 2 1 6 Average: 4.67 8 9 7 5 8975 5 4218 8 9 CCS 104 Copyright © 2022 HNU-Department of Computer Studies 2-Dimensional Array "Alone we can do so little; together we can do so much." Helen Keller https://blog.hubspot.com/marketing/teamwork-quotes 10 Copyright © 2022 HNU-Department of Computer Studies CCS 104

Use Quizgecko on...
Browser
Browser