Data Structre Practical File by Satyajeet Mohanty
Data Structre Practical File by Satyajeet Mohanty
Data Structure
#include <stdio.h>
void main()
{
float A,B,C,D;
printf("Enter your no.s\n");
scanf("%f%f%f",&A,&B,&C);
D=(A+B+C)/3;
printf("Your avg of 3 no.s is %f
\n",D);
}
#include <stdio.h>
void main() {
int A[50],n,x,pos=0;
printf("Enter the size of the list\n");
scanf("%d",&n);
printf("Enter %d no.s\n",n);
for (int i=0;i<n;i++) {
scanf("%d",&A[i]);
}
printf("Enter a no. to search in list
\n"); scanf("%d",&x);
for (int i = 0; i < n; i++)
{
if (x==A[i])
{
pos=i+1;
break;
}
}
if (pos==0)
{
printf("%d does not exist in the list",x);
}
else
{
printf("%d is at position %d\n",x,pos);
}
}
#include <stdio.h>
void main() {
int A[50],n,x,pos=0,i;
printf("Enter the size of the list\n");
scanf("%d",&n);
printf("Enter %d no.s in ascending order\n",n);
for (int i=0;i<n;i++) {
scanf("%d",&A[i]);
}
printf("Enter a no. to be searched\n");
scanf("%d",&x);
int beg=0,end=n-1,mid;
while (beg<=end)
{
mid=(beg+end)/2;
if (x==A[mid])
{
pos=mid+1;
break;
}
else if (x<A[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
}
if (pos==0)
{
printf("%d does not exist in the list",x);
}
else
{
printf("%d is at position %d\n",x,pos);
}
}
#include <stdio.h>
void main() {
int A[50],n,x,i,pos;
printf("Enter the size of the list\n");
scanf("%d",&n);
printf("Enter %d no.s\n",n);
for (int i=0;i<n;i++) {
scanf("%d",&A[i]);
}
printf("Enter a no. to insert in list\n");
scanf("%d",&x);
printf("Enter the position to be inserted\n");
scanf("%d",&pos);
for (i = n-1; i >= pos-1; i--)
{
A[i+1]=A[i];
}
A[pos-1]=x;
n++;
printf("The updated list is:\n");
for (i = 0; i < n; i++)
{
printf("%d ",A[i]);
}
printf("\n");
}
#include <stdio.h>
void main() {
int A[50],n,x,i,pos;
printf("Enter the size of the list\n");
scanf("%d",&n);
printf("Enter %d no.s\n",n);
for (int i=0;i<n;i++) {
scanf("%d",&A[i]);
}
printf("Enter the position to delete in list\n");
scanf("%d",&pos);
for (i =pos; i <= n-1; i++)
{
A[i-1]=A[i];
}
n--;
printf("The updated list is:\n");
for (i = 0; i < n; i++)
{
printf("%d ",A[i]);
}
printf("\n");
}
Code 6 –
Addition and matrices
subtraction of 2
#include<stdio.h>
void main() {
int A[50][50],B[50][50],C[50][50],D[50][50],i,j,m,n;
printf("Please enter the rows of the matrice \n");
scanf("%d",&m);
printf("Please enter the columns of the matrice \n");
scanf("%d",&n);
printf("Please enter %d element for the first matrice: \n",m*n);
for ( i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d",&A[i][j]);
}
}
#include<stdio.h>
void main() {
int A[50][50],B[50][50],C[50][50],i,j,m,n,p,q;
printf("Please enter the rows of the 1st matrice \n");
scanf("%d",&m);
printf("Please enter the columns of the 1st matrice \n");
scanf("%d",&n);
printf("Please enter %d element for the first matrice: \n",m*n);
for ( i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d",&A[i][j]);
}
}
if (n==p)
{
for ( i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
C[i][j]=0;
for (int k = 0; k < n; k++)
{
C[i][j]+=A[i][k]*B[k][j];
}
}
}
}
}
}
Code 8-
Matrice Transpose Of
#include<stdio.h>
void main() {
int A[50][50],i,j,n;
printf("Please enter the order of the square matrice: \n");
scanf("%d",&n);
printf("Please enter the %d elements for the matrice: \n",n*n);
for(i=0;i<n;i++) {
for (j = 0; j < n; j++)
{
scanf("%d",&A[i][j]);
}
#include <stdio.h>
}
A[pos]=A[i];
A[i]=min;
}
void main ()
{
int arr[10],i,n;
printf("Enter the size of array\n");
scanf("%d",&n);
selectionSort(arr,n);
#include <stdio.h>
}
} }
void main() {
int A[50],n;
printf("Enter no. of elements:
\n"); scanf("%d",&n);
printf("Enter %d numbers:
\n",n); for (int i = 0; i < n; i++)
{
scanf("%d",&A[i]);
}
BubSort(A,n);
printf("The sorted array is :");
for (int i = 0; i < n; i++)
{
printf("%d ",A[i]);
}
}
printf("\n");
Code 12- Insertion Sort Algorithm Program
#include <stdio.h>
void printArray(int A[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
void main() {
int A[50],n;
printf("Please enter the size of array to be created: ");
scanf("%d",&n);
printf("Please enter %d elements: \n",n);
for (int i = 0; i < n; i++)
{
scanf("%d",&A[i]);
}
insertionSort(A, n);
printf("Sorted array in ascending order:\n");
printArray(A, n);
}
Code 13- Tower of Hanoi Recursive Function Program
#include<stdio.h>
void TOH(char X,char Y,char Z,int n) {
if (n==1)
{
printf("%c->%c\n",X,Y);
return;
}
else
{
TOH(X,Z,Y,n-1);
printf("%c->%c\n",X,Y);
TOH(Z,Y,X,n-1);
}
}
void main() {
int n;
printf("Write the no.of disks to be used: \n");
scanf("%d",&n);
printf("The moves for the Tower of Hanoi game are: \n");
TOH('A','B','C',n);
}