SlideShare a Scribd company logo
©LPU CSE101 C Programming
CSE101-Lec#17(Part-2)
• Multidimensional Arrays(2D Array)
©LPU CSE101 C Programming
2D-Array
• The basic form of declaring a two-dimensional array of size x,
y:
• Syntax:
• data_type array_name[x][y];
• data_type: Type of data to be stored. Valid C/C++ data type.
• We can declare a two dimensional integer array say ‘x’ of size
10,20 as:
• int x[10][20];
• Elements in two-dimensional arrays are commonly referred by
x[i][j] where i is the row number and ‘j’ is the column number
©LPU CSE101 C Programming
2D-Array
• A two – dimensional array can be seen as a table with ‘x’ rows
and ‘y’ columns where the row number ranges from 0 to (x-1)
and column number ranges from 0 to (y-1). A two –
dimensional array ‘x’ with 3 rows and 3 columns is shown
below:
©LPU CSE101 C Programming
Also known as Multiple-Subscripted Arrays
• Multiple subscripted arrays
– Tables with rows and columns (m by n array)
– Like matrices: specify row, then column
int a[rows][column];
4
Row 0
Row 1
Row 2
Column 0
0
Column 1 Column 2 Column 3
a[ 0 ][ 0 ]
a[ 1 ][ 0 ]
a[ 2 ][ 0 ]
a[ 0 ][ 1 ]
a[ 1 ][ 1 ]
a[ 2 ][ 1 ]
a[ 0 ][ 2 ]
a[ 1 ][ 2 ]
a[ 2 ][ 2 ]
a[ 0 ][ 3 ]
a[ 1 ][ 3 ]
a[ 2 ][ 3 ]
Row subscript
Array name
Column subscript
©LPU CSE101 C Programming
Memory representation of 2D-Array
• A 2D array’s elements are stored in continuous memory locations. It can
be represented in memory using any of the following two ways:
1. Column-Major Order
2. Row-Major Order
1. Column-Major Order:
In this method the elements are stored column wise, i.e. m elements of
first column are stored in first m locations, m elements of second column
are stored in next m locations and so on. E.g.
A 3 x 4 array will stored as below:
©LPU CSE101 C Programming
Memory representation of 2D-Array
• 2. Row-Major Order:
In this method the elements are stored row wise, i.e. n
elements of first row are stored in first n locations, n elements
of second row are stored in next n locations and so on. E.g.
A 3 x 4 array will stored as below:
©LPU CSE101 C Programming
Initialization
1) Initializing at the point of declaration:
• int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
Initializers grouped by row in braces
• If not enough, unspecified elements set to zero
• Int a[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
• int a3[2][2]={1,2};//Remaining elements are zero
• int a4[2][2]={0};//All elements are zero
1 2
3 4
1 0
3 4
©LPU CSE101 C Programming
Initialization
• int a5[][2]={1,2,3};//It is possible to skip row size, if
elements are initialized at the point of declaration
• int a[2][]={1,2,3};//Not possible to skip column
size[Error will come]
• int a[][]={1,2,3};//Not possible to skip both row and
column size[Error will come]
©LPU CSE101 C Programming
Initialization
2) Taking input from user
int a[3][3], i, j;
for(i=0; i<3; i++)
{ // for loop for rows
for(j=0; j<3;j++)
{ // for loop for columns
printf(“enter the value ofa[%d][%d]: ”, i, j);
scanf(“%d”, &a[i][j]);
} //end for columns
} //end for rows
©LPU CSE101 C Programming
Program to
display 2D
array
#include<stdio.h>
void main()
{
int a[3][3], i, j;
for(i=0; i<3; i++)
{ // for loop for rows
for(j=0; j<3;j++)
{ // for loop for columns
printf(“enter the value of a[%d][%d]: ”, i, j);
scanf(“%d”, &a[i][j]);
} //end for columns
} //end for rows
printf(“elements of 2D matrix are”);
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
print(“%dt”, a[i][j]);
} //end for
printf(“n”);
} //end for
} //end main
enter the value of a[0][1] :1
enter the value of a[0][1] :2
enter the value of a[0][1] :3
enter the value of a[0][1] :4
enter the value of a[0][1] :5
enter the value of a[0][1] :6
enter the value of a[0][1] :7
enter the value of a[0][1] :8
enter the value of a[0][1] :9
Element of 2D matrix are:
1 2 3
4 5 6
7 8 9
©LPU CSE101 C Programming
Matrix operations using 2D arrays
• WAP to find the sum of two matrices
• WAP to display the transpose of a matrix
• WAP to find the sum of diagonal elements of a
matrix
• WAP to perform multiplication of 2 matrices
and display the result
©LPU CSE101 C Programming
WAP to find the sum of two matrices
#include <stdio.h>
int main()
{
float a[3][3], b[3][3], c[3][3];
int i, j;
printf("Enter elements of 1st matrixn");
for(i=0; i<3; i++)
{
for(j=0; j<3 ;j++)
{
printf("Enter a%d%d: ", i, j);
scanf("%f", &a[i][j]);
}
}
// Taking input using nested for loop
printf("Enter elements of 2nd matrixn");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("Enter b%d%d: ", i, j);
scanf("%f", &b[i][j]);
}
}
// adding corresponding elements of two arrays
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
// Displaying the sum
printf("nSum Of Matrix:n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
printf("%.1ft", c[i][j]);
}
printf("n");
}
return 0;
}
©LPU CSE101 C Programming
WAP to display the transpose of a matrix
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns of matrix: ");
scanf("%d %d", &r, &c);
// Storing elements of the matrix
printf("nEnter elements of matrix:n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("Enter element a%d%d: ",i, j);
scanf("%d", &a[i][j]);
}
}
// Displaying the matrix a[][] */
printf("nEntered Matrix: n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("%d ", a[i][j]);
}
printf("nn");
}
// Finding the transpose of matrix a
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
transpose[i][j] = a[j][i];
}
}
// Displaying the transpose of matrix a
printf("nTranspose of Matrix:n");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("%d ",transpose[i][j]);
}
printf("nn");
}
return 0;
}
©LPU CSE101 C Programming
WAP to find the sum of diagonal elements of a matrix
#include<stdio.h>
int main()
{
int a[10][10],sum=0;
int i,j,m,n;
printf("Enter number of rows and
column:");
scanf("%d%d",&m,&n);
printf("Enter Elements : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
sum=sum+a[i][j];
}
}
}
printf("Sum of Diagonal
Elements = %d ",sum);
}
©LPU CSE101 C Programming
WAP to perform multiplication of 2 matrices and display the result
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
// Column of first matrix should be equal to column of second matrix and
while (c1 != r2)
{
printf("Error! No. of columns of first matrix not equal to no.of row of
second.nn");
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for second matrix: ");
scanf("%d %d",&r2, &c2);
}
// Storing elements of first matrix.
printf("nEnter elements of matrix 1:n");
for(i=0; i<r1; i++)
{
for(j=0; j<c1; j++)
{
printf("Enter elements a%d%d: ",i,j);
scanf("%d", &a[i][j]);
}
}
// Storing elements of second matrix.
printf("nEnter elements of matrix 2:n");
for(i=0; i<r2; i++)
{
for(j=0; j<c2; j++)
{
printf("Enter elements b%d%d: ",i, j);
scanf("%d",&b[i][j]);
}
// Initializing all elements of result matrix to 0
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
result[i][j] = 0;
}
}
// Multiplying matrices a and b and
// storing result in result matrix
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
for(k=0; k<c1; k++)
{
result[i][j]+=a[i][k]*b[k][j];
}
}
}
// Displaying the result
printf("nOutput Matrix:n");
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
printf("%d ", result[i][j]);
}
printf("nn");
}
return 0;
• }
©LPU CSE101 C Programming
Dry running
©LPU CSE101 C Programming
Q1
What will be the output of following code?
#include<stdio.h>
int main()
{
int a[][3]={1,2,3,4,5,6};
printf("%d",a[0][2]);
return 0;
}
A. 2
B. 3
C. 4
D. Compile time error
©LPU CSE101 C Programming
Q2
Which of the following is invalid initialization of 2D Array?
A. int a[][2]={1,2,3,4};
B. int a[2][2]={1,2,3,4};
C. int a[2][]={1,2,3,4};
D. int a[2][2]={};
©LPU CSE101 C Programming
Q3
What will be the output of following code?
#include<stdio.h>
int main()
{
int a[3][2]={{1,2},{3,4},{5,6}};
printf("%d",a[1][1]*a[2][1]);
return 0;
}
A. 24
B. 12
C. 8
D. 20
Ad

