0% found this document useful (0 votes)
6 views42 pages

Array - I

The document provides an overview of one-dimensional arrays, including their structure, address calculation, and basic operations like insertion, deletion, and searching. It also introduces abstract data types (ADT) and discusses algorithms for traversing and manipulating arrays, including linear and binary search methods. Additionally, it touches on multidimensional arrays and their size calculations, as well as the use of loops for element access.

Uploaded by

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

Array - I

The document provides an overview of one-dimensional arrays, including their structure, address calculation, and basic operations like insertion, deletion, and searching. It also introduces abstract data types (ADT) and discusses algorithms for traversing and manipulating arrays, including linear and binary search methods. Additionally, it touches on multidimensional arrays and their size calculations, as well as the use of loops for element access.

Uploaded by

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

A One-Dimensional Array is the

simplest form of an Array in which


the elements are stored linearly
and can be accessed individually
by specifying the index value of
each element stored in the array.
Address of any element 1-D array:

Number of elements:
UB – LB +1

Example:
Question:

Given the base address of an array B[1300…..1900] as 1020 and size of


each element is 2 bytes in the memory. Find the address of B[1700].

Solution:
The given values are: B = 1020, LB = 1300, W = 2, I = 1700

Address of A [ I ] = B + W * ( I – LB )
= 1020 + 2 * (1700 – 1300)
= 1020 + 2 * 400
= 1020 + 800
= 1820
Take 5 integer input from user and store then in an array
and display all numbers stored in array.
Abstract Data type (ADT)

An abstract data type is an abstraction of a data structure that


provides only the interface to which the data structure must
adhere. The interface does not give any specific details about
something should be implemented or in what programming
language.
We know the data that we are storing and the operations that
can be performed on the data, but we don't know about the
implementation details.

The ADT model contains the data structures that we are using in a program. In this model, first
encapsulation is performed, i.e., all the data is wrapped in a single unit, i.e., ADT. Then, the
abstraction is performed means showing the operations that can be performed on the data structure
and what are the data structures that we are using in a program.
Array ADT

Stack ADT

Queue ADT

List ADT
Traverse Linear Array

C Code Algorithm

#include<stdio.h>
TraverseLinearArray(array,n)
int main()
{ STEP I: Set i=0
int n=5; STEP 2: Repeat step 3 For i=0 to (n-1) in steps of +1
int a[n]= {22,55,33,1,78}; STEP 3: Process the element: (array[i])
for (i=0; i<n;i++) End For
{ STEP 4:Exit
printf(“%d”, a[i]);
}
return 0;
}

Performance Analysis: Big O notation


For an array of size n, for loop executes n times. Thus traversal operation on linear arrays is O(n) operations
Linear Search
Write a Program to search an element in an array.
Linear_Search(a, n, val) // 'a' is the given array, ’n’ is the size of the array,
‘val' is the value to search
Step 1: set pos = -1
Step 2: set i = 1
Step 3: repeat step 4 while i <= n
Step 4: if a[i] == val
set pos = i
print pos
go to step 6
[end of if]
set i = i + 1
[end of loop]
Step 5: if pos = -1
print "value is not present in the array "
[end of if]
Step 6: exit
Insertion
Algorithm

Let LA be a Linear Array (unordered) with N elements and K is a positive integer such that K<=N.
Following is the algorithm where ITEM is inserted into the Kth position of LA −

1. Start
2. Set J = N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop
int main()
{
// read index at which element is to be inserted
int arr[20];
printf("Enter the index at which the element is to be inserted
int i, item, index, size;
");
printf("Enter %d elements: ",size);
scanf("%d",&index);
printf("Enter the size of the array");
scanf("%d",&size);
// increase the size
size++;
printf("Enter %d elements: ",size);
// shift elements forward
// reading array
for (i = size-1; i >= index+1; i--)
for (i = 0; i < size; i++)
{
{
arr[i] = arr[i - 1];
scanf("%d",&arr[i]);
}
// insert item at position
}
arr[index] = item;
// print the original array
// print the updated array
printf("Array before insertion: ");
printf("Array after insertion: ");
for (i = 0; i < size; i++)
for (i = 0; i < size; i++)
{
{
printf("%d ", arr[i]);
printf("%d ", arr[i]);
printf("\n");
}
}
return 0;
// read element to be inserted
}
printf("Enter the element to be inserted: ");
scanf("%d",&item);
Deletion

Deletion refers to removing an existing element from the array


and re-organizing all elements of an array.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Following is the algorithm
to delete an element available at the Kth position of LA.

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
1.#include <stdio.h> 23. // check whether the deletion is possible or not
2.#include <conio.h> 24. if (index >= num+1)
3.int main () 25. {
4.{ 26. printf (" \n Deletion is not possible in the array.");
5. // declaration of the int type variable 27. }
6. int arr[50]; 28. else
7. int pos, i, num; // declare int type variable 29. {
8. printf (" \n Enter the number of elements in an array: \n "); 30. // use for loop to delete the element and update the index
9. scanf (" %d", &num); 31. for (i = index; i < num -1; i++)
10. 32. {
11. printf (" \n Enter %d elements in array: \n ", num); 33. arr[i] = arr[i+1]; // assign arr[i+1] to arr[i]
12. 34. }
13. // use for loop to insert elements one by one in array 35.
14. for (i = 0; i < num; i++ ) 36. printf (" \n The resultant array is: \n");
15. { printf (" arr[%d] = ", i); 37.
16. scanf (" %d", &arr[i]); 38. // display the final array
17. } 39. for (i = 0; i< num - 1; i++)
18. 40. {
19. // enter the index of the element to be deleted 41. printf (" arr[%d] = ", i);
20. printf( " Define the index of the array element where you want to delete:"); 42. printf (" %d \n", arr[i]);
21. scanf (" %d", &index); 43. }
44. }
45. return 0;
46.}
Tutorial
1. Identify the correct form of creating an array
a. int arr[];
b. int arr[7];
c. int arr[]={1,2};
d. int arr;

2. Can you declare an array without assigning the size of an array?

3. Consider the linear array ARR as shown below


a. Find ARR[0], ARR[2], Arr[8]
b. Suppose ‘P’ is to inserted at location 5 (index=5) into the array.
How many characters must be moved to new locations?
A B C D E F G H I J
ARR
4. Suppose a library system has a record of 8 books for a Course “Computer Graphics” arranged in a rack of library.
The current status of books arranged and the quantity of books are as follows:

Book Author 1 Author 2 Author 3 Author 4 Author 5 Author 6 Author 7 Author 8

Quantity 32 15 80 44 65 7 45 24

With high demand over time, Suppose that the number of books for Author 6 has now been increased to 30. Write an
algorithm to modify the inventory such that the value for Author 6 is now updated from 7 to 30.

5.Each student in a class of 20 takes a test in which scores range between 0-100. Suppose the test scores are stored in
a linear array SCORE of size 20. Write a module that:
a. Finds the average grade.
b. Find the number NUM of students who have failed i.e. score is less than 60.
Binary Search

Binary Search is defined as a searching algorithm used in a sorted array by repeatedly dividing the
search interval in half.

Condition: The data structure must be sorted.


In this algorithm,
•Divide the search space into two halves by finding the middle index “mid”.
• Compare the middle element of the search space with the key.
• If the key is found at middle element, the process is terminated.
• If the key is not found at middle element, choose which half will be used as the next search space.
• If the key is smaller than the middle element, then the left side is used for next search.
• If the key is larger than the middle element, then the right side is used for next search.
• This process is continued until the key is found or the total search space is exhausted.
Consider an array arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.

First Step: Calculate the mid and compare the mid element with the key. If the key is less than mid
element, move to left and if it is greater than the mid then move search space to the right.

•Key (i.e., 23) is greater than current mid element


(i.e., 16). The search space moves to the right.

•Key is less than the current mid 56. The search space
moves to the left.
arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.

Second Step: If the key matches the value of the mid element, the element is found and stop
search.
// 'a' is the given array, ’n’ is the size of the
Binary_Search(a, n , item) array,‘item' is the value to search

Step 1: set beg = 0, end = n-1, pos = -1

Step 2: repeat steps 3 and 4 while beg <=end

Step 3: set mid = (beg + end)/2

Step 4: if a[mid] = item


set pos = mid
PRINT "value present in the array"
PRINT pos
Step 5: if pos = -1
PRINT "value is not present in the array"
GOTO Step 6
[end of if]
else if a[mid] > item
set end = mid - 1
Step 6: exit
else
set beg = mid + 1
[end of if]
[end of loop]
Size of Multidimensional Arrays:

The total number of elements that can be stored in a multidimensional array can be
calculated by multiplying the size of all the dimensions.

For example:
•The array int x[10][20] can store total (10*20) = 200 elements

To get the size of the array in bytes, we multiply the size of a single element with the
total number of elements in the array.
For example:
•Size of array int x[10][20] = 10 * 20 * 4 = 800 bytes. (where int = 4 bytes)
Using Loops
Accessing Elements of Two-Dimensional Arrays in C
Traversal in 2D Array

To input/output all the elements of


a Two-Dimensional array we can
use nested for loops. We will
require two ‘for‘ loops. One to
traverse the rows and another to
traverse columns.

You might also like