Chapter 5 Arrays
Chapter 5 Arrays
Chapter 5 Arrays
Consider array:
age[10]
Multidimensional arrays.
Single / One dimensional arrays
Consider:
age[0] = 20;
age[1] = 15;
Declaration of single dimensional arrays
Arrays must be declared before they are used.
Syntax: - return_type array_name[size];
E.g., int age[5];
Explanation:
Return_type: determines the type of elements contained in the array.
Array_name: name of the array
Size: the number of elements in the array.
Initialization of single dimensional arrays
Array declaration creates a template without any values. Therefore they must
be initialized to eliminate garbage values.
Syntax:
Example:
for(i=0;i<5;i++)
scanf(“%d”, &age[i]);
}
Application of single dimensional arrays
Sorting
Searching
Sorting printf("\nsorting process starts here\n");
for(i=0;i<no;i++)
/*searching*/ {
#include<stdio.h> for(j=i+1;j<no;j++)
#include<conio.h> {
if(age[i] > age[j])
int main()
{
{ temp = age[i];
int i, no, age[10],temp,j; age[i] = age[j];
printf("\nenter size of array\n"); age[j] = temp;
scanf("%d",&no); }
}
printf("\ninitialize the array\n");
}
for(i=0;i<no;i++) printf("\n sorted elements are:\n");
{ for(i=0;i<no;i++)
scanf("%d",age[i]); printf("\n%d\n",age[i]);
} return 0;
Searching for(i=0;i<5;i++)
{
/*searching*/
if(key==a[i])
#include<stdio.h>
{
#include<conio.h> printf("\nelement
int main() found\n");
{ printf("\nloc = %d\n",i);
int flag=0,i, key, a[5]; printf("\nelement is
printf("\ninitialize the array\n"); %d\n",a[i]);
flag=1;
for(i=0;i<5;i++)
break;
{
}
scanf("%d",a[i]);
}
}
if(flag==0)
printf("\nenter the element to search\n");
printf("\nelement not found\n");
scanf("%d",&key); return 0;
Two dimensional arrays
Declaration of two dimensional arrays
Sytnax:
return_type array_name[size][size];
int matrix[3][3] = {
{2, 3, 4}
{5, 6, 7}
{3, 6, 9}
}
Initialization of two dimensional arrays
Initialization at runtime time:
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d", &matrixA[i][j]);
}
Application of two dimensional arrays
Addition of matrices
Product of matrices
Transpose of matrices
Identity matrices
printf("\nGiven matrix is\n");
Matrix Transpose for(i=0;i<3;i++) {
/*TRANSPOSE OF A MATRIX*/ for(j=0;j<3;j++)
#include<stdio.h> {
#include<conio.h> printf("%d\t",matrixA[i][j]);
int main() }
{ printf("\n\n");
int i,j,matrixA[3][3]; }
printf("\nInitialize the matrix\n"); printf("\nTranspose of the matrixA\n");
for(i=0;i<3;i++) for(i=0;i<3;i++) {
{ for(j=0;j<3;j++){
for(j=0;j<3;j++) printf("%d\t",matrixA[j][i]);
{ }
scanf("%d", &matrixA[i][j]); printf("\n\n");
} }
} return 0;
}
printf("\nInitialize the matrixB\n");
Matrix addition for(i=0;i<2;i++) {
/*TRANSPOSE OF A MATRIX*/ for(j=0;j<2;j++) {
#include<stdio.h> scanf("%d", &matrixB[i][j]);
#include<conio.h> }
int main() }
{ printf("\nAdd matrix is\n");
int i,j,matrixA[2][2], matrixB[2][2], for(i=0;i<2;i++) {
matrixC[2][2]; for(j=0;j<2;j++)
printf("\nInitialize the matrixA\n"); {
for(i=0;i<2;i++) printf("%d\t",matrixA[i][j] +
{ matrixB[i][j]);
for(j=0;j<2;j++) }
{ printf("\n\n");
scanf("%d", &matrixA[i][j]); }
} return 0;
} }
Matrix product if (n != p)
#include <stdio.h> printf("multiplication isn't possible.\n");
int main() { else {
int m, n, p, q, c, d, k, sum = 0; printf("Enter elements of 2nd matrix\n");
int first[10][10], second[10][10], for (c = 0; c < p; c++)
multiply[10][10]; for (d = 0; d < q; d++)
printf("Enter number of rows and columns scanf("%d", &second[c][d]);
of first matrix\n"); for (c = 0; c < m; c++) {
scanf("%d%d", &m, &n); for (d = 0; d < q; d++) {
printf("Enter elements of first matrix\n"); for (k = 0; k < p; k++) {
for (c = 0; c < m; c++) sum = sum + first[c][k]*second[k][d];
for (d = 0; d < n; d++) }
scanf("%d", &first[c][d]); multiply[c][d] = sum;
printf("Enter number of rows and columns sum = 0; } }
of second matrix\n"); printf("Product of the matrices:\n");
scanf("%d%d", &p, &q); for (c = 0; c < m; c++) {
for (d = 0; d < q; d++)