More Related Content

Similar to Arrays 2d Arrays 2d Arrays 2d Arrrays 2d (20)

Array
ArrayArray
Array
Anil Dutt
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
AntareepMajumder
 
2D array
2D array2D array
2D array
A. S. M. Shafi
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
Shubham Sharma
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
DeveshDewangan5
 
Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789
Ghh
 
labb123456789123456789123456789123456789
labb123456789123456789123456789123456789labb123456789123456789123456789123456789
labb123456789123456789123456789123456789
Ghh
 
Unit 3 arrays and_string
Unit 3 arrays and_stringUnit 3 arrays and_string
Unit 3 arrays and_string
kirthika jeyenth
 
C- Programming Assignment 3
C- Programming Assignment 3C- Programming Assignment 3
C- Programming Assignment 3
Animesh Chaturvedi
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
naveed jamali
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
SwapnaliPawar27
 
02 arrays
02 arrays02 arrays
02 arrays
Rajan Gautam
 
Yash Bhargava Array In programming in C
Yash Bhargava  Array In programming in CYash Bhargava  Array In programming in C
Yash Bhargava Array In programming in C
simranroy370
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
NelyJay
 
unit-2-dsa.pptx
unit-2-dsa.pptxunit-2-dsa.pptx
unit-2-dsa.pptx
sayalishivarkar1
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
rumanatasnim415
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
mohd_mizan
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Boro
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
AntareepMajumder
 
Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789Zoro123456789123456789123456789123456789
Zoro123456789123456789123456789123456789
Ghh
 
