DSA Project 1
DSA Project 1
DSA Project 1
B. TECH
I.T 2ND
YEAR 3RD
SEM.
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
SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 3 PCC-CS 391
SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 4 PCC-CS 391
SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 5 PCC-CS 391
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;
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
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
SOUPTIK_BASU_MALLICK_11000223044 B.TECH_IT_3RD_SEM
DATA STRUCTURE & ALGORITHM 12 PCC-CS 391
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
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
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