DS&AL - Sample Answersheet
DS&AL - Sample Answersheet
PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year
Program: PRG01.c
#include<stdio.h>
#include<stdlib.h>
#define MAX 20
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
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;
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;
}
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:
A[0]=99
A[1]=98
A[2]=97
A[3]=96
A[4]=95
A[5]=94
A[6]=93
1)Insert Element.
2)Delete Element.
3)Search Element.
4)Sort List
5)Reverse List.
6)Display.
7)Exit.
// INSERTION
Enter Ur Choice: 1
PCA-2 (P (PCC-CS391)
CSE – 3rd Sem. – 2nd Year
A[6]=94
A[7]=93
// DELETION
Enter Ur Choice: 2
// SEARCHING
Enter Ur Choice: 3 Enter Ur Choice: 3
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
// WRONG CHOICE
Enter Ur Choice: 8
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