Array - I
Array - I
Number of elements:
UB – LB +1
Example:
Question:
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)
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;
}
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
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;
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.
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 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
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