Chapter 5 Arrays

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

Chapter 5: Arrays

By Mr. Samwel Tarus


Introduction to Arrays Concepts
Group of related data items that share a common name.
Homogeneous collection of elements that are placed / stored in contiguous
memory locations and can be accessed by an index.
Collection of data items, all of the same type, accessed using a common name.
Kind of data structure that can store a fixed-size sequential collection of elements
of the same type.
All arrays consist of contiguous memory locations. The lowest address
corresponds to the first element and the highest address to the last element.
Arrays begin counting from 0, therefore the last element is always n-1.
Arrays are user defined data types.
P.T.O…

Consider array:

age[10]

First element Last element

age[0] age[1] age[2] Age[0]


age[3] age[n-1]

i=0 i=1 i=2 i=3 i=n-1


Types of arrays
Single / One dimensional arrays

Two dimensional arrays

Multidimensional arrays.
Single / One dimensional arrays

Consider:

The subscripted variable refers to the element of .

In C, single-subscripted variable can be expressed as:

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.

Initialization can be done in two ways: -


At compile time
At runtime
Compile Time Initialization

Initializing arrays during declaration time.

Syntax:

return_type array_name[size] = { list of values };

Example:

int age[5] = { 20, 12, 67, 34, 45 };


Run Time Initialization

Initializing arrays during runtime.

Normally used to initialize large arrays.

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];

e.g., int matrix[3][3];


Initialization of two dimensional arrays
Initialization at declaration time:

int matrix[3][3] = {

{2, 3, 4}

{5, 6, 7}

{3, 6, 9}

}
Initialization of two dimensional arrays
Initialization at runtime time:

printf("\nInitialize the matrix\n");

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++)

You might also like