0% found this document useful (0 votes)
54 views

Two-Dimensional Arrays

The document discusses two-dimensional and three-dimensional arrays in C programming. It defines two-dimensional arrays as tables with rows and columns that can be accessed using two indexes, describes how to declare and initialize 2D arrays, and provides examples of initializing 2D arrays and accessing elements. It also demonstrates how to declare and initialize a three-dimensional array, defining the indexes for rows, columns, and third dimension. Code examples are given for initializing and displaying 2D and 3D arrays.

Uploaded by

Irfan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Two-Dimensional Arrays

The document discusses two-dimensional and three-dimensional arrays in C programming. It defines two-dimensional arrays as tables with rows and columns that can be accessed using two indexes, describes how to declare and initialize 2D arrays, and provides examples of initializing 2D arrays and accessing elements. It also demonstrates how to declare and initialize a three-dimensional array, defining the indexes for rows, columns, and third dimension. Code examples are given for initializing and displaying 2D and 3D arrays.

Uploaded by

Irfan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Two-Dimensional Arrays

Lecture 7

1
Contents:
 Declaring, initializing 2-dimensional array.
 Declaration and Initialization of a three dimensional array.

2
Two dimensional array can be considered as a table
that consists of rows and columns .
Each element in 2-d array is referred with the help of
two indexes.
One index is used to indicate the row and the second
index indicates the column of the element.

3
Data_type identifier[Rows][cols];
Identifier indicate the name of array
Rows indicates the number of rows in the array.
Cols indicates the number of columns in the array.

Int arr[4][3];

This example shows an array with 4 rows and 3


columns

4
Declaring and & Initializing 2D Array
Similar to a 1D array, the elements of a 2D array can be initialized either
one at a time or all at once.
Syntax
Data_type arrName[size1][size2] = {{value1, value2, value3, …},
{value1, value2, value3, …},…..}
Int myArray[2][3] = {{10, 20, 30}, {40, 50, 60}}
The preceding example, the myArray is a 2D array containing 2 rows and
3 columns.

myArrya[0][0] = 10;
myArrya[0][1] = 20;
myArrya[0][2] = 30;
myArrya[1][0] = 40;
myArrya[1][1] = 50;
myArrya[1][2] = 60;

5
• In C programming, you can create array of an array known
as multidimensional array. For example,

• float x[3][4];
• Here, x is a two-dimensional (2d) array. The array can hold
12 elements. You can think the array as table with 3 row and
each row has 4 column.

6
Declaring, initializing of a 2-dimension
array.
Declaration of two dimension array is:
Float c[2][3];

Different ways are include to Initialize two dimension array :


Example-1:
c[0][0] = 45; c[0][1] = 10; c[0][2] = 30;
c[1][0] = 20; ….
Example-2:
int c[2][3] = {{1, 3, 0}, {-1, 5, 9}};
Example 3:
int c[2][3] = {1, 3, 0, -1, 5, 9};

7
Accessing elements from 2d array
int main(){
int arr[4][4] = { {1,2,3,4},{2,3,4,1}, {3,4,1,2}, {4,1,2,3} };
int i, j, m=4, n=4;
printf("Elements of the array are:\n");
for(i=0; i<m; i++)
{
for(j = 0; j<n; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
}

8
Program
Write a program that store integer values in an array of 2 rows and 4 columns.

#include<stdio.h>
#include<conio.h>
main()
{
int arr[2][4];

for (int i=0;i<2;i++)


for (int j=0;j<4;j++)
{
printf(“enter ur no”);
scanf(“%d”,&arr[i][j]);
}

for (int i=0;i<2;i++)


{
for (int j=0;j<4;j++)
printf(“%d \n”,arr[i][j]);
}

9
Write a program that initializes two dimensional array of
3 rows and 3 columns and then display the max and
min number of the initialized array.
#include<iostream.h>
int main()
{
int arr[3][3],max,min;
for(int a=0;a<3;a++)
for(int b=0;b<3;b++)
{
cout<<"arr["<<a<<"]["<<b<<"]=";
cin>>arr[a][b];
cout<<endl;
}
max=min=arr[0][0];
for(int c=0;c<3;c++)
for(int d=0;d<3;d++)
{
if(arr[c][d]>max)
max=arr[c][d];
if(arr[c][d]<min)
min=arr[c][d];
}
cout<<"max="<<max<<"\t min="<<min<<endl;
system("pause");
}
Sum of 2D array elements
#include<stdio.h>
#include<conio.h>

int main(){
int arr[3][3], i, j, sum=0;
/*Accepts input from the user and stores it in 2-D array*/
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("\nEnter the value for A[%d][%d]: ",i,j);
scanf("%d",&arr[i][j]);
}
}
/*Calculate sum of elements in 2-D array*/
for(i=0;i<3;i++){
for(j=0;j<3;j++){
sum=sum+arr[i][j];
}
}
/*Display the value of sum*/
printf("\nThe sum of the elements of 2-D array is %d", sum);
getch();
return 0;
}
Declaration and Initialization of a three
dimensional array.
You can initialize a three dimensional array in a similar
way like a two dimensional array. Here's an example,

int test[2][3][4] = { { {3, 4, 2, 3}, {0, -3, 9, 11},


{23, 12, 23, 2} }, { {13, 4, 56, 3},
{5, 9, 3, 5}, {3, 1, 4, 9} } };

[2]  z
[3]  rows
[4]  columns

13
14
main(){
// this array can store 12 elements
int i, j, k, test[2][3][2];
printf("Enter 12 values: \n");
for(i = 0; i < 2; ++i) {
for (j = 0; j < 3; ++j) {
for(k = 0; k < 2; ++k ) {
scanf("%d", &test[i][j][k]);
}
Example- C }
Program to store values }
entered by the user in a // Displaying values with proper index.
three-dimensional array printf("\nDisplaying values:\n");
for(i = 0; i < 2; ++i)
and display it.
{ for (j = 0; j < 3; ++j)
{ for(k = 0; k < 2; ++k )
{
printf("test[%d][%d][%d] = %d\n", i, j, k, test[i][j
}
}
}
15
}
16

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy