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:
Example:
Number of elements:
UB – LB +1
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
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].
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.
Queue ADT
Stack ADT
List ADT
Array ADT
Traverse Linear Array
TraverseLinearArray(array,n)
STEP I: Set i=0
STEP 2: Repeat step 3 For i=0 to (n-1) in steps of +1
STEP 3: Process the element: (array[i])
End For
STEP 4:Exit
#include<stdio.h>
int main()
{
int n=5;
int a[n]= {22,55,33,1,78};
for (i=0; i<n;i++)
{
printf(“%d”, a[i]);
}
return 0;
}
C Code Algorithm
For an array of size n, for loop executes n times. Thus traversal operation on linear arrays is O(n) operations
Performance Analysis: Big O notation
Linear Search
Write a Program to search an element in an array.
Insertion
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
Algorithm
int main()
{
int arr[20];
int i, item, index, size;
printf("Enter %d elements: ",size);
printf("Enter the size of the array");
scanf("%d",&size);
printf("Enter %d elements: ",size);
// reading array
for (i = 0; i < size; i++)
{
scanf("%d",&arr[i]);
}
// print the original array
printf("Array before insertion: ");
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
printf("n");
}
// read element to be inserted
printf("Enter the element to be inserted: ");
scanf("%d",&item);
// read index at which element is to be inserted
printf("Enter the index at which the element is to be inserted: ");
scanf("%d",&index);
// increase the size
size++;
// shift elements forward
for (i = size-1; i >= index+1; i--)
{
arr[i] = arr[i - 1];
}
// insert item at position
arr[index] = item;
// print the updated array
printf("Array after insertion: ");
for (i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
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
Deletion refers to removing an existing element from the array
and re-organizing all elements of an array.
1.#include <stdio.h>
2.#include <conio.h>
3.int main ()
4.{
5. // declaration of the int type variable
6. int arr[50];
7. int pos, i, num; // declare int type variable
8. printf (" n Enter the number of elements in an array: n ");
9. scanf (" %d", &num);
10.
11. printf (" n Enter %d elements in array: n ", num);
12.
13. // use for loop to insert elements one by one in array
14. for (i = 0; i < num; i++ )
15. { printf (" arr[%d] = ", i);
16. scanf (" %d", &arr[i]);
17. }
18.
19. // enter the index of the element to be deleted
20. printf( " Define the index of the array element where you want to delete:");
21. scanf (" %d", &index);
23. // check whether the deletion is possible or not
24. if (index >= num+1)
25. {
26. printf (" n Deletion is not possible in the array.");
27. }
28. else
29. {
30. // use for loop to delete the element and update the index
31. for (i = index; i < num -1; i++)
32. {
33. arr[i] = arr[i+1]; // assign arr[i+1] to arr[i]
34. }
35.
36. printf (" n The resultant array is: n");
37.
38. // display the final array
39. for (i = 0; i< num - 1; i++)
40. {
41. printf (" arr[%d] = ", i);
42. printf (" %d n", arr[i]);
43. }
44. }
45. return 0;
46.}
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?
ARR A B C D E F G H I J
Tutorial
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:
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 30. 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.
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
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly dividing the
search interval in half.
Binary Search
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.
Second Step: If the key matches the value of the mid element, the element is found and stop
search.
arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.
Binary_Search(a, n , item)
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
GOTO Step 6
else if a[mid] > item
set end = mid - 1
else
set beg = mid + 1
[end of if]
[end of loop]
// 'a' is the given array, ’n’ is the size of the
array,‘item' is the value to search
Step 5: if pos = -1
PRINT "value is not present in the array"
[end of if]
Step 6: exit
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
Size of Multidimensional Arrays:
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.

ARRAY in python and c with examples .pptx

  • 7.
    A One-Dimensional Arrayis 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.
  • 15.
    Address of anyelement 1-D array: Example: Number of elements: UB – LB +1
  • 16.
    Solution: The given valuesare: 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 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].
  • 17.
    Take 5 integerinput from user and store then in an array and display all numbers stored in array.
  • 19.
    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.
  • 20.
  • 21.
    Traverse Linear Array TraverseLinearArray(array,n) STEPI: Set i=0 STEP 2: Repeat step 3 For i=0 to (n-1) in steps of +1 STEP 3: Process the element: (array[i]) End For STEP 4:Exit #include<stdio.h> int main() { int n=5; int a[n]= {22,55,33,1,78}; for (i=0; i<n;i++) { printf(“%d”, a[i]); } return 0; } C Code Algorithm For an array of size n, for loop executes n times. Thus traversal operation on linear arrays is O(n) operations Performance Analysis: Big O notation
  • 22.
    Linear Search Write aProgram to search an element in an array.
  • 23.
  • 24.
    Let LA bea 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 Algorithm
  • 25.
    int main() { int arr[20]; inti, item, index, size; printf("Enter %d elements: ",size); printf("Enter the size of the array"); scanf("%d",&size); printf("Enter %d elements: ",size); // reading array for (i = 0; i < size; i++) { scanf("%d",&arr[i]); } // print the original array printf("Array before insertion: "); for (i = 0; i < size; i++) { printf("%d ", arr[i]); printf("n"); } // read element to be inserted printf("Enter the element to be inserted: "); scanf("%d",&item); // read index at which element is to be inserted printf("Enter the index at which the element is to be inserted: "); scanf("%d",&index); // increase the size size++; // shift elements forward for (i = size-1; i >= index+1; i--) { arr[i] = arr[i - 1]; } // insert item at position arr[index] = item; // print the updated array printf("Array after insertion: "); for (i = 0; i < size; i++) { printf("%d ", arr[i]); } return 0; }
  • 26.
    Deletion Algorithm Consider LA isa 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 Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
  • 27.
    1.#include <stdio.h> 2.#include <conio.h> 3.intmain () 4.{ 5. // declaration of the int type variable 6. int arr[50]; 7. int pos, i, num; // declare int type variable 8. printf (" n Enter the number of elements in an array: n "); 9. scanf (" %d", &num); 10. 11. printf (" n Enter %d elements in array: n ", num); 12. 13. // use for loop to insert elements one by one in array 14. for (i = 0; i < num; i++ ) 15. { printf (" arr[%d] = ", i); 16. scanf (" %d", &arr[i]); 17. } 18. 19. // enter the index of the element to be deleted 20. printf( " Define the index of the array element where you want to delete:"); 21. scanf (" %d", &index); 23. // check whether the deletion is possible or not 24. if (index >= num+1) 25. { 26. printf (" n Deletion is not possible in the array."); 27. } 28. else 29. { 30. // use for loop to delete the element and update the index 31. for (i = index; i < num -1; i++) 32. { 33. arr[i] = arr[i+1]; // assign arr[i+1] to arr[i] 34. } 35. 36. printf (" n The resultant array is: n"); 37. 38. // display the final array 39. for (i = 0; i< num - 1; i++) 40. { 41. printf (" arr[%d] = ", i); 42. printf (" %d n", arr[i]); 43. } 44. } 45. return 0; 46.}
  • 28.
    1. Identify thecorrect 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? ARR A B C D E F G H I J Tutorial
  • 29.
    4. Suppose alibrary 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: 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 30. 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. 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
  • 30.
    Binary Search isdefined as a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. Binary Search Condition: The data structure must be sorted.
  • 31.
    In this algorithm, •Dividethe 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.
  • 32.
    Consider an arrayarr[] = {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.
  • 33.
    Second Step: Ifthe key matches the value of the mid element, the element is found and stop search. arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91}, and the target = 23.
  • 34.
    Binary_Search(a, n ,item) 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 GOTO Step 6 else if a[mid] > item set end = mid - 1 else set beg = mid + 1 [end of if] [end of loop] // 'a' is the given array, ’n’ is the size of the array,‘item' is the value to search Step 5: if pos = -1 PRINT "value is not present in the array" [end of if] Step 6: exit
  • 38.
    The total numberof 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 Size of Multidimensional Arrays: 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)
  • 39.
  • 40.
    Accessing Elements ofTwo-Dimensional Arrays in C
  • 41.
    Traversal in 2DArray 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.