DSA Project 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

DATA STRUCTURE & ALGORITHM [PCC-CS 391]

B. TECH
I.T 2ND
YEAR 3RD
SEM.

NAME: SOUPTIK BASU MALLICK


UNIV. ROLL: 11000223044
STREAM: INFORMATION TECHNOLOGY
DATA STRUCTURE & ALGORITHM 1 PCC-CS 391

1. Gove
PROGRAM TO TAKE INPUT AN ARRAY AND
PRINT
rnme IT:
nt
Colle
#include<stdio.h>
ge of
#include<conio.h>
Engin
int main()
eerin
{ g and
intTextil
a[100],n,i;
printf("Enter
e the size of the array:");
scanf("%d",&n);
Techn
ology,
for(i=0;i<n;i++)
{ Sera
mpor
printf("Enter the element in %d index:",i);
e
scanf("%d",&a[i]);
}
printf("\nArray elements successfully taken input!!");
printf("\n\nGiven Array is: \n");
for (i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 2 PCC-CS 391

2. PROGRAM TO INSERT AN ELEMENT AT A


PARTICULAR LOCATION IN AN ARRAY:
#include<stdio.h>
#include<conio.h>
void insert(int a[], int n, int pos, int num)
{
int i;
for (i=n;i>=pos;i--)
a[i+1] = a[i];
a[pos] =num;
if (pos>n)
printf("Insertion outside array!!");
n=n+1;
for (i=0;i<n;i++)
printf("%d\t",a[i]);
}
int main ()
{
int a[100],n,i,pos,num;
printf("Enter the size of the Array : ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the element in %d index: ",i);
scanf("%d",&a[i]);
}
printf("Enter the position in which insertion is to be done: ");
scanf("%d",&pos);
printf("Enter the number to be inserted : ");
scanf("%d",&num);
printf("Initial Array : \n");
for (i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\nFinal array after insertion: \n");
insert(a,n,pos-1,num);
getch();
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 3 PCC-CS 391

3. PROGRAM TO DELETE AN ELEMENT FROM


A PARTICULAR LOCATION IN AN ARRAY:
#include<stdio.h>
#include<conio.h>
void Delete(int a[], int n, int j)
{
int i;
while(j<n)
{
a[j-1] = a[j];
j++;
}
n=n-1;
for (i=0;i<n;i++)
printf("%d\t",a[i]);
}
int main ()
{
int a[100],n,i,j;
printf("Enter the size of the Array : ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter the element in %dindex: ",i);
scanf("%d",&a[i]);
}
printf("Enter the position from which deletion is tobedone: ");
scanf("%d",&j);
printf("Initial Array : \n");
for (i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\nFinal array after deletion : \n");
Delete(a,n,j);
getch();
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 4 PCC-CS 391

4. PROGRAM TO COPY THE ELEMENTS OF AN


ARRAY INTO ANOTHER ARRAY AND DISPLAY
THE FINAL ARRAY:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[100],b[100],len1,len2,p,k,i;
printf("Enter the size of the first Array: ");
scanf("%d",&len1);
for (i=0;i<len1;i++)
{
printf("Enter the element in %d index: ",i);
scanf("%d",&a[i]);
}
printf("Initial first Array is : \n");
for (i=0;i<len1;i++)
printf("%d ",a[i]);
printf("\nEnter the size of the second Array: ");
scanf("%d",&len2);
for (i=0;i<len2;i++)
{
printf("Enter the element in %d index: ",i);
scanf("%d",&b[i]);
}
printf("Initial second Array is : \n");
for (i=0;i<len2;i++)
printf("%d ",b[i]);
p=len1+len2; k=0;
for (i=len2;i<p;i++)
{
b[i] = a[k];
k++;

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 5 PCC-CS 391

printf("\nFinal Array after copying first array in


second array is : \n");
for (i=0;i<p;i++)
printf("%d ",b[i]);
getch();
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 6 PCC-CS 391

5. SPARSE MATRIX:
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10],B[10][3],m,n,s=1,i,j,c=0;

printf("\nEnter the order mxn of the sparse matrix: ");


scanf("%d%d",&m,&n);
printf("\nEnter the elements in the sparse matrix (mostly zeroes)\
n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("\n%d row and %d column: ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("The given matrix is:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(A[i][j]!=0)
{
c++;
B[s][2]=A[i][j];
B[s][0]=i;
B[s][1]=j;
s++;
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 7 PCC-CS 391

}
}
printf("\nThe sparse matrix is given by");
printf("\n");
B[0][0]=m;
B[0][1]=n;
B[0][2]=c;
for(i=0;i<s;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
}
getch();
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 8 PCC-CS 391

6. Write a program to represent array


elements in column major format :
#include <stdio.h>
void main ()
{
int arr[3][3],i,j;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\n printing the elements ....\n");
for(i=0;i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
{
printf("%d\t",arr[j][i]);
}
}
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 9 PCC-CS 391

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 10 PCC-CS 391

7. Write
a program to Transpose a matrix:
#include <stdio.h>
int main(int argc, char const *argv[])
{
int arr[100][100],r,c,i,j,count=0,temp;
printf("Enter the number of rows of matrix:");
scanf("%d",&r);
printf("Enter the number of columns of matrix:");
scanf("%d",&c);
for ( i = 0; i < r; i++)
{
for ( j = 0; j < c; j++)
{
scanf("%d",&arr[i][j]);
}
}
for ( i = 0; i < r; i++)
{
for ( j = i; j <c ; j++)
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
printf("The transpose of the matrix is:\n");
for ( i = 0; i < r; i++)
{
for ( j = 0; j < c; j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 11 PCC-CS 391

8. Write a program to represent polynomials


as arrays:
#include <stdio.h>
#include <math.h>
struct poly
{
float coeff;
int exp;
};
struct poly a[50];
int main()
{
int i;
int deg1;
int k = 0, l = 0, m = 0;
printf("Enter the highest degree of polynomial:");
scanf("%d", &deg1);
for (i = 0; i <= deg1; i++)
{
printf("\nEnter the coeff of x^%d :", i);
scanf("%f", &a[i].coeff);
a[k++].exp = i;
}
printf("\nPolynomial = %.1f", a[0].coeff);
for (i = 1; i <= deg1; i++)
{
printf("+ %.1fx^%d", a[i].coeff, a[i].exp);
}
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 12 PCC-CS 391

9. Write a program to perform string


operations without using library operations:
/*Program for String operations without library functions */
/* Program details:
Program showing various operations on string without library functions
functions implemented:
(1) Length of a string
(2) Reversing a string
(3) Palindrome
(4) Copy
(5) String comparison
(6) String concatenations
(7) Searching a string
(8) Counting of Words,Characters and Special characters
*/
#include <stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
//function prototypes
int length(char a[]);
void reverse(char a[]);
int palindrome(char a[]);
void copy(char a[],char b[]);
int compare(char a[],char b[]);
void concat(char a[],char b[]);
void search(char a[],char b[]);
void count(char a[]);
void main()
{
char a[100],b[100];
int result,op;
do
{ /* display the menu */
printf("\n1)length of a string");
printf("\n2)Reverse the Given String");
printf("\n3)Check for palindrome");
printf("\n4)Copy");
printf("\n5)String Comparison");

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 13 PCC-CS 391

printf("\n6)String Concatenation");
printf("\n7)String Searching");
printf("\n8)Counting of Words,Characters & Special Characters");
printf("\n9)Quit");
printf("\n\nEnter Your Choice:");
scanf("%d",&op);
getchar();
switch(op)
{
case 1:
printf("\n Enter a String:");
gets(a);
result=length(a);
printf("\n Length of %s=%d",a,result);
printf("\n\n press a Character !!!!!!");
getch();
break;
case 2:
printf("\n Enter a String:");
gets(a);
reverse(a);
printf("\n Result=%s",a);
printf("\n\n press a Character !!!!!!");
getch();
break;
case 3:
printf("\n Enter a String:");
gets(a);
result=palindrome(a);
if(result==0)
printf("\nNot a palindrome");
else
printf("\nA palindrome");
printf("\n\n press a Character !!!!!!");
getch();
break;
case 4:
printf("\n Enter a String:");
gets(a);
copy(b,a);
printf("\nResult=%s",b);
printf("\n\n press a Character !!!!!!");
getch();
break;
case 5:
printf("\n Enter 1st string:");
gets(a);
printf("\n Enter 2nd string:");
gets(b);
result=compare(a,b);
if(result==0)
printf("\nboth are same");
else
if(result>0)

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 14 PCC-CS 391

printf("\n1st>2nd");
else
printf("\n1st<2nd");
printf("\n\n press a Character !!!!!!");
getch();
break;
case 6:
printf("\n Enter 1st string:");
gets(a);
printf("\n Enter 2nd string:");
gets(b);
concat(a,b);
printf("\nresult=%s",a);
printf("\n\n press a Character !!!!!!");
getch();
break;
case 7:
printf("\n Enter 1st string:");
gets(a);
printf("\n Enter 2nd string:");
gets(b);
search(a,b);
printf("\n\n press a Character !!!!!!");
getch();
break;
case 8:
printf("\n Enter a string:");
gets(a);
count(a);
printf("\n\n press a Character !!!!!!");
getch();
break;
default :
printf("\n A wrong Choice:");break;
}
}
while(op!=9);
}
int length(char a[])
{
int i=0;
/* sting is scanned from beginning and until reaches the '\0'
character
i will give length of the string
function returns length of the string
*/
while(a[i] !='\0')
i++;
return(i);
}
void reverse(char a[])
{
int i,j;
char temp;

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 15 PCC-CS 391

/* Algorithm used is an inplace Algorithm


1. j is postioned on the last character
2. i is postioned on the first character
3. a[i] is interchanged with a[j]
4. i is increased by 1 and j is decremented by 1
5. if i<j then goto step 3
*/
i=j=0;
while(a[j]!='\0')
j++;
/* j is on the null character*/
j--;
while(i<j)
{
temp=a[i]; a[i]=a[j];a[j]=temp;
i++;j--;
}
}
int palindrome(char a[])
{
int i,j;
/* Algorith used
1. j is positioned on the last character
2. i is positioned on the first character
3. if a[i] != a[j] then it is not a palindrome,return(0)
4. i is increased by 1 and j is decremented by 1
5. if i<j then goto step 3
6. string is a palindrome , return(1)
*/
i=j=0;
while(a[j]!='\0')
j++;
/* j is on the null character*/
j--;//j is on the last character/
while(i<j)
{
if(a[i]!=a[j])
return(0);
i++;j--;
}
return(1);
}
void copy(char b[],char a[])
{
int i=0;
while(a[i]!='\0')
{
b[i]=a[i];
i++;
}
b[i]='\0';
}
int compare(char a[],char b[])
{

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 16 PCC-CS 391

int i;
/* Algorithm
1.both the strings are compared,character by character
from the beginning
2.on first point of mismatch:
a.if(a[i]>b[i]) then a>b
b.if(a[i]<b[i]) then a<b
3. if both the strings end together then they are eqaul
*/
i=0;
while(a[i]!='\0')
{
if(a[i] < b[i])
return(1);
if(a[i] > b[i])
return(-1);
i++;
}
return(0);
}
void concat(char a[],char b[])
{
int i,j;
/* Algorithm:
1. i is position on null character in string a[]
2. string b[] is appended at the end of,variable j is used
*/
i=0;
while(a[i]!='\0')
i++;
for(j=0;b[j]!='\0';i++,j++)
a[i]=b[j];
a[i]='\0';
}
void search(char a[] ,char b[])
{
int i,j,lena,lenb;
/*Algorithm
1. lenb=length of string b[],lena=length of a[]
string a[] is scanned using the variable i from location
0 to length of a[]-lenb+1
2. string b[] is matched in string a[] from the position i
*/
for(lena=0;a[lena]!='\0';lena++);
for(lenb=0;b[lenb]!='\0';lenb++);
/* searching */
for(i=0;i<=lena-lenb+1;i++)
{
for(j=0;a[i+j]==b[j]&&b[j]!='\0';j++);
if(b[j]=='\0')
printf("\nstring found at location:%d",i+1);
}
}
void count(char a[])

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 17 PCC-CS 391

{
int words=0,characters=0,spchar=0,i;
/*algorith
1. function is isalnum() chaecks whether the current character
is an alphabet or a digit
2. if the current character is an alphanumeric and previous
charater is not an alphanumeric then it is a word
3. a word can start from beginning
*/
for(i=0;a[i]!='\0';i++)
{
if(isalnum(a[i]) && (i==0 || !isalnum(a[i-1])))
words++;
characters++;
if(!isalnum(a[i]) && !isspace(a[i]))
spchar++;
}
printf("\n no of characters=%d",characters);
printf("\n no of special characters=%d",spchar);
printf("\n no of words=%d",words);
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 18 PCC-CS 391

10. MENU BASED MATRIX PROGRAM:


#include<stdio.h>
#include<stdlib.h>
// function to add two 3x3 matrix
void add(int m[3][3], int n[3][3], int sum[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
sum[i][j] = m[i][j] + n[i][j];
}
void subtract(int m[3][3], int n[3][3], int result[3][3])
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
result[i][j] = m[i][j] - n[i][j];
}
// function to multiply two 3x3 matrix
void multiply(int m[3][3], int n[3][3], int result[3][3])
{
for(int i=0; i < 3; i++)
{
for(int j=0; j < 3; j++)
{
result[i][j] = 0; // assign 0
// find product
for (int k = 0; k < 3; k++)
result[i][j] += m[i][k] * n[k][j];
}
}
}
// function to find transpose of a 3x3 matrix
void transpose(int matrix[3][3], int trans[3][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
trans[i][j] = matrix[j][i];
}
// function to display 3x3 matrix
void display(int matrix[3][3])
{
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
printf("%d\t",matrix[i][j]);
printf("\n"); // new line
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 19 PCC-CS 391

}
// main function
int main()
{
// matrix
int a[][3] = { {5,6,7}, {8,9,10}, {3,1,2} };
int b[][3] = { {1,2,3}, {4,5,6}, {7,8,9} };
int c[3][3];
// print both matrix
printf("First Matrix:\n");
display(a);
printf("Second Matrix:\n");
display(b);
//variable to take choice
int choice;
//menu driven
do
{
// menu to choose the operation
printf("\nChoose the matrix operation,\n");
printf("----------------------------\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Transpose\n");
printf("5. Exit\n");
printf("----------------------------\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
add(a, b, c);
printf("Sum of matrix: \n");
display(c);
break;
case 2:
subtract(a, b, c);
printf("Subtraction of matrix: \n");
display(c);
break;
case 3:
multiply(a, b, c);
printf("Multiplication of matrix: \n");
display(c);
break;

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 20 PCC-CS 391

case 4:
printf("Transpose of the first matrix: \n");
transpose(a, c);
display(c);
printf("Transpose of the second matrix: \n");
transpose(b, c);
display(c);
break;
case 5:
printf("Thank You.\n");
exit(0);
default:
printf("Invalid input.\n");
printf("Please enter the correct input.\n");
}
}while(1);
return 0;
}

SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy