Practical Exercises on Array Operations: Insertion, Deletion, and Search, Exams of Computer Science

Practical exercises on array operations, including inserting a new element at a given position, deleting an element, and finding the location of a given element. The exercises include the algorithm, flowchart, and code in c++ for each operation.

Typology: Exams

2017/2018

Uploaded on 08/08/2018

17BCS2898
17BCS2898 🇮🇳

1 document

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PRACTICAL-1(A)
AIM: - To Insert a new element at end as well as at a given
position.
ALGORITHM:-
1. Start
2. Define an array A of size N
3. Loop through i=N-1 to i>pos-1
4. A[pos-1] =element
5. Display array
6. Stop
FLOWCHART:-
DATA STRUCTURES LAB: - (CSP-232) UID:-17BCS2898 (G-2)
STOPA[pos-1] =
element
A[i+1] = A[i];
i++
While
i>=pos-1
Set I = N-1Input elment and pos to insertDefine int N
and Int A[N]
START
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Practical Exercises on Array Operations: Insertion, Deletion, and Search and more Exams Computer Science in PDF only on Docsity!

PRACTICAL-1(A)

AIM : - To Insert a new element at end as well as at a given

position.

ALGORITHM:-

  1. Start
  2. Define an array A of size N
  3. Loop through i=N-1 to i>pos-
  4. A[pos-1] =element
  5. Display array
  6. Stop

FLOWCHART:-

A[pos-1] =STOP element

A[i+1] = A[i]; i++

While i>=pos-

Set I = N-1Input elment and pos to insertDefine int N and Int A[N]

START

CODE :-

#include using namespace std; int main() { int A[20],N,element,pos; cout<<"Enter size of array :"; cin>>N; for(int i=0;i<N; i++) { cin>>A[i]; } cout<<"Enter the element & pos to insert :"; cin>>element>>pos; for (int i=N-1;i>=pos-1;i--) { A[i+1]=A[i]; } A[pos-1]=element; cout<<"Resultant Array :\n"; for(int i=0;i<N;i++) { cout<<A[i]; } return 0; }

FLOWCHART:-

CODE :-

#include using namespace std; int main() { int arr[50], size, i, del, count=0; cout<<"Enter array size : "; cin>>size; cout<<"Enter array elements: "; for(i=0; i<size; i++) { cin>>arr[i];

cout<<"Enter element to be delete : "; cin>>del; for(i=0; i<size; i++) { if(arr[i]==del) { for(int j=i; j<(size-1); j++) { arr[j]=arr[j+1]; } count++; break; } } if(count==0) { cout<<"Element not found..!!"; } else { cout<<"Element deleted successfully..!!\n"; cout<<"Now the new array is :\n"; for(i=0; i<(size-1); i++) { cout<<arr[i]<<" "; } } }

CODE :-

#include using namespace std; int main() { cout<<"Enter The Size Of Array: "; int size; cin>>size; int array[size], key,i; for(int j=0;j<size;j++) { cout<<"Enter "<<j<<" Element: "; cin>>array[j]; } for(int a=0;a<size;a++){ cout<<"array[ "<<a<<" ] = "; cout<<array[a]<<endl; } cout<<"Enter Key To Search in Array"; cin>>key; for(i=0;i<size;i++){ if(key==array[i]) { cout<<"Key Found At Index Number : "<<i<<endl;

break; } } if(i != size) { cout<<"KEY FOUND at index : "<<i; } else{ cout<<"KEY NOT FOUND in Array "; } return 0; }

OUTPUT:-

CODE :-

#include <stdio.h> int main() { int LA[] = {2,4,6,8,9}; int i, n = 5;

printf("The array elements are:\n");

for(i = 0; i < n; i++) { printf("LA[%d] = %d \n", i, LA[i]); } }

OUTPUT:-