[go: up one dir, main page]

0% found this document useful (0 votes)
28 views7 pages

DS&AL - Sample Answersheet

The document describes a menu driven C program to implement operations on a 1D array, including insert, delete, search, sort, and reverse elements. The program allows the user to choose an operation, perform it on the array, and display the results. Operations include inserting a new element at a specified location, deleting an element, searching for an element, sorting the array, and reversing the order of elements. The output shows examples of each operation being performed on a sample array.

Uploaded by

abhirupnath666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

DS&AL - Sample Answersheet

The document describes a menu driven C program to implement operations on a 1D array, including insert, delete, search, sort, and reverse elements. The program allows the user to choose an operation, perform it on the array, and display the results. Operations include inserting a new element at a specified location, deleting an element, searching for an element, sorting the array, and reversing the order of elements. The output shows examples of each operation being performed on a sample array.

Uploaded by

abhirupnath666
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Datastructure & Algorithm Lab (Using C)

PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year

1. Write a Menu driven C Program to implement Following operations on 1D Array:


1) INSERT ELEMENT. 2) DELETE ELEMENT. 3) SEARCH. 4) SORT. 5) REVERSE. 6) DISPLAY 7) EXIT.

Program: PRG01.c
#include<stdio.h>
#include<stdlib.h>
#define MAX 20

void display(int a[],int n)


{
int i;

for(i=0;i<MAX;i++)
{
printf(" %d ,",a[i]);
}
for(i=0;i<n;i++)
{
printf("\n\t\tA[%d]=%d",i,a[i]);
}
getch();
}
int insert(int a[],int n, int l, int e)
{
int j;

for(j=n-1;j>=l;j--)
{
a[j+1]=a[j];
}
a[j+1]=e;
return (n+1);
}
int del(int a[],int n, int l)
{
int j;
for(j=l;j<=n;j++)
{

a[j]=a[j+1];

}
return (n-1);
}
Assignment No. – I Page - 1 SUBHAS CHAKRABORTY, 22/CSE/065, </> CSE Dept, FIEM
Datastructure & Algorithm Lab (Using C)

PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year

void search(int a[],int n,int e)


{
int i,F=0;
for (i=0;i<n;i++)
{
if(a[i]==e)
{
printf("\n The Element %d is found in Position %d.\n",e,i+1);
F=1;
}
}
if(F==0)
printf("\n The Element %d is Not Present in the Array!",e);
getch();
}

void sort(int a[],int n)


{
int i,j,temp;
for (i=0;i<n;i++)
{
for (j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
}

void reverse(int a[],int n)


{
int i,j,temp;
for (i=0;i<n/2;i++)
{
temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;
}
}
Assignment No. – I Page - 2 SUBHAS CHAKRABORTY, 22/CSE/065, </> CSE Dept, FIEM
Datastructure & Algorithm Lab (Using C)

PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year

int main()
{
int i,n,l,e,ch,a[MAX];
system("CLS");
printf("\n\t ARRAY INSERT, DELETE, SEARCH, SORT, REVERSE & DISPLAY\n");
printf("\t ....................................................");
printf("\n\nEnter The no of Elements U Want: ");
scanf("%d",&n);
printf("\nEnter the Array Elements One by One:\n\n");
for(i=0;i<n;i++)
{
printf(" A[%d]=",i);
scanf("%d",&a[i]);
}
while(1)
{
system("CLS");
printf("\n\nYour Choices for Array Operations Are:-\n ");
printf("\n 1)Insert Element.\n\n 2)Delete Element.\n\n 3)Search Element.\n\n 4)Sort List\n\n 5)Reverse
List.\n\n 6)Display.\n\n 7)Exit.\n");
printf("\nEnter Ur Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the Location to Insert(1 to %d): ",n);
scanf("%d",&l);
if(l>n)
{
printf("\n\tPlease Check the location!\n");
getch();
}
else
{
printf("\nEnter the Element to Insert: ");
scanf("%d",&e);
n=insert(a,n,l-1,e);
printf("\n\nELEMENTS AFTER INSERTION:\n");
display(a,n);
}
break;

case 2: printf("\nEnter the Location to delete(1 to %d): ",n);


scanf("%d",&l);
if(l>n)
Assignment No. – I Page - 3 SUBHAS CHAKRABORTY, 22/CSE/065, </> CSE Dept, FIEM
Datastructure & Algorithm Lab (Using C)

PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year

{
printf("\n\tPlease Check the location!\n");
fflush(stdin);
getchar();
}
else
{
n=del(a,n,l-1);
printf("\n\nELEMENTS AFTER DELETION:\n");
display(a,n);
}
break;

case 3: printf("\nEnter the Element to Search: ");


scanf("%d",&e);
search(a,n,e);
break;

case 4: printf("\n\nBEFORE SORTING:\n");


display(a,n);
sort(a,n);
printf("\n\nAFTER SORTING:\n");
display(a,n);
break;

case 5: printf("\n\nBEFORE REVERSE:\n");


display(a,n);
reverse(a,n);
printf("\n\nAFTER REVERSE:\n");
display(a,n);
break;

case 6: printf("\nARRAY ELEMENTS ARE:\n");


display(a,n);
break;
case 7: exit(0);

default: printf("\n\tSORRY!!! WRONG CHOICE!\n");


fflush(stdin);
getchar();
}
}

}
Assignment No. – I Page - 4 SUBHAS CHAKRABORTY, 22/CSE/065, </> CSE Dept, FIEM
Datastructure & Algorithm Lab (Using C)

PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year

OUTPUT:

ARRAY INSERT, DELETE, SEARCH, SORT, REVERSE & DISPLAY


.…..…………………………………….....................................................
Enter The no of Elements U Want: 7

Enter the Array Elements One by One:

A[0]=99
A[1]=98
A[2]=97
A[3]=96
A[4]=95
A[5]=94
A[6]=93

Your Choices for Array Operations Are:-

1)Insert Element.

2)Delete Element.

3)Search Element.

4)Sort List

5)Reverse List.

6)Display.

7)Exit.

// INSERTION
Enter Ur Choice: 1

Enter the Location to Insert(1 to 7): 6

Enter the Element to Insert: 5

ELEMENTS AFTER INSERTION:


99 , 98 , 97 , 96 , 95 , 5 , 94 , 93 , 7803744 , 0 , 1 , 0 , -1 , -1 , 42 , 0 , 1 , 0 , 4205321 , 0 ,
A[0]=99
A[1]=98
A[2]=97
A[3]=96
A[4]=95
A[5]=5
Assignment No. – I Page - 5 SUBHAS CHAKRABORTY, 22/CSE/065, </> CSE Dept, FIEM
Datastructure & Algorithm Lab (Using C)

PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year

A[6]=94
A[7]=93

// DELETION
Enter Ur Choice: 2

Enter the Location to delete(1 to 8): 6

ELEMENTS AFTER DELETION:


99 , 98 , 97 , 96 , 95 , 94 , 93 , 7803744 , 0 , 0 , 1 , 0 , -1 , -1 , 42 , 0 , 1 , 0 , 4205321 , 0 ,
A[0]=99
A[1]=98
A[2]=97
A[3]=96
A[4]=95
A[5]=94
A[6]=93

// SEARCHING
Enter Ur Choice: 3 Enter Ur Choice: 3

Enter the Element to Search: 95 Enter the Element to Search: 100

The Element 95 is found in Position 5. The Element 100 is Not Present in the Array!

//SORTING
Enter Ur Choice: 4

BEFORE SORTING:
99 , 98 , 97 , 96 , 95 , 94 , 93 , 7803744 , 0 , 0 , 1 , 0 , -1 , -1 , 42 , 0 , 1 , 0 , 4205321 , 0 ,
A[0]=99
A[1]=98
A[2]=97
A[3]=96
A[4]=95
A[5]=94
A[6]=93

AFTER SORTING:
93 , 94 , 95 , 96 , 97 , 98 , 99 , 7803744 , 0 , 0 , 1 , 0 , -1 , -1 , 42 , 0 , 1 , 0 , 4205321 , 0 ,
A[0]=93
A[1]=94
A[2]=95
A[3]=96
A[4]=97
A[5]=98
Assignment No. – I Page - 6 SUBHAS CHAKRABORTY, 22/CSE/065, </> CSE Dept, FIEM
Datastructure & Algorithm Lab (Using C)

PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year

A[6]=99

// REVERSING
BEFORE REVERSE:
93 , 94 , 95 , 96 , 97 , 98 , 99 , 7803744 , 0 , 0 , 1 , 0 , -1 , -1 , 42 , 0 , 1 , 0 , 4205321 , 0 ,
A[0]=93
A[1]=94
A[2]=95
A[3]=96
A[4]=97
A[5]=98
A[6]=99

AFTER REVERSE:
99 , 98 , 97 , 96 , 95 , 94 , 93 , 7803744 , 0 , 0 , 1 , 0 , -1 , -1 , 42 , 0 , 1 , 0 , 4205321 , 0 ,
A[0]=99
A[1]=98
A[2]=97
A[3]=96
A[4]=95
A[5]=94
A[6]=93

// DISPLAY
Enter Ur Choice: 6

ARRAY ELEMENTS ARE:


99 , 98 , 97 , 96 , 95 , 94 , 93 , 7803744 , 0 , 0 , 1 , 0 , -1 , -1 , 42 , 0 , 1 , 0 , 4205321 , 0 ,
A[0]=99
A[1]=98
A[2]=97
A[3]=96
A[4]=95
A[5]=94
A[6]=93

// WRONG CHOICE
Enter Ur Choice: 8

SORRY!!! WRONG CHOICE!

Enter Ur Choice: 7

--------------------------------
Process exited after 810.3 seconds with return value 0
Press any key to continue . . .

Assignment No. – I Page - 7 SUBHAS CHAKRABORTY, 22/CSE/065, </> CSE Dept, FIEM

You might also like