Two-Dimensional Arrays in C Programming PDF

Document Details

ComelyMesa

Uploaded by ComelyMesa

جامعة أكتوبر التكنولوجية

Tags

C programming two-dimensional arrays arrays programming

Summary

This document explains how to work with two-dimensional arrays in C programming and provides examples for different scenarios. The document introduces the concept, how to initialize a 2D array and how to access elements.

Full Transcript

C Multidimensional Arrays A multidimensional array is basically an array of arrays. Arrays can have any number of dimensions., we will introduce the most common; two-dimensional arrays (2D). Two-Dimensional Arrays A 2D array is also known as a matrix (a table of rows and columns). To create a 2D...

C Multidimensional Arrays A multidimensional array is basically an array of arrays. Arrays can have any number of dimensions., we will introduce the most common; two-dimensional arrays (2D). Two-Dimensional Arrays A 2D array is also known as a matrix (a table of rows and columns). To create a 2D array of integers, take a look at the following example: int matrix = { {1, 4, 2}, {3, 6, 8} }; The first dimension represents the number of rows , while the second dimension represents the number of columns. The values are placed in row-order, and can be visualized like this: You must always specify the second dimension even if you are specifying elements during the declaration. Let’s understand this with the help of few examples – int abc = {1, 2, 3 ,4 } int abc[] = {1, 2, 3 ,4 } int abc[][] = {1, 2, 3 ,4 } int abc[] = {1, 2, 3 ,4 } Initialization of Two-Dimensional Arrays There are two methods to initialize two-dimensional arrays. Method 1 int multi_dim={10,20,30,40,50,60,20,80,90,100,110,120}; Method 2 int multi_dim={{10,20,30},{40.50,60},{70,80,90},{100,110,120}}; Access the Elements of a 2D Array To access an element of a two-dimensional array, you must specify the index number of both the row and column. Accessing two-dimensional arrays can be done using row index value and column index value. Name_of_the arrays[row_index][column_index]; This statement accesses the value of the element in the first row (0) and third column (2) of the matrix array. Example int matrix = { {1, 4, 2}, {3, 6, 8} }; printf("%d", matrix); // Outputs 2 Change Elements in a 2D Array To change the value of an element, refer to the index number of the element in each of the dimensions: The following example will change the value of the element in the first row (0) and first column (0): Example int matrix = { {1, 4, 2}, {3, 6, 8} }; matrix = 9; printf("%d", matrix); // Now outputs 9 instead of 1 Loop Through a 2D Array To loop through a multi-dimensional array, you need one loop for each of the array's dimensions. The following example outputs all elements in the matrix array: Example #include int main() { int matrix = { {1, 4, 2}, {3, 6, 8} }; int i, j; for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { printf("%d\n", matrix[i][j]); } } return 0;} EXAMPLE: Find the average rating by the reviewer in row 2. int sum = 0; for (int col = 0; col

Use Quizgecko on...
Browser
Browser