2-D Arrays

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

2-Dimentional arrays

2-Dimentional arrays
 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.

 Declaration of 2-D array

 Data type variable_name[Row_size][Col_size];

Eg: int a[5][4]; // 5 Rows and 4 columns


Initialization of 2D Array

There are two ways to initialize a two Dimensional arrays during declaration.

int disp[2][4] = { {10, 11, 12, 13}, {14, 15, 16, 17} };

OR

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Although both the above declarations are valid, I recommend you to use the first method as it

is more readable, because you can visualize the rows and columns of 2d array in this method.
Initialization of 2D array using Loops
We can use any C loop to initialize each member of a 2D array one by one as shown in the
below example.
Example:
int x[3][4];
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 4; j++)
{
scanf(“%d”,a[i][j];
}
}
Accessing 2D array elements using Loops
#include <stdio.h>
void main(void)
{
// an array with 3 rows and 2 columns.
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// output each array element's value
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
printf("Element at x[%d][%d]: ", i, j);
printf("%d\n", x[i][j]);
}
}}
Accessing 2D array elements using Loops
#include <stdio.h>
void main(void)
{
// an array with 3 rows and 2 columns.
int x[3][2] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };
// output each array element's value
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
printf("Element at x[%d][%d]: ", i, j);
printf("%d\n", x[i][j]);
}
}
}
Multi-Dimensional arrays
Lab1: Date Lab2: Date
Program1 Program1
Title Title
Aim Aim
Algorithm / Pseudocode Algorithm / Pseudocode
Program Program
Output Screen shot Output Screen shot

Program2 Program2
Title Title
Aim Aim
Algorithm / Pseudocode Algorithm / Pseudocode
Program Program
Output Screen shot Output Screen shot
2-D arrays - Addition
Given two N x M matrices. Find a N x M matrix as the sum of given matrices each value at the sum of values of
corresponding elements of the given two matrices.
2-D arrays - Addition
Read a[ ] [ ]
Read b[ ] [ ]
i=0
Until(i<n)
{
j=0
until(j<n)
{
c[i][j] = a[i][j]+b[i][j]
j=j+1
}
i=i+1
}
Print c[ ][ ]
2-D arrays - Addition
#include <stdio.h> // adding two matrices
int main() { for (i = 0; i < r; ++i)
int r, c, a[100][100], b[100][100], sum[100][100], i, j; for (j = 0; j < c; ++j) {
printf("Enter the number of rows (between 1 and 100): "); sum[i][j] = a[i][j] + b[i][j];
scanf("%d", &r); }
printf("Enter the number of columns (between 1 and 100): "); // printing the result
scanf("%d", &c); printf("\nSum of two matrices: \n");
// Read matrix1 for (i = 0; i < r; ++i)
printf("\nEnter elements of 1st matrix:\n"); for (j = 0; j < c; ++j) {
for (i = 0; i < r; ++i) printf("%d ", sum[i][j]);
for (j = 0; j < c; ++j) { if (j == c - 1) {
printf("Enter element a%d%d: ", i + 1, j + 1); printf("\n\n");
scanf("%d", &a[i][j]); } }}
}
// Read matrix2
printf("Enter elements of 2nd matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
2-D arrays – Linear Searching
printf("Enter the item to find: ");
#include<stdio.h>
scanf("%d", &item);
int main(){
for(int i=0; i<m; i++)
int m, n, item, count=0, array[10][10];
{
for(int j=0; j<n; j++)
printf("Enter the number of rows and columns: ");
{
scanf("%d %d", &m, &n);
if(array[i][j] == item)
{
printf("Enter %d elements: ", (m*n));
printf("Item found at [%d, %d] \n", i, j);
for(int i=0; i<m; i++){
count++;
for(int j=0; j<n; j++){
}}}
scanf("%d", &array[i][j]);
if(count==0)
}
printf("Item Not found");
}
}
2-D arrays – Finding Row and Column Sum
2-D arrays – Finding Row and Column Sum
Algorithm:
1.Start
2.Declare a 2-D array i.e., an M*N matrix.
3.Initialize the array using two for loops.
4.Declare two variables that will store the row and column sum.
5.Now to calculate the row sum use a nested loop.
6.Keep the first index of the matrix constant and increment the second index to access each element of the
row.
7.Keep on adding these elements and display the result after coming out of the inner loop.
8.Now to calculate the column sum again using the nested loop.
9.This time increment the first index of the matrix and keep the second index of the matrix constant to
access each element of the column.
10.Keep on adding these elements and display the result after coming out of the nested loop.
11.Stop.
2-D arrays – Finding Row and Column Sum
#include <stdio.h>
int main() printf("\nRow Sum....\n"); // Row sum
{ for(int i=0;i<m;i++)
int m,n; //Row Column Declaration {
printf("Enter the number of rows and column\n"); int rsum=0;
scanf("%d %d",&m,&n); //Row Column Initialization for(int j=0;j<n;j++)
int arr[m][n]; //Matrix Declaration {
printf("Enter the elements of the matrix\n"); rsum=rsum+arr[i][j];
for(int i=0;i<m;i++) //Matrix Initialization }
{ printf("\nSum of all the elements in row %d is %d\n",i,rsum);
for(int j=0;j<n;j++) }
{ printf("\nColumn Sum....\n"); // Col sum
scanf("%d",&arr[i][j]); for(int i=0;i<m;i++)
} {
} int csum=0;
printf("\nElements in the matrix are \n"); for(int j=0;j<n;j++)
for(int i=0;i<m;i++) //Print Matrix {
{ csum=csum+arr[j][i];
for(int j=0;j<n;j++) }
{ printf("\nSum of all the elements in column %d is %d\n",i,csum);
printf("%d ",arr[i][j]); }
} }
printf("\n");
}
2-D arrays – Finding sum of diagonals of a matrix
#include <stdio.h> for (i = 0; i < m; ++i) {
int main()
{
// calculating the main diagonal sum
int i, j, m = 3, n = 3, a = 0, sum = 0; sum = sum + matrix[i][i];
int matrix[3][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
// if both rows and columns are equal then it is // calculating the off diagonal sum
// possible to calculate diagonal sum
if (m == n) a = a + matrix[i][m - i - 1];
{ // printing the input matrix
printf("The matrix is \n"); }
for (i = 0; i < m; ++i) {
// iterates number of columns // printing the result
for (j = 0; j < n; ++j) { printf("\nMain diagonal elements sum is = %d\n", sum);
printf(" %d", matrix[i][j]); printf("Off-diagonal elements sum is = %d\n", a);
} }
printf("\n"); else
} // if both rows and columns are not equal then it is
// not possible to calculate the sum
printf("not a square matrix\n");
}

You might also like