labb123456789123456789123456789123456789
labb123456789123456789123456789123456789labb123456789123456789123456789123456789
labb123456789123456789123456789123456789
Ghh
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
naveed jamali
 
Yash Bhargava Array In programming in C
Yash Bhargava  Array In programming in CYash Bhargava  Array In programming in C
Yash Bhargava Array In programming in C
simranroy370
 
Lecture 9_Classes.pptx
Lecture 9_Classes.pptxLecture 9_Classes.pptx
Lecture 9_Classes.pptx
NelyJay
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
vrgokila
 

Recently uploaded (20)

Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
Ad

Arrays 2d Arrays 2d Arrays 2d Arrrays 2d

  • 1. ©LPU CSE101 C Programming CSE101-Lec#17(Part-2) • Multidimensional Arrays(2D Array)
  • 2. ©LPU CSE101 C Programming 2D-Array • The basic form of declaring a two-dimensional array of size x, y: • Syntax: • data_type array_name[x][y]; • data_type: Type of data to be stored. Valid C/C++ data type. • We can declare a two dimensional integer array say ‘x’ of size 10,20 as: • int x[10][20]; • Elements in two-dimensional arrays are commonly referred by x[i][j] where i is the row number and ‘j’ is the column number
  • 3. ©LPU CSE101 C Programming 2D-Array • A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’ columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A two – dimensional array ‘x’ with 3 rows and 3 columns is shown below:
  • 4. ©LPU CSE101 C Programming Also known as Multiple-Subscripted Arrays • Multiple subscripted arrays – Tables with rows and columns (m by n array) – Like matrices: specify row, then column int a[rows][column]; 4 Row 0 Row 1 Row 2 Column 0 0 Column 1 Column 2 Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript
  • 5. ©LPU CSE101 C Programming Memory representation of 2D-Array • A 2D array’s elements are stored in continuous memory locations. It can be represented in memory using any of the following two ways: 1. Column-Major Order 2. Row-Major Order 1. Column-Major Order: In this method the elements are stored column wise, i.e. m elements of first column are stored in first m locations, m elements of second column are stored in next m locations and so on. E.g. A 3 x 4 array will stored as below:
  • 6. ©LPU CSE101 C Programming Memory representation of 2D-Array • 2. Row-Major Order: In this method the elements are stored row wise, i.e. n elements of first row are stored in first n locations, n elements of second row are stored in next n locations and so on. E.g. A 3 x 4 array will stored as below:
  • 7. ©LPU CSE101 C Programming Initialization 1) Initializing at the point of declaration: • int a[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; Initializers grouped by row in braces • If not enough, unspecified elements set to zero • Int a[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; • int a3[2][2]={1,2};//Remaining elements are zero • int a4[2][2]={0};//All elements are zero 1 2 3 4 1 0 3 4
  • 8. ©LPU CSE101 C Programming Initialization • int a5[][2]={1,2,3};//It is possible to skip row size, if elements are initialized at the point of declaration • int a[2][]={1,2,3};//Not possible to skip column size[Error will come] • int a[][]={1,2,3};//Not possible to skip both row and column size[Error will come]
  • 9. ©LPU CSE101 C Programming Initialization 2) Taking input from user int a[3][3], i, j; for(i=0; i<3; i++) { // for loop for rows for(j=0; j<3;j++) { // for loop for columns printf(“enter the value ofa[%d][%d]: ”, i, j); scanf(“%d”, &a[i][j]); } //end for columns } //end for rows
  • 10. ©LPU CSE101 C Programming Program to display 2D array #include<stdio.h> void main() { int a[3][3], i, j; for(i=0; i<3; i++) { // for loop for rows for(j=0; j<3;j++) { // for loop for columns printf(“enter the value of a[%d][%d]: ”, i, j); scanf(“%d”, &a[i][j]); } //end for columns } //end for rows printf(“elements of 2D matrix are”); for(i=0; i<3; i++) { for(j=0;j<3;j++) { print(“%dt”, a[i][j]); } //end for printf(“n”); } //end for } //end main
  • 11. enter the value of a[0][1] :1 enter the value of a[0][1] :2 enter the value of a[0][1] :3 enter the value of a[0][1] :4 enter the value of a[0][1] :5 enter the value of a[0][1] :6 enter the value of a[0][1] :7 enter the value of a[0][1] :8 enter the value of a[0][1] :9 Element of 2D matrix are: 1 2 3 4 5 6 7 8 9
  • 12. ©LPU CSE101 C Programming Matrix operations using 2D arrays • WAP to find the sum of two matrices • WAP to display the transpose of a matrix • WAP to find the sum of diagonal elements of a matrix • WAP to perform multiplication of 2 matrices and display the result
  • 13. ©LPU CSE101 C Programming WAP to find the sum of two matrices #include <stdio.h> int main() { float a[3][3], b[3][3], c[3][3]; int i, j; printf("Enter elements of 1st matrixn"); for(i=0; i<3; i++) { for(j=0; j<3 ;j++) { printf("Enter a%d%d: ", i, j); scanf("%f", &a[i][j]); } } // Taking input using nested for loop printf("Enter elements of 2nd matrixn"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf("Enter b%d%d: ", i, j); scanf("%f", &b[i][j]); } } // adding corresponding elements of two arrays for(i=0; i<3; i++) { for(j=0; j<3; j++) { c[i][j] = a[i][j] + b[i][j]; } } // Displaying the sum printf("nSum Of Matrix:n"); for(i=0; i<3; i++) { for(j=0; j<3; j++) { printf("%.1ft", c[i][j]); } printf("n"); } return 0; }
  • 14. ©LPU CSE101 C Programming WAP to display the transpose of a matrix #include <stdio.h> int main() { int a[10][10], transpose[10][10], r, c, i, j; printf("Enter rows and columns of matrix: "); scanf("%d %d", &r, &c); // Storing elements of the matrix printf("nEnter elements of matrix:n"); for(i=0; i<r; i++) { for(j=0; j<c; j++) { printf("Enter element a%d%d: ",i, j); scanf("%d", &a[i][j]); } } // Displaying the matrix a[][] */ printf("nEntered Matrix: n"); for(i=0; i<r; i++) { for(j=0; j<c; j++) { printf("%d ", a[i][j]); } printf("nn"); } // Finding the transpose of matrix a for(i=0; i<r; i++) { for(j=0; j<c; j++) { transpose[i][j] = a[j][i]; } } // Displaying the transpose of matrix a printf("nTranspose of Matrix:n"); for(i=0; i<r; i++) { for(j=0; j<c; j++) { printf("%d ",transpose[i][j]); } printf("nn"); } return 0; }
  • 15. ©LPU CSE101 C Programming WAP to find the sum of diagonal elements of a matrix #include<stdio.h> int main() { int a[10][10],sum=0; int i,j,m,n; printf("Enter number of rows and column:"); scanf("%d%d",&m,&n); printf("Enter Elements : "); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&a[i][j]); } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(i==j) { sum=sum+a[i][j]; } } } printf("Sum of Diagonal Elements = %d ",sum); }
  • 16. ©LPU CSE101 C Programming WAP to perform multiplication of 2 matrices and display the result #include <stdio.h> int main() { int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k; printf("Enter rows and column for first matrix: "); scanf("%d %d", &r1, &c1); printf("Enter rows and column for second matrix: "); scanf("%d %d",&r2, &c2); // Column of first matrix should be equal to column of second matrix and while (c1 != r2) { printf("Error! No. of columns of first matrix not equal to no.of row of second.nn"); printf("Enter rows and column for first matrix: "); scanf("%d %d", &r1, &c1); printf("Enter rows and column for second matrix: "); scanf("%d %d",&r2, &c2); } // Storing elements of first matrix. printf("nEnter elements of matrix 1:n"); for(i=0; i<r1; i++) { for(j=0; j<c1; j++) { printf("Enter elements a%d%d: ",i,j); scanf("%d", &a[i][j]); } } // Storing elements of second matrix. printf("nEnter elements of matrix 2:n"); for(i=0; i<r2; i++) { for(j=0; j<c2; j++) { printf("Enter elements b%d%d: ",i, j); scanf("%d",&b[i][j]); } // Initializing all elements of result matrix to 0 for(i=0; i<r1; i++) { for(j=0; j<c2; j++) { result[i][j] = 0; } } // Multiplying matrices a and b and // storing result in result matrix for(i=0; i<r1; i++) { for(j=0; j<c2; j++) { for(k=0; k<c1; k++) { result[i][j]+=a[i][k]*b[k][j]; } } } // Displaying the result printf("nOutput Matrix:n"); for(i=0; i<r1; i++) { for(j=0; j<c2; j++) { printf("%d ", result[i][j]); } printf("nn"); } return 0; • }
  • 17. ©LPU CSE101 C Programming Dry running
  • 18. ©LPU CSE101 C Programming Q1 What will be the output of following code? #include<stdio.h> int main() { int a[][3]={1,2,3,4,5,6}; printf("%d",a[0][2]); return 0; } A. 2 B. 3 C. 4 D. Compile time error
  • 19. ©LPU CSE101 C Programming Q2 Which of the following is invalid initialization of 2D Array? A. int a[][2]={1,2,3,4}; B. int a[2][2]={1,2,3,4}; C. int a[2][]={1,2,3,4}; D. int a[2][2]={};
  • 20. ©LPU CSE101 C Programming Q3 What will be the output of following code? #include<stdio.h> int main() { int a[3][2]={{1,2},{3,4},{5,6}}; printf("%d",a[1][1]*a[2][1]); return 0; } A. 24 B. 12 C. 8 D. 20
  翻译: