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

Lecture 2

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)
14 views

Lecture 2

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/ 24

Lecture 2: Array

Md. Nazmul Abdal


Lecturer
Department of CSE
University of Liberal Arts Bangladesh (ULAB)
Introduction

⚫ Array is a type of linear data structure that is defined as a


collection of elements with same data types.
⚫ They exist in both single dimension and multiple dimensions.
⚫ These data structures come into picture when there is a necessity
to store multiple elements of similar nature together at one place.
⚫ The difference between an array index and a memory address is
that the array index acts like a key value to label the elements in
the array. However, a memory address is the starting address of
free memory available.
Syntax

⚫ data_type array_name[array_size] = {comma separated elements}


⚫ int LA[3] = {1,3,5};
⚫ data_type array_name[array_size];
⚫ int LA[3];

⚫ Index starts with 0.


⚫ Array length is 9 which means it can store 9 elements.
⚫ Each element can be accessed via its index. For example, we can
fetch an element at index 6 as 23.
Basic Operations

⚫ Traverse − print all the array elements one by one.

⚫ Insertion − Adds an element at the given index.

⚫ Deletion − Deletes an element at the given index.

⚫ Search − Searches an element using the given index or value.

⚫ Update − Updates an element at the given index.

⚫ Display − Displays the contents of the array.


Insertion

Algorithm
1. Start
2. Create an Array of a desired datatype and size.
3. Initialize a variable ‘i’ as 0.
4. Enter the element at ith index of the array.
5. Increment i by 1.
6. Repeat Steps 4 & 5 until the end of the array.
7. Stop
Insertion

#include <stdio.h> Output:


int main() { Array Before Insertion:
int i, n, num, pos, arr[10]; LA[0] = 587297216
printf("\n Enter the number of elements in the array : LA[1] = 32767
"); scanf("%d", &n); LA[2] = 0
for(i=0;i<n;i++) { Inserting Elements..
printf("\n arr[%d] = ", i); scanf("%d", &arr[i]); The array elements after insertion:
} LA[0] = 2
printf("\n Enter the number to be inserted : "); LA[1] = 3
scanf("%d", &num); LA[2] = 4
printf("\n Enter the position at which the number has
to be added :"); scanf("%d", &pos);
for(int i=n-1;i>=pos;i--){ arr[i+1] = arr[i];
}
arr[pos] = num; n = n+1;
printf("\n The array after insertion of %d is : ", num);
for(i=0;i<n;i++){
printf("\n arr[%d] = %d", i, arr[i]);
} return 0; }
Deletion

Algorithm
1. Start
2. Set J = K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J] = LA[J + 1]
5. Set J = J+1
6. Set N = N-1
7. Stop
Deletion

#include <stdio.h>
Output:
void main(){ The original array elements are :
int LA[] = {1,3,5}; LA[0] = 1
int n = 3; LA[1] = 3
int i; LA[2] = 5
printf("The original array elements are :\n"); The array elements after deletion :
for(i = 0; i<n; i++) LA[0] = 1
LA[1] = 5
printf("LA[%d] = %d \n", i, LA[i]);
for(i = 1; i<n; i++) {
LA[i] = LA[i+1];
n = n – 1;
}
printf("The array elements after deletion :\n");
for(i = 0; i<n-1; i++)
printf("LA[%d] = %d \n", i, LA[i]);
}
Search

Algorithm
1. Start
2. Set J = 0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Search

#include <stdio.h>
Output:
void main(){ The original array elements are :
int LA[] = {1,3,5,7,8}; LA[0] = 1
int item = 5, n = 5; LA[1] = 3
int i = 0, j = 0; LA[2] = 5
printf("The original array elements are :\n"); LA[3] = 7
for(i = 0; i<n; i++) { LA[4] = 8
Found element 5 at position 3
printf("LA[%d] = %d \n", i, LA[i]);
}
for(i = 0; i<n; i++) {
if( LA[i] == item ) {
printf("Found element %d at position %d\n",
item, i+1);
}
}
}
Traversal

Algorithm
1. Start
2. Initialize an Array of certain size and datatype.
3. Initialize another variable ‘i’ with 0.
4. Print the ith value in the array and increment i.
5. Repeat Step 4 until the end of the array is reached.
6. End
Traversal

#include <stdio.h> Output:


int main(){ The original array elements are :
int LA[] = {1,3,5,7,8}; LA[0] = 1
LA[1] = 3
int item = 10, k = 3, n = 5;
LA[2] = 5
int i = 0, j = n; LA[3] = 7
printf("The original array elements are :\n"); LA[4] = 8
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Update

Algorithm
1. Start
2. Set LA[K-1] = ITEM
3. Stop
Update

#include <stdio.h> Output:


void main(){ The original array elements are :
int LA[] = {1,3,5,7,8}; LA[0] = 1
LA[1] = 3
int k = 3, n = 5, item = 10;
LA[2] = 5 LA[3] = 7
int i, j; LA[4] = 8
printf("The original array elements are :\n"); The array elements after updation :
for(i = 0; i<n; i++) { LA[0] = 1
printf("LA[%d] = %d \n", i, LA[i]); LA[1] = 3
LA[2] = 10
}
LA[3] = 7
LA[k-1] = item; LA[4] = 8
printf("The array elements after updation :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Display

Algorithm
1. Start
2. Print all the elements in the Array
3. Stop
Display

#include <stdio.h> Output:


int main(){ The original array elements are :
int LA[] = {1,3,5,7,8}; LA[0] = 1
LA[1] = 3
int n = 5;
LA[2] = 5
int i; LA[3] = 7
printf("The original array elements are :\n"); LA[4] = 8
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
Merging Two Arrays

⚫ Merging two arrays in a third array means first copying the


contents of the first array into the third array and then copying
the contents of the second array into the third array.
⚫ Hence, the merged array contains the contents of the first array
followed by the contents of the second array.
Merging Two Arrays

#include <stdio.h> Output:


int main() { Enter the number of elements in array1 : 3
int arr1[10], arr2[10], arr3[20];
int i, n1, n2, m, index=0; Enter the elements of the first array
arr1[0] = 2
printf("\n Enter the number of elements in array1 : ");
scanf("%d", &n1); arr1[1] = 3
printf("\n\n Enter the elements of the first array");
for(i=0;i<n1;i++) { arr1[2] = 4
printf("\n arr1[%d] = ", i);
Enter the number of elements in array2 : 2
scanf("%d", &arr1[i]);
}
printf("\n Enter the number of elements in array2 : ");
scanf("%d", &n2);
printf("\n\n Enter the elements of the second array");
Merging Two Arrays
for(i=0;i<n2;i++) {
Output:
printf("\n arr2[%d] = ", i);
Enter the elements of the second array
scanf("%d", &arr2[i]); arr2[0] = 8
}
m = n1+n2; arr2[1] = 9
for(i=0;i<n1;i++) {
The merged array is
arr3[index] = arr1[i]; index++; arr[0] = 2
} arr[1] = 3
for(i=0;i<n2;i++) { arr[2] = 4
arr3[index] = arr2[i]; index++; arr[3] = 8
arr[4] = 9
}
printf("\n\n The merged array is");
for(i=0;i<m;i++) {
printf("\n arr[%d] = %d", i, arr3[i]);
}
return 0;
}
Two-Dimensional Array

⚫ A two-dimensional array is specified using two subscripts


where the first subscript denotes the row and the second
denotes the column.
⚫ The C compiler treats a two-dimensional array as an array of
one-dimensional arrays.
⚫ data_type array_name [row_size] [column_size];
⚫ int marks[3][5];
Read and Display a 3 x 3 Matrix
#include <stdio.h>
int main() {
Output:
int i, j, mat[3][3];
Enter the elements of the matrix:
printf("Enter the elements of the matrix: \n");
123456789
for(i=0;i<3;i++){
The elements of the matrix are:
for(j=0;j<3;j++){
1 2 3
scanf("%d",&mat[i][j]);
4 5 6
}
7 8 9
}
printf(“\nThe elements of the matrix are: ");
for(i=0;i<3;i++){
printf("\n");
for(j=0;j<3;j++){
printf("\t %d",mat[i][j]);
}
}
return 0;
}
Transpose a 3x3 Matrix

#include <stdio.h> for(i=0;i<3;i++){


int main() { for(j=0;j<3;j++){
int i, j, mat[3][3], transposed_mat[3][3]; transposed_mat[i][j] = mat[j][i];
printf("Enter the elements of the matrix: \n"); }
for(i=0;i<3;i++){ }
for(j=0;j<3;j++){ printf("\nThe elements of the transposed matrix are: ");
scanf("%d", &mat[i][j]); for(i=0;i<3;i++){
} printf("\n");
} for(j=0;j<3;j++){
printf(“\nThe elements of the matrix are: "); printf("\t %d",transposed_mat[i][j]);
for(i=0;i<3;i++){ }
printf("\n"); }
for(j=0;j<3;j++){ return 0;
printf("\t %d", mat[i][j]); }
}
}
Output

Enter the elements of the matrix:


123456789

The elements of the matrix are:


1 2 3
4 5 6
7 8 9

The elements of the transposed matrix are:


1 4 7
2 5 8
3 6 9
Thank You

You might also like