Advertisements
Home > Cpp > C++ Program for Array Sorting, Searching and Deletion of element by using Pointers – Q3

C++ Program for Array Sorting, Searching and Deletion of element by using Pointers – Q3


Q3. Program for Array Sorting, Searching and Deletion of element:

Perform following task with an array of N numbers:
  i) Sort the array in ascending order
 ii) Search the given number in the array & notify its position in the list if present.
iii) Delete a given no from the array from a given position

… from College notes (BCA/MCA assignments):
 

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

void Sort_Array (int *, const int);
void Search_Elm (int *, const int);
void Delete_Elm (int *, int &);

void main(){
	int *arr = new(int);
	int n, ch;

	clrscr();

	cout<<"\n How many Number of Elements in an Array?: ";
	cin>>n;
	cout<<"\n Enter "<<n<<" Elements: ";
	for(int i=0; i<n; i++)
		cin>>*(arr+i);

	while(1){
		clrscr();
		cout<<"\n Array is: ";
		for(i=0; i<n; i++)
			cout<<*(arr+i)<<" ";
		
		cout<<"\n\n Main Menu. \n";
		cout<<"\n 1 -> Sort Elements.";
		cout<<"\n 2 -> Search a Given Element.";
		cout<<"\n 3 -> Delete an Element.";
		cout<<"\n 4 -> Exit.";

		cout<<"\n Enter your choice: ";
		cin>>ch;

		switch(ch){
			case 1:
				Sort_Array(arr, n);
				break;
			case 2:
				Search_Elm(arr, n);
				break;
			case 3:
				Delete_Elm(arr, n);
				break;
			case 4:
				delete(arr);
				exit(1);
			default:
				cout<<"\n Enter Correct Choice.";
			} //  end of switch.
		getch();
		} // end of while.
	} // end of main.

void Sort_Array(int *arr, const int n){
	int temp;

	for(int i=0; i<n; i++){
		for(int j=i; j<n; j++){
			if( *(arr+i) > *(arr+j) ){
				temp = *(arr+i);
				*(arr+i) = *(arr+j);
				*(arr+j) = temp;
				}
			}
		}

	cout<<"\n Array Sorted.";
	}

void Search_Elm(int *arr, const int n){
	int elm, found=0;

	cout<<"\n Enter an Element to Search: ";
	cin>>elm;

	for(int i=0; i<n; i++){
		if( elm == *(arr+i) ){
			cout<<"\n Element "<<*(arr+i)<<" in position " 
    <<i+1;
			found = 1;
			}
		}
		if(!found)
			cout<<"\n Element not Found.";
	}

void Delete_Elm(int *arr, int &n){
	int elm, found = 0;

	cout<<"\n Enter an Element to Delete: ";
	cin>>elm;

	for(int i=0; i<n; i++){
		if( elm == *(arr+i) ){
			found = 1;
			break;
			}
		if(i >= n)
			found = 0;
		}
	for(int j=i; j<n; j++)
		*(arr+j) = *(arr+j+1);

	if(!found)
		cout<<"\n Element not Found.";
	else{
		cout<<"\n Element Deleted.";
		n--;
		}
	}

 

Output:

How many Number of Elements in an Array?: 5

Enter 5 Elements: 21
23
645
32
19

Array is: 19 21 23 32 645

Main Menu.

1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 1

Array Sorted.

Array is: 19 21 23 32 645

Main Menu.

1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 2

Enter an Element to Search: 33

Element not Found.

Array is: 19 21 23 32 645

Main Menu.

1 -> Sort Elements.
2 -> Search a Given Element.
3 -> Delete an Element.
4 -> Exit.
Enter your choice: 2

Enter an Element to Search: 32

Element 32 in position 4


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: