0% found this document useful (0 votes)
20 views33 pages

1 8

The document describes a C program that implements various operations on arrays such as insertion, deletion, and searching of elements. It includes the source code and output of the program which allows the user to enter the size of an array, insert elements, and perform operations like inserting a new element at a given location, deleting an element at a location, and searching for a given element. The output shows the results of each operation performed on sample arrays and inputs provided by the user.

Uploaded by

Tejas Patil
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)
20 views33 pages

1 8

The document describes a C program that implements various operations on arrays such as insertion, deletion, and searching of elements. It includes the source code and output of the program which allows the user to enter the size of an array, insert elements, and perform operations like inserting a new element at a given location, deleting an element at a location, and searching for a given element. The output shows the results of each operation performed on sample arrays and inputs provided by the user.

Uploaded by

Tejas Patil
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/ 33

Roll No : 7814

Assignment No : 01
Assignment Name : A C program to perform different operations on array
(Insertion,Deletion and Searching).

#include<stdio.h>
int main()
{
int a[50],n,i;
printf("Enter size of an array\n");
scanf("%d",&n);
printf("Enter element of an array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Display element of an array\n");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
int no;
printf("\nEnter number");
scanf("%d",&no);
switch (no)
{
case 1:
printf("insertion opreation\n");
printf("Enter new element and loc\n");
int ele;
int loc1;
printf("Enter new element \n");
scanf("%d",&ele);
printf("%d",ele);
printf("Enter location \n");
scanf("%d",&loc1);
for(i=(n-1);i>=loc1;i--)
{
a[i+1]=a[i];
}
a[loc1]=ele;
printf("After adding new element");
for(i=0;i<=n;i++)
{
printf("%d",a[i]);
}
break;
case 2:
printf("Deletion opreation \n");
int loc;
printf("Enter location \n");
scanf("%d",&loc);
for(i=loc;i<n;i++)
{
a[i]=a[i+1];
}
printf("After deleting new element\n");
for(i=0;i<=n;i++)
{
printf("%d",a[i]);
}
break;
case 3:
printf("Searching opreation \n");
int no,flag=0;
printf("Enter elements to be searched:\n");
scanf("%d",&no);
for(i=0;i<n;i++)
{
if(no==a[i])
{
printf("Search successful");
flag=1;
break;
}
}
if(flag==0)
{
printf("Search unsuccessful");
}
break;
}
return 0;
}
Output :
Enter size of an array
5
Enter element of an array
5
3
6
9
8
Display element of an array
5
3
6
9
8
Enter number1
insertion opreation
Enter new element and loc
Enter new element7
Enter location2
After adding new element
5
3
7
6
9
8
Enter size of an array
5
Enter element of an array
1
5
6
3
8
Display element of an array
1
5
6
3
8
Enter number2
Deletion opreation
Enter location3
After deleting new element
1
5
6
8
Enter size of an array
5
Enter element of an array
4
6
5
3
2
Display element of an array
4
6
5
3
2
Enter number3
Searching opreation
Enter elements to be searched:
5
Search successful
Enter size of an array
5
Enter element of an array
2
3
6
4
1
Display element of an array
2
3
6
4
1
Enter number3
Searching opreation
Enter elements to be searched:
7
Search unsuccessful
Roll No : 7814
Assignment No : 02
Assignment Name : A C program to sort given elements using bubble sort.

