Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Project File

Data Structure

From:- Satyajeet Mohanty


210BTCSEPF002
B.Tech CSE (full stack)
Taught by: Dinesh Rai
Code File

Code 1- Average of 3 numbers

#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);
}

Code 2- Linear Search Algorithm


Program

#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);
}
}

Code 3- BInary Search


Algorithm Program

#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);
}
}

Code 4- Insertion Algorithm


Program

#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");
}

Code 5- Deletion Algorithm


Program

#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]);
}
}

printf("Please enter %d element for the second matrice: \n",m*n);


for ( i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d",&B[i][j]);
}
}

for (i = 0; i < m; i++)


{
for (j = 0; j < n; j++)
{
C[i][j]=A[i][j]+B[i][j];
D[i][j]=A[i][j]-B[i][j];
}

printf("The addition of the 2 matrices is \n");


for ( i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ",C[i][j]);
}
printf("\n");
}

printf("The subtraction of the 2 matrices is \n");


for ( i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ",D[i][j]);
}
printf("\n");
}
}

Code 7- Multiplication of 2 Matrices

#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]);
}
}

printf("Please enter the rows of the 2nd matrice \n");


scanf("%d",&p);
printf("Please enter the columns of the 2nd matrice \n");
scanf("%d",&q);
printf("Please enter %d element for the second matrice: \n",p*q);
for ( i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
{
scanf("%d",&B[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];
}

}
}

printf("The new matrice is :\n");


for ( i = 0; i < m; i++)
{
for (j = 0; j < q; j++)
{
printf("%d ",C[i][j]);
}
printf("\n");

}
}

}
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]);
}

for ( i = 0; i < n; i++)


{
for ( j = 0; j < n; j++)
{
if (i<j)
{
int t=A[i][j];
A[i][j]=A[j][i];
A[j][i]=t;
}
}

printf("The transpose of the matrice is: \n");


for(i=0;i<n;i++) {
for (j = 0; j < n; j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}

Code 9- Upper and Triangular


Matrice

#include <stdio.h>

void lowerTriangle_Matrice (int a[50][50], int n)


{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i < j)
printf("0");
else
printf("%d" , a[i][j]);
printf(" ");
}
printf("\n");
}
}
void upperTriangle_Matrice (int a[50][50], int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(i > j)
printf("0");
else
printf("%d" , a[i][j]);
printf(" ");
}
printf("\n");
}
}
int main() {
int A[50][50],r,c,n;
printf("Please enter the order of the matrice: \n");
scanf("%d",&n);
printf("Please enter %d no. of elements :\n",n*n);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%d",&A[i][j]);
}

printf("Lower Triangular Matrix is :\n");


lowerTriangle_Matrice(A, n);
printf("Upper Triangular Matrix is :\n");
upperTriangle_Matrice(A, n);
return 0;
}

Code 10-Selection Sort Algorithm Program


#include <stdio.h>
void selectionSort(int A[],int n) {
for (int i = 0; i <=n-2; i++)
{
int min=A[i];
int pos=i;
for (int j = i+1; j <= n-1; j++)
{
if (A[j]<min)
{
min=A[j];
pos=j;
}

}
A[pos]=A[i];
A[i]=min;
}

void main ()
{
int arr[10],i,n;
printf("Enter the size of array\n");
scanf("%d",&n);

printf("Enter %d numbers \n",n);


for (i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}

selectionSort(arr,n);

printf("The array arranged in ascending order is given below \n");


for(i =0;i<n;i++)
{
printf("%d ",arr[i]);
}
printf("\n");
}
Code 11- Bubble Sort Algorithm Program

#include <stdio.h>

void BubSort(int A[],int n)


{
for(int i=0;i<n-1;i
++) { for (int
j=0;j<n-1;j++) {
if(A[j]>A[j+1])
{
int t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}

}
} }

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 insertionSort(int A[], int n) {


for (int i = 1; i < n; i++) {
int t = A[i];
int k = i - 1;

while (k >= 0 && t < A[k]) {


A[k + 1] = A[k];
--k;
}
A[k + 1] = t;
}
}

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);
}

Linear Search Algo Output Pic

You might also like