Docsity

Basic Operations in Arrays, Study notes of Computer Science

Toggle sidebar
bg1
1
Basic Operations in the Arrays
The basic operations in the Arrays are insertion, deletion, searching, display, traverse, and update.
These operations are usually performed to either modify the data in the array or to report the status
of the array.
Following are the basic operations supported by an array.
ļ‚· 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 by the value.
ļ‚· Update āˆ’ Updates an element at the given index.
ļ‚· Display āˆ’ Displays the contents of the array.
1. Insertion Operation
In the insertion operation, we are adding one or more elements to the array. Based on the
requirement, a new element can be added at the beginning, end, or any given index of array. This
is done using input statements of the programming languages.
Array Insertion Algorithm
Following is an algorithm to insert elements into a Linear Array until we reach the end of the array
āˆ’
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
Here, we see a practical implementation of insertion operation, where we add data at the end of
the array āˆ’
Example
(i) C Language
#include <stdio.h>
int main(){
int LA[3], i;
printf("Array Before Insertion:\n");
for(i = 0; i < 3; i++)
printf("LA[%d] = %d \n", i, LA[i]);
printf("Inserting Elements.. ");
printf("The array elements after insertion :\n"); // prints array values
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Basic Operations in Arrays and more Study notes Computer Science in PDF only on Docsity!

Basic Operations in the Arrays The basic operations in the Arrays are insertion, deletion, searching, display, traverse, and update. These operations are usually performed to either modify the data in the array or to report the status of the array. Following are the basic operations supported by an array. ļ‚· 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 by the value. ļ‚· Update āˆ’ Updates an element at the given index. ļ‚· Display āˆ’ Displays the contents of the array.

1. Insertion Operation In the insertion operation, we are adding one or more elements to the array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array. This is done using input statements of the programming languages. Array Insertion Algorithm Following is an algorithm to insert elements into a Linear Array until we reach the end of the array āˆ’

  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 Here, we see a practical implementation of insertion operation, where we add data at the end of the array āˆ’ Example (i) C Language #include <stdio.h> int main(){ int LA[ 3 ], i; printf("Array Before Insertion:\n"); for(i = 0 ; i < 3 ; i++) printf("LA[%d] = %d \n", i, LA[i]); printf("Inserting Elements.. "); printf("The array elements after insertion :\n"); // prints array values

for(i = 0 ; i < 3 ; i++) { LA[i] = i + 2 ; printf("LA[%d] = %d \n", i, LA[i]); } return 0 ; } Output Array Before Insertion: LA[0] = 587297216 LA[1] = 32767 LA[2] = 0 Inserting Elements.. The array elements after insertion : LA[0] = 2 LA[1] = 3 LA[2] = 4 (ii) C++ Language #include using namespace std; int main(){ int LA[ 3 ], i; cout << "Array Before Insertion:" << endl; for(i = 0 ; i < 3 ; i++) cout << "LA[" << i <<"] = " << LA[i] << endl; //prints garbage values cout << "Inserting elements.." <<endl; cout << "Array After Insertion:" << endl; // prints array values for(i = 0 ; i < 5 ; i++) { LA[i] = i + 2 ; cout << "LA[" << i <<"] = " << LA[i] << endl; } return 0 ; } Output Array Before Insertion: LA[0] = - 1834117968 LA[1] = 32767LA[2] = 0 Inserting elements.. Array After Insertion: LA[0] = 2 LA[1] = 3 LA[2] = 4 LA[5] = 0

Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 The array elements after deletion : LA[0] = 1 LA[1] = 5 (ii) C++ Language #include using namespace std; int main(){ int LA[] = { 1 , 3 , 5 }; int i, n = 3 ; cout << "The original array elements are :"<<endl; for(i = 0 ; i<n; i++) { cout << "LA[" << i << "] = " << LA[i] << endl; } for(i = 1 ; i<n; i++) { LA[i] = LA[i+ 1 ]; n = n - 1 ; } cout << "The array elements after deletion :"<<endl; for(i = 0 ; i<n; i++) { cout << "LA[" << i << "] = " << LA[i] <<endl; } } Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 The array elements after deletion : LA[0] = 1 LA[1] = 5

3. Search Operation Searching an element in the array using a key; The key element sequentially compares every value in the array to check if the key is present in the array or not. Array Search 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 find an element with a value of ITEM using sequential search.

  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 +
  6. PRINT J, ITEM
  7. Stop Example Following are the implementations of this operation in various programming languages āˆ’ (i) C Language #include <stdio.h> void main(){ int LA[] = { 1 , 3 , 5 , 7 , 8 }; int item = 5 , n = 5 ; int i = 0 , j = 0 ; printf("The original array elements are :\n"); for(i = 0 ; i<n; i++) { 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 ); } } }

4. Traversal Operation This operation traverses through all the elements of an array. We use loop statements to carry this out. Algorithm Following is the algorithm to traverse through all the elements present in a Linear Array āˆ’ 1 Start

  1. Initialize an Array of certain size and datatype.
  2. Initialize another variable ā€˜i’ with 0.
  3. Print the ith value in the array and increment i.
  4. Repeat Step 4 until the end of the array is reached.
  5. End Example Following are the implementations of this operation in various programming languages āˆ’ (i) C Language #include <stdio.h> int main(){ int LA[] = { 1 , 3 , 5 , 7 , 8 }; int item = 10 , k = 3 , n = 5 ; int i = 0 , j = n; printf("The original array elements are :\n"); for(i = 0 ; i<n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } } Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8

(ii) C++ Language #include using namespace std; int main(){ int LA[] = { 1 , 3 , 5 , 7 , 8 }; int item = 10 , k = 3 , n = 5 ; int i = 0 , j = n; cout << "The original array elements are:\n"; for(i = 0 ; i<n; i++) cout << "LA[" << i << "] = " << LA[i] << endl; return 0 ; } Output The original array elements are: LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8

5. Update Operation Update operation refers to updating an existing element from the array at a given index. Array Update 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 update an element available at the Kth position of LA.

  1. Start
  2. Set LA[K-1] = ITEM
  3. Stop Example Following are the implementations of this operation in various programming languages āˆ’ (i) C Language #include <stdio.h> void main(){ int LA[] = { 1 , 3 , 5 , 7 , 8 };

cout << "LA[" << i << "] = " << LA[i] << endl; return 0 ; } Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 The array elements after updation are : LA[0] = 1 LA[1] = 3 LA[2] = 10 LA[3] = 7 LA[4] = 8

6. Display Operation This operation displays all the elements in the entire array using a print statement. Array Display Algorithm

  1. Start
  2. Print all the elements in the Array
  3. Stop Example Following are the implementations of this operation in various programming languages āˆ’ (i) C Language #include <stdio.h> int main(){ int LA[] = { 1 , 3 , 5 , 7 , 8 }; int n = 5 ; int i; printf("The original array elements are :\n"); for(i = 0 ; i<n; i++) {

printf("LA[%d] = %d \n", i, LA[i]); } } Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8 (ii) C++ Language #include using namespace std; int main(){ int LA[] = { 1 , 3 , 5 , 7 , 8 }; int n = 5 ; int i; cout << "The original array elements are :\n"; for(i = 0 ; i<n; i++) cout << "LA[" << i << "] = " << LA[i] << endl; return 0 ; } Output The original array elements are : LA[0] = 1 LA[1] = 3 LA[2] = 5 LA[3] = 7 LA[4] = 8