#include<stdio.h>
int main()
{
int i,j,n,temp;
printf("Enter size\n");
scanf("%d",&n);
int a[n];
printf("Enter element\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Display elements of an array\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j+1]<a[j])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("Array after bubble sort\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
Output :
Enter size
5
Enter element
5
6
8
7
9
Display elements of an array
5
6
8
7
9
Array after bubble sort
5
6
7
8
9
Roll No : 7814
Assignment No : 03
Assignment Name : A C program to sort given elements using Insertion sort.

#include<stdio.h>
int main()
{
int i,j,temp,a[100],n;
printf("Enter size of array \n");
scanf("%d",&n);
printf("Enter element\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Display elements\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf("Insertion sort\n");
for(i=1;i<n;i++)
{
j=i;
while(j>0 &&a[j-1]>a[j])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
j--;
}
}
printf("display sort\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
return 0;
}
Output :
Enter size of array
5
Enter element
2
3
6
9
8
Display elements
2
3
6
9
8
Insertion sort
display sort
2
3
6
8
9
Roll No : 7814
Assignment No : 04
Assignment Name : A C program to sort given elements using selection sort.

#include<stdio.h>
int main()
{
int i,j,temp,n;
printf("Enter size\n");
scanf("%d",&n);
int a[n];
printf("Enter element\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Display element\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n Sorted array elements");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
return 0;
}

Output :
Enter size
5
Enter element
3
1
9
4
6
Display element
3
1
9
4
6
Sorted array elements
1
3
4
6
9
Roll No : 7814
Assignment No : 05
Assignment Name : Write a C program to search given element using Linear Search
Method.

Source Code:
#include<stdio.h>
int main()
{
int size,i,flag=0,no;
printf("How many elements are you going to insert?:");
scanf("%d",&size);
int a[size];
printf("\nEnter %d elements:",size);
for(i=0;i<size;i++)
{
scanf("%d",&a[size]);
}
printf("\nEnter number to be search:");
scanf("%d",&no);
for(i=0;i<size;i++)
{
if(a[i] == no)
{
printf("\nSearch is Successful...");
flag=1;
break;
}
}
if(flag == 0)
{
printf("\nSearch is Unsuccessful...");
}
return 0;
}
Output:
How many elements are you going to insert?:10

Enter 10 elements:10 20 30 40 50 60 70 80 90 100

Enter number to be search:60

Search is Successful...
Roll No : 7809
Assignment No : 06
Assignment Name : Write a C program to search given element using Binary Search
Method.

Source Code:
#include<stdio.h>
int main()
{
int size,i;
printf("how many elements are you going to insert?:");
scanf("%d",&size);
int arr[size];
printf("\nenter %d elements:",size);
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}

int beg=0,end=size-1,mid=0, n=0,flag=0;


printf("\n Enter element to be search:");
scanf("%d",&n);
while((beg<=end)&&(n!=arr[mid]))
{
mid=(beg+end)/2;
if (n==arr[mid])
{
printf("\n search is sucessful....");
flag=1;
break;
}
if(n<arr[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
}
if(flag==0)
{
printf("\n search is unsucessful.....");
}
return 0;
}
Output:
how many elements are you going to insert?:7

enter 7 elements:10 20 30 40 50 60 70

Enter element to be search:40

search is sucessful....
Roll No : 7814
Assignment No : 07
Assignment Name : Write a C program to implement Stack using Static
Method.

Source Code:

#include<stdio.h>
#define MAX 6

struct stack
{
int data[MAX];
int top;
}p;

int main()
{
int ch,y,i;
void PUSH(int y);
p.top=-1;
while(1)
{
printf("\n1:PUSH\n2:POP\n3:Display");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(IsFull())
{
printf("\nStack Overflow...");
}
else
{
printf("\nEnter data to PUSH:");
scanf("%d",&y);
PUSH(y);
}
break;
case 2:
if(IsEmpty())
{
printf("\nStack Underflow...");
}
else
{
y=POP();
printf("\nPoped element:%d",y);
}
break;
case 3:
if(!IsEmpty())
{
printf("\nElements in Stack are:");
for(i=p.top;i>=0;i--)
{
printf("\n%d",p.data[i]);
}
}
else
{
printf("\nStack Underflow...");
}
break;
default:
printf("\nPlease Enter correct Choice...");
}
}
return 0;
}

int IsFull()
{
if(p.top==MAX-1)
{
return(1);
}
else
{
return(0);
}
}

int IsEmpty()
{
if(p.top==-1)
{
return 1;
}
else
{
return 0;
}
}

void PUSH(int y)
{
p.top++;
p.data[p.top]=y;
}

int POP()
{
p.top--;
return(p.data[p.top+1]);
}
Output:

1:PUSH
2:POP
3:Display
Enter your choice:1

Enter data to PUSH:10

1:PUSH
2:POP
3:Display
Enter your choice:3

Elements in Stack are:


10
1:PUSH
2:POP
3:Display
Enter your choice:1

Enter data to PUSH:20

1:PUSH
2:POP
3:Display
Enter your choice:1

Enter data to PUSH:30

1:PUSH
2:POP
3:Display
Enter your choice:3

Elements in Stack are:


30
20
10
1:PUSH
2:POP
3:Display
Enter your choice:2

Poped element:30
1:PUSH
2:POP
3:Display
Enter your choice:3

Elements in Stack are:


20
10
Roll No :7814
Assignment NO :08
Assignment Name : A C program to implement Queue using static method.
Source :
# include<stdio.h>
# define max 5
struct queue
{
int front,rear;
int data [max];
}p;
int main()
{
int n ,ch;
void insert(int);
void delete1();
void display();
void exit();
p.front=p.rear=-1;
while(1)
{
printf("\n Menu");
printf("\n 1:Insert \n 2:Delete \n 3:Display \n 4:Exit");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: if(IsFull())
{
printf("\n Queue is overflow");
}
else
{
printf("\n Enter number to be inserted:");
scanf("%d",&n);
insert(n);
}
break;
case 2: if(IsEmpty())
{
printf("\n Queue is underflow");
}
else
{
delete();
}
break;
case 3: if(IsEmpty())
{
printf("Queue is underflow");
}
else
{
display();
}
break;
case 4:
exit();
default:
printf("\n Enter correct chioce");
break;
}
}
return 0;
}
int IsFull()
{
if(p.rear==max-1)
{
return 1;
}
else
{
return 0;
}
}
int IsEmpty()
{
if(p.front==-1||p.front>p.rear)
{
return(1);
}
else
{
return 0;
}
}
void insert(int no)
{
if(p.front==-1)
{
p.front=0;
p.rear=p.rear+1;
p.data[p.rear]=no;
printf("Number inserted in queue:")
}
}
void delete()
{
int temp;
temp=p.data[p.front];
p.front=p.front+1;
printf("element delete succesful");
}
void display()
{
int i;
for(i=p.front;i<=p.rear;i++)
{
printf("\t %i",p.data[i]);
}
}
Output:
Menu
1:Insert
2:Delete
3:Display
4:Exit
Enter your choice:1
Enter number to be inserted:40
Number inserted in queue:
Menu
1:Insert
2:Delete
3:Display
4:Exit
Enter your choice:2
element delete succesful
Menu
1:Insert
2:Delete
3:Display
4:Exit
Enter your choice:3
Queue is underflow
Menu
1:Insert
2:Delete
3:Display
4:Exit

You might also like