computer lab programs
TRANSCRIPT
-
7/24/2019 computer lab programs
1/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 1
Computer Programming Lab Solutions(as per JNTU Hyderabad Syllabus)
Prepared by: Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU
Date: January 04, 2013
Email: [email protected] , Phone: 08220 172 182 (TN), 09490 456 987 (AP)
Week I (a) Write a program to find the sum of individual digits of a positive integer.
#includevoid main()
{
int num,d,sum,temp;
printf("\nEnter a positive integer : ");
scanf("%d",&num);
temp=num;sum=0;
while(num)
{d=num%10;
sum=sum+d;
num/=10;}
printf("\nSum of digits of %d is %d",temp,sum);
}
Enter a positive integer : 1234
Sum of digits of 1234 is 10
Week I (b) A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are
0 and 1. Subsequent terms are found by adding the preceding two terms in the sequence. Write a C
program to generate the first n terms of the sequence.
#includevoid main(){
int f1,f2,f3,n,i;
printf("\nHow many Fibonacci Sequence numbers you want? ");
scanf("%d",&n);
f1=0, f2=1;printf("\nThe follwing are %d Fibonacci Numbers\n",n);
-
7/24/2019 computer lab programs
2/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 2
for(i=1;i
-
7/24/2019 computer lab programs
3/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 3
Week 2 (a) Write a C program to calculate the following Sum:
Sum=1-x2/2!+x
4/4!-x
6/6!+x
8/8!-x
10/10!
#include
void main(){
float sum,term;
int n,x,i;
printf("\nSequence is 1-x^2/2!+x^4/4-x^6/6!+x^8/8!-x^10/10! . . .");
printf("\nHow many terms you want to calculate in the sequence");
printf("\nEnter x and n values\n");scanf("%d%d",&x,&n);
term=1.0;
for(i=1;i
-
7/24/2019 computer lab programs
4/55
-
7/24/2019 computer lab programs
5/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 5
Week 3 (a) Write C programs that use both recursive and non-recursive functions
(i)
To find the factorial of a given integer.
Non-Recursive
#includevoid main()
{
int num,fact;
int factorial();
printf("\nEnter a positive integer: ");
scanf("%d",&num);fact=factorial(num);
printf("\nFactorial of %d is %d",num,fact);
}
int factorial(int n)
{
int i,f;
f=1;
for(i=1;i
-
7/24/2019 computer lab programs
6/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 6
int factorial(int n){
int f;
if(n
-
7/24/2019 computer lab programs
7/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 7
Enter two positive integers24 16
GCD of 24 and 16 is 8
Enter two positive integers
35 275
GCD of 35 and 275 is 5
Non-Recursive Solution 2#include
void main()
{int a,b,val;
int GCD();
printf("\nEnter two positive integers\n");
scanf("%d%d",&a,&b);val=GCD(a,b);
printf("\nGCD of %d and %d is %d",a,b,val);}
int GCD(int x,int y)
{int i,temp;
if(x>y){
temp=x;
x=y;y=temp;
}
for(i=y;i>=1;i--)
if(x%i==0&&y%i==0)break;
return i;
}
Enter two positive integers
24 16
GCD of 24 and 16 is 8
Enter two positive integers
78 24GCD of 78 and 24 is 6
-
7/24/2019 computer lab programs
8/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 8
Recursive#include
void main()
{int a,b,val,temp;
int GCD();
printf("\nEnter two positive integers\n");scanf("%d%d",&a,&b);
val=GCD(a,b);
printf("\nGCD of %d and %d is %d",a,b,val);}
int GCD(int x,int y){
int g,temp;
if(x%y!=0)
GCD(y,x%y);else
return y;}
Enter two positive integers78 25
GCD of 78 and 25 is 1
Enter two positive integers
78 24
GCD of 78 and 24 is 6
Week 4 (b) Write a C program, which takes two integer operands and one operator from the user,
performs the operation and then prints the result. (Consider the operators +, -, *, /, % and use
Switch Statement)#include
void main(){
int a,b,val;
char op;
printf("\nEnter two integers and operator\n");
scanf("%d%d %c",&a,&b,&op);
switch(op){
case '+': val=a+b;
break;case '-': val=a-b;
break;
-
7/24/2019 computer lab programs
9/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 9
case '*': val=a*b;break;
case '/': val=a/b;
break;case '%': val=a%b;
break;
}
printf("\n %d %c %d = %d",a,op,b,val);}
Enter two integers and operator
10 12 +10 + 12 = 22
Enter two integers and operator
10 12 -10 - 12 = -2
Enter two integers and operator
10 12 *10 * 12 = 120
Enter two integers and operator22 5 /
22 / 5 = 4Enter two integers and operator
22 5 %
22 % 5 = 2
Week 5 (a) Write a C program to find both the largest and smallest number in a list of integers.#includevoid main()
{
int x[100],n,i,large,small;
printf("\nHow many values you want to enter into an array? ");
scanf("%d",&n);printf("\nEnter %d integers\n",n);
for(i=0;i
-
7/24/2019 computer lab programs
10/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 10
printf("\nArray values are\n");for(i=0;i
-
7/24/2019 computer lab programs
11/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 11
for(i=0;i
-
7/24/2019 computer lab programs
12/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 12
Matrix BContents of matrix are
1 1 1 1
2 2 2 23 3 3 3
Matrix C
Contents of matrix are
2 3 4 53 4 5 6
4 5 6 7
Enter size of matrix A : 2 3
Enter values into 2 X 3 matrix1 2 3
4 5 6
Enter size of matrix B: 2 2Enter values into 2 X 2 matrix
1 12 2
Addition of matrices is not possible
Week 5 (b) Write a C program that uses functions to perform the following:
ii) Multiplication of Two Matrices
#include
void main()
{int a[20][20],b[20][20],c[20][20];int m1,m2,m3,n1,n2,n3;
void read2D(),print2D(),multiply();
printf("\nEnter size of matrix A : ");
scanf("%d%d",&m1,&n1);
read2D(a,m1,n1);printf("\nEnter size of matrix B: ");
scanf("%d%d",&m2,&n2);
read2D(b,m2,n2);
multiply(a,m1,n1,b,m2,n2,c,&m3,&n3);
printf("\nMatrix A\n");
print2D(a,m1,n1);printf("\nMatrix B\n");
print2D(b,m2,n2);
printf("\nMatrix A x B\n");print2D(c,m3,n3);
}
-
7/24/2019 computer lab programs
13/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 13
void multiply(int a[20][20],int m1,int n1, int b[20][20],int m2,int n2,int c[20][20],int *mp,int *np)
{
int i,j,k;
if(n1!=m2)
{
printf("\nMultiplication of matrices is not possible");exit(0);
}
for(i=0;i
-
7/24/2019 computer lab programs
14/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 14
Enter size of matrix B: 4 1Enter values into 4 X 1 matrix
1 2 3 4
Matrix A
Contents of matrix are
1 2 3 41 1 1 1
Matrix B
Contents of matrix are1
2
34
Matrix A x B
Contents of matrix are29
9
Enter size of matrix A : 2 4Enter values into 2 X 4 matrix
1 2 3 4
5 6 7 8
Enter size of matrix B: 2 3
Enter values into 2 X 3 matrix4 4 4
7 7 7
Multiplication of matrices is not possible
Enter size of matrix A : 3 4
Enter values into 3 X 4 matrix
1 2 3 45 6 7 8
9 10 11 12
Enter size of matrix B: 4 5Enter values into 4 X 5 matrix
1 1 1 1 1
2 2 2 2 2
3 3 3 3 34 4 4 4 4
Matrix AContents of matrix are
1 2 3 45 6 7 8
9 10 11 12
-
7/24/2019 computer lab programs
15/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 15
Matrix BContents of matrix are
1 1 1 1 1
2 2 2 2 23 3 3 3 3
4 4 4 4 4
Matrix A x B
Contents of matrix are
29 29 29 29 29
65 65 65 65 65101 101 101 101 101
Week 6
a) Write a C program that uses functions to perform the following operations:
i) To insert a sub-string in to a given main string from a given position.
ii) To delete n Characters from a given position in a given string.
b) Write a C program to determine if the given string is a palindrome or not
To insert a sub-string in to a given main string from a given position.
#include
void main()
{char str[500],sub[100];
int n1,n2,i,j,loc;
printf("\nEnter main string: ");
scanf("%s",str);
printf("\nEnter sub string: ");scanf("%s",sub);printf("\nEnter position: ");
scanf("%d",&loc);
printf("\nMain string: %s",str);
printf("\nSub string: %s",sub);
n1=strlen(str);
n2=strlen(sub);
if(loc>n1){
printf("\nPosition is out of range");
exit(0);}
for(i=n1;i>=loc;i--)str[i+n2]=str[i];
-
7/24/2019 computer lab programs
16/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 16
j=0;while(sub[j])
{
str[loc+j]=sub[j];j++;
}
printf("\nMain string: %s",str);printf("\nSub string: %s",sub);
}
INPUT/OUTPUT
Enter main string: JntuHyderabad
Enter sub string: ASReddyEnter position: 4
Main string: JntuHyderabad
Sub string: ASReddy
Main string: JntuASReddyHyderabad
Sub string: ASReddy
Enter main string: AravindReddyJNTUH
Enter sub string: 12011U0502
Enter position: 12
Main string: AravindReddyJNTUH
Sub string: 12011U0502
Main string: AravindReddy12011U0502JNTUH
Sub string: 12011U0502
To delete n Characters from a given position in a given string.
#include
void main()
{char str[500];
int n,i,loc;
printf("\nEnter a string: ");
scanf("%s",str);
-
7/24/2019 computer lab programs
17/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 17
printf("\nEnter number of characters to be deleted: ");scanf("%d",&n);
printf("\nEnter position from which deletion should be done: ");
scanf("%d",&loc);
if(loc+n>strlen(str))
{
printf("\nDeletion not possible");printf("\nToo many characters from the given location");
exit(0);
}
i=loc;
while(str[i+n]){
str[i]=str[i+n];
i++;}
str[i]=str[i+n];printf("\nString after deletion: %s",str);
}
INPUT/OUTPUT
Enter a string: AravindReddyJNTU
Enter number of characters to be deleted: 5Enter position from which deletion should be done: 7
String after deletion: AravindJNTU
Enter a string: AravindReddyNarmetta12011U0502
Enter number of characters to be deleted: 8Enter position from which deletion should be done: 12
String after deletion: AravindReddy12011U0502
-
7/24/2019 computer lab programs
18/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 18
b) Write a C program to determine if the given string is a palindrome or not
#include#include
void main()
{
char str[500];int n,i;
printf("\nEnter a string: ");scanf("%s",str);
n=strlen(str);for(i=0;i
-
7/24/2019 computer lab programs
19/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 19
Week 7
a) Write a C program that displays the position or index in the string S where the string T begins, or
1 if S doesnt contain T.
b) Write a C program to count the lines, words and characters in a given text.
#include
void main()
{char str[500],sub[100];
int n1,n2,i,j,loc;
printf("\nEnter main string: ");
scanf("%s",str);
printf("\nEnter sub string: ");scanf("%s",sub);
i=0;while(str[i])
{j=0;
while(str[i+j]&&sub[j]&&str[i+j]==sub[j])j++;
if(sub[j]==NULL)
break;i=i+1;
}
if(sub[j]==NULL)printf("\nSub string available at %d location",i+1);
else
printf("\nSub String not available : %d",-1);}
INPUT/OUTPUT
Enter main string: AravindReddy
Enter sub string: Reddy
Sub string available at 8 location
Enter main string: AravindJNTUcse
Enter sub string: JNTU
Sub string available at 8 location
Enter main string: SrinivasReddy
Enter sub string: vasSub string available at 6 location
Enter main string: FirstYearEnter sub string: irt
Sub String not available : -1
-
7/24/2019 computer lab programs
20/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 20
Week 8 (a) Write a program to generate Pascals triangle.#include
void main()
{int x[50][50],i,j,spaces,l,n;
printf("\nThis is a program to generate Pascal triangle");
printf("\nHow many lines you want : ");scanf("%d",&n);
for(i=0;i
-
7/24/2019 computer lab programs
21/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 21
Week 8 (b) Write a C program to construct a pyramid of numbers.
(i)#include
void main(){
int i,j,l,n,sp;
printf("\nHow many lines you want ? ");scanf("%d",&n);
sp=35;
for(i=1;i
-
7/24/2019 computer lab programs
22/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 22
for(i=1;i
-
7/24/2019 computer lab programs
23/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 23
(iii)#include
void main()
{int i,j,k,l,n,spaces;
printf("\nThis is a dymand pattern program");
printf("\nEnter n value ");scanf("%d",&n);
spaces=36;
for(i=1;i
-
7/24/2019 computer lab programs
24/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 24
Week 9Write a C program to read in two numbers, x and n, and then compute the sum of this geometric
progression:
1+x+x2+x
3+.+x
n
For example: if n is 3 and x is 5, then the program computes 1+5+25+125.
Print x, n, the sum
Perform error checking. For example, the formula does not make sense for negative exponents if n is less
than 0. Have your program print an error message if n
-
7/24/2019 computer lab programs
25/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 25
void read(struct complex *p){
printf("\nEnter Complex number (real imaginary)");
scanf("%d%d",&p->real,&p->imag);}
void display(struct complex c){
printf("\nComplex number is ");
printf("\n%d %di",c.real,c.imag);}
struct complex add(struct complex x, struct complex y)
{
struct complex z;
z.real=x.real+y.real;z.imag=x.imag+y.imag;
return z;}
struct complex mult(struct complex x, struct complex y)
{
struct complex z;
z.real=x.real*y.real-x.imag*y.imag;
z.imag=x.imag*y.real+x.real*y.imag;return z;
}
Enter Complex number (real imaginary)2 3Enter Complex number (real imaginary)4 7
Enter complex numbers areComplex number is
2 3i
Complex number is
4 7i
Addition of two complex numbers
Complex number is6 10i
Multiplication of two complex numbersComplex number is
-13 26i
-
7/24/2019 computer lab programs
26/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 26
Week 12
a) Write a C program which copies one file to another.
#includevoid main(int argc,char* argv[])
{
FILE *fp1,*fp2;
char ch;
if(argc!=3)
{printf("\nUse Command Properly");
printf("\nCommand SourceFileName TargetFileName");
exit(0);}
fp1=fopen(argv[1],"r");if(fp1==NULL)
{printf("\nSource File Not Existing");
exit(1);}
fp2=fopen(argv[2],"w");ch=fgetc(fp1);
while(ch!=EOF)
{fputc(ch,fp2);
ch=fgetc(fp1);
}printf("\nFile Copied");fclose(fp1);
fclose(fp2);
}
C:\TC\BIN>dir asrc*.*Volume in drive C has no label.
Volume Serial Number is 60A8-805B
Directory of C:\TC\BIN
01/05/2013 11:46 PM 485 ASRCPY.BAK
01/05/2013 11:46 PM 485 ASRCPY.C01/05/2013 11:46 PM 12,280 ASRCPY.EXE
5 File(s) 15,883 bytes
0 Dir(s) 36,109,975,552 bytes free
-
7/24/2019 computer lab programs
27/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 27
C:\TC\BIN>asrcpy asrcpy.c aravind.cFile Copied
C:\TC\BIN>asrcpy sss.c yyy.cSource File Not Existing
C:\TC\BIN>asrcpy asrcpy.c yy.c zz.c
Use Command ProperlyCommand SourceFileName TargetFileName
C:\TC\BIN>asrcpy aravind.c jntuh.cFile Copied
C:\TC\BIN>dir jnt*.*Volume in drive C has no label.
Volume Serial Number is 60A8-805B
Directory of C:\TC\BIN
01/05/2013 11:49 PM 485 JNTUH.C
1 File(s) 485 bytes0 Dir(s) 36,109,844,480 bytes free
C:\TC\BIN>exit
b) Write a C program to reverse the first n characters in a file.
(Note: The file name and n are specified on the command line.)
Week 13
a) Write a C programme to display the contents of a file.
#includevoid main(int argc,char* argv[])
{
FILE *fp;
char ch;
if(argc!=2)
{printf("\nUse Command Properly");
printf("\nCommand FileName");
exit(0);}
-
7/24/2019 computer lab programs
28/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 28
fp=fopen(argv[1],"r");if(fp==NULL)
{
printf("\nSource File Not Existing");exit(1);
}
ch=fgetc(fp);while(ch!=EOF)
{
printf("%c",ch);ch=fgetc(fp);
}
fclose(fp);}
C:\TC\BIN>asrtype yy.c
Source File Not ExistingC:\TC\BIN>asrtype simple.c
void main(){
printf("\nWelcome to JNTU--Aravind Reddy CSE");
}C:\TC\BIN>asrtype simple.c asrtype.c
Use Command ProperlyCommand FileName
C:\TC\BIN>exit
b) Write a C programme to merge two files into a third file ( i.e., the contents of the first file followed
by those of the second are put in the third file)
#includevoid main(int argc,char* argv[])
{
FILE *fp1,*fp2,*fp3;
char ch;
if(argc!=4)
{printf("\nUse Command Properly");
printf("\nCommand SourceFileName1 SourceFileName2 TargetFileName");
exit(0);}
-
7/24/2019 computer lab programs
29/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 29
fp1=fopen(argv[1],"r");if(fp1==NULL)
{
printf("\nSource File1 Not Existing");exit(1);
}
fp3=fopen(argv[3],"w");ch=fgetc(fp1);
while(ch!=EOF)
{fputc(ch,fp3);
ch=fgetc(fp1);
}fclose(fp1);
fp2=fopen(argv[2],"r");if(fp2==NULL)
{printf("\nSource File 2 Not Existing");
fclose(fp3);exit(2);
}
ch=fgetc(fp2);
while(ch!=EOF)
{fputc(ch,fp3);
ch=fgetc(fp2);
}fclose(fp2);fclose(fp3);
}
INPUT/OUTPUT
C:\TC\BIN>asrmerg simple.c hworld.c aravind.c
C:\TC\BIN>type simple.cvoid main()
{
printf("\nWelcome to JNTU--Aravind Reddy CSE");}
-
7/24/2019 computer lab programs
30/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 30
C:\TC\BIN>type hworld.c#include
void main()
{printf("\nHello World!");
printf("\nThis is Aravind Reddy");
printf("\nRoll No. 12011U0502, CSE");
}
C:\TC\BIN>type aravind.c
void main(){
printf("\nWelcome to JNTU--Aravind Reddy CSE");
}#include
void main()
{printf("\nHello World!");
printf("\nThis is Aravind Reddy");printf("\nRoll No. 12011U0502, CSE");
}
C:\TC\BIN>exit
Week 14
Write a C program that uses functions to perform the following operations on singly linked list.:
i) Creation ii) Insertion iii) Deletion iv) Traversal
#include#include
struct node{
int data;
struct node *link;};
typedef struct node* nodeptr;
void main()
{
int val,pos;nodeptr list;
nodeptr create();
void print(),insertnth(nodeptr*,int,int);int deletenth(nodeptr*,int);
-
7/24/2019 computer lab programs
31/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 31
list=create();print(list);
printf("\nEnter Element to be inserted and position : ");
scanf("%d%d",&val,&pos);
insertnth(&list,val,pos);
print(list);
printf("\nWhich position node you want to delete? ");scanf("%d",&pos);
val=deletenth(&list,pos);
printf("\nDeleted node value is %d",val);print(list);
}
int deletenth(nodeptr *lp,int pos)
{int val=-1;
nodeptr p,q,r;
if(*lp==NULL)
printf("\nLinked List is empty, delete not possible");else{
p=*lp;
if(pos==1){
*lp=p->link;
val=p->data;free(p);
}
else
{pos=pos-2;
while(p&&pos)
{p=p->link;
pos=pos-1;
}if(p)
{
q=p->link;
p->link=q->link;
val=q->data;free(q);
}else
printf("\nLess nodes than given node number");
}}
return val;
}
-
7/24/2019 computer lab programs
32/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 32
void insertnth(nodeptr *lp,int val,int pos){
nodeptr p,q,r;
nodeptr getnode();
q=getnode();
q->data=val;
q->link=NULL;if(*lp==NULL)
*lp=q;
else{
p=*lp;
pos=pos-1;while(p&&pos)
{
r=p;p=p->link;
pos=pos-1;}
q->link=r->link;r->link=q;
}
}
nodeptr create(){
int val;
nodeptr p,q,r;nodeptr getnode();
printf("\nEnter 0 (zero) to stop\n");
scanf("%d",&val);p=getnode();
r=p;
while(val){
q=getnode();
q->data=val;
p->link=q;p=q;
scanf("%d",&val);
}p->link=NULL;
p=r->link;
free(r);return p;
}
-
7/24/2019 computer lab programs
33/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 33
void print(nodeptr p){
printf("\nContents of Linked List are\n");
while(p){
printf(" %d",p->data);
p=p->link;
}}
nodeptr getnode()
{
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
return p;}
INPUT/OUTPUT
Enter 0 (zero) to stop
11 22 33 44 55 66 77 88 99 0
Contents of Linked List are
11 22 33 44 55 66 77 88 99
Enter Element to be inserted and position : 678 6
Contents of Linked List are11 22 33 44 55 678 66 77 88 99
Which position node you want to delete? 3
Deleted node value is 33Contents of Linked List are
11 22 44 55 678 66 77 88 99
-
7/24/2019 computer lab programs
34/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 34
Week 15
Write C programs that implement stack (its operations) using
i) Arrays ii) Pointers
Arrays
#include
#define MAX 100
void main()
{int stack[MAX],top=-1,val,op;
void push(),display(),options();
int pop();
clrscr();
options();while(1)
{printf("\nSelect option : ");
scanf("%d",&op);switch(op)
{
case 1: printf("\nEnter value to be pushed : ");scanf("%d",&val);
push(stack,&top,val);
break;case 2: val=pop(stack,&top);
printf("\nValue poped is %d",val);
break;case 3: display(stack,top);
break;
case 4: exit(0);
break; //optional ie, non reachable codedefault:options();
break; //optional, last case
}}
}
void push(int stk[],int *tp,int val)
{if(*tp==MAX-1)
printf("\nStack full, %d not inserted",val);
else{
*tp=*tp+1;
stk[*tp]=val;}
}
-
7/24/2019 computer lab programs
35/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 35
int pop(int stk[],int *tp){
int val=-1;
if(*tp==-1)
printf("\nStack empty, no value deleted");
else
{val=stk[*tp];
*tp=*tp-1;
}return val;
}
void display(int stk[],int top)
{int i;
printf("\nContents of Stack are\n");
for(i=top;i>=0;i--)printf("\n\t\t%4d",stk[i]);
}
void options()
{printf("\nAvailable optons are\n");
printf("\n\t\t0. Options");
printf("\n\t\t1. Push");printf("\n\t\t2. Pop");printf("\n\t\t3. Display");
printf("\n\t\t4. Exit");
}
INPUT/OUTPUT
Available optons are
0. Options
1. Push
2. Pop3. Display
4. Exit
Select option : 1
Enter value to be pushed : 11
Select option : 1
Enter value to be pushed : 99
-
7/24/2019 computer lab programs
36/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 36
Select option : 1
Enter value to be pushed : 88
Select option : 3
Contents of Stack are
88
9911
Select option : 2Value poped is 88
Select option : 2Value poped is 99
Select option : 2Value poped is 11
Select option : 2
Stack empty, no value deletedValue poped is -1
Select option : 4
Pointers
#include
struct node{
int data;
struct node *link;};
typedef struct node* nodeptr;
void main(){
int op,val;
nodeptr sp=NULL;void push(),display(),options();
int pop();
clrscr();
options();
-
7/24/2019 computer lab programs
37/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 37
while(1){
printf("\nSelect option : ");
scanf("%d",&op);switch(op)
{
case 1: printf("\nEnter value to be pushed : ");
scanf("%d",&val);push(&sp,val);
break;
case 2: val=pop(&sp);printf("\nValue poped is %d",val);
break;
case 3: display(sp);break;
case 4: exit(0);
break; //optional ie, non reachable codedefault:options();
break; //optional, last case}
}}
void push(nodeptr *spp,int val){
nodeptr p;
nodeptr getnode();
p=getnode();
p->data=val;p->link=*spp;*spp=p;
}
int pop(nodeptr *spp){
int val=-1;
nodeptr p;
if(*spp==NULL)
printf("\nStack Empty");
else{
p=*spp;
*spp=p->link;val=p->data;
free(p);
}return val;
}
-
7/24/2019 computer lab programs
38/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 38
void display(nodeptr p){
printf("\nContents of Stack are\n");while(p)
{
printf("\n\t%4d",p->data);
p=p->link;}
}
void options()
{printf("\nAvailable optons are\n");
printf("\n\t0. Options");
printf("\n\t1. Push");printf("\n\t2. Pop");
printf("\n\t3. Display");printf("\n\t4. Exit");
}
nodeptr getnode()
{nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));return p;
}
INPUT/OUTPUT
Available optons are0. Options
1. Push
2. Pop3. Display
4. Exit
Select option : 1Enter value to be pushed : 11
Select option : 1Enter value to be pushed : 22
Select option : 1Enter value to be pushed : 44
-
7/24/2019 computer lab programs
39/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 39
Select option : 2Value poped is 44
Select option : 3Contents of Stack are
22
11
Select option : 2
Value poped is 22
Select option : 2
Value poped is 11
Select option : 2
Stack Empty
Value poped is -1
Select option : 4
Week 16
Write C programs that implement Queue (its operations) using
i) Arrays ii) Pointers
Arrays
#include
#define MAX 100
void main()
{
int val,op;int queue[MAX],front=0,rear=-1;
int delet();
void insert(),display(),options();
options();
while(1)
{printf("\nSelect option: ");
scanf("%d",&op);
switch(op){
case 1: printf("\nEnter value to be inserted: ");
scanf("%d",&val);insert(queue,&rear,val);
break;
-
7/24/2019 computer lab programs
40/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 40
case 2: val=delet(queue,&front,rear);printf("\nDeleted value is %d",val);
break;
case 3: display(queue,front,rear);break;
case 4: exit(0);
break;
default: options();break;
}
}}
int delet(int q[],int *fp,int r)
{
int val=-1;
if(*fp>r)printf("\nQueue empty");
else{
val=q[*fp];
*fp=*fp+1;}
return val;
}
void insert(int q[],int *rp,int val){
if(*rp==MAX-1)
printf("\nQueue full");
else{
*rp=*rp+1;
q[*rp]=val;}
}
void display(int q[],int f,int r)
{
int i;
printf("\nContents of queue are:\n");
for(i=f;i
-
7/24/2019 computer lab programs
41/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 41
void options()
{
printf("\nAvailable operations");printf("\n\t1. Insert");
printf("\n\t2. Delete");
printf("\n\t3. Display");
printf("\n\t4. Exit");}
Available operations
1. Insert
2. Delete3. Display
4. Exit
Select option: 1
Enter value to be inserted: 11
Select option: 1Enter value to be inserted: 66
Select option: 1Enter value to be inserted: 88
Select option: 3Contents of queue are:
11 66 88
Select option: 2Deleted value is 11
Select option: 4
Pointers
#include
#define MAX 100
struct node
{int data;
struct node *link;
};typedef struct node* nodeptr;
-
7/24/2019 computer lab programs
42/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 42
void main(){
int val,op;
nodeptr front=NULL,rear=NULL;int delet();
void insert(),display(),options();
options();while(1)
{
printf("\nSelect option: ");scanf("%d",&op);
switch(op)
{case 1: printf("\nEnter value to be inserted: ");
scanf("%d",&val);
insert(&front,&rear,val);break;
case 2: val=delet(&front,&rear);printf("\nDeleted value is %d",val);
break;case 3: display(front);
break;
case 4: exit(0);break;
default: options();
break;}
}
}
int delet(nodeptr *fp,nodeptr *rp)
{int val=-1;
nodeptr p;
if(*fp==NULL)
printf("\nQueue is Empty");
else
{p=*fp;
val=p->data;
*fp=p->link;if(*fp==NULL)
*rp=NULL;
}return val;
}
-
7/24/2019 computer lab programs
43/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 43
void insert(nodeptr *fp,nodeptr *rp,int val){
nodeptr p;
nodeptr getnode();
p=getnode();
p->data=val;
p->link=NULL;if(*rp==NULL)
*fp=*rp=p;
else{
(*rp)->link=p;
*rp=p;}
}
void display(nodeptr p){
printf("\nContents of queue are:\n");while(p)
{
printf(" %d",p->data);p=p->link;
}
}
void options(){
printf("\nAvailable operations");
printf("\n\t1. Insert");
printf("\n\t2. Delete");printf("\n\t3. Display");
printf("\n\t4. Exit");
}
nodeptr getnode(){
nodeptr p;
p=(nodeptr)malloc(sizeof(struct node));
return p;
}
-
7/24/2019 computer lab programs
44/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 44
Available operations1. Insert
2. Delete
3. Display4. Exit
Select option: 1
Enter value to be inserted: 11
Select option: 1
Enter value to be inserted: 12
Select option: 1
Enter value to be inserted: 14
Select option: 2
Deleted value is 11
Select option: 3Contents of queue are:
12 14
Select option: 2
Deleted value is 12
Select option: 2
Deleted value is 14
Select option: 2
Queue is EmptyDeleted value is -1
Select option: 3
Contents of queue are:
Select option: 4
-
7/24/2019 computer lab programs
45/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 45
Week 18
Write a C program that implements the following sorting methods to sort a given list of integers in
ascending
order
i) Bubble sort
ii) Selection sort
Bubble Sort
#include#define MAX 100
void main()
{int arr[MAX],n,temp,i,j;
void read1D(),print1D(),bubble();
printf("\nEnter number of terms: ");scanf("%d",&n);
read1D(arr,n);bubble(arr,n);
print1D(arr,n);
}
void bubble(int a[],int n)
{int i,j,temp;
for(i=0;i
-
7/24/2019 computer lab programs
46/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 46
void print1D(int arr[],int n){
int i;
printf("\nValues of array are:\n");
for(i=0;i
-
7/24/2019 computer lab programs
47/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 47
loc=lsearch(arr,n,val);
printf("\n%d availabel at %d location",val,loc);
}
int lsearch(int arr[],int n,int val)
{
int i;
for(i=0;i
-
7/24/2019 computer lab programs
48/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 48
Iterative Binary Search
#includevoid main()
{
int arr[100],n,val,loc;
void read1D();int bsearch();
printf("\nHowmany elements you want to enter? ");scanf("%d",&n);
read1D(arr,n);
printf("\nEnter element to be searched: ");
scanf("%d",&val);
loc=bsearch(arr,0,n-1,val);
printf("\n%d availabel at %d location",val,loc);}
int bsearch(int arr[],int lb,int ub,int val)
{
int mid;
while(lb
-
7/24/2019 computer lab programs
49/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 49
INPUT/OUTPUT
Howmany elements you want to enter? 7
Enter 7 values:11 33 45 67 88 90 112
Enter element to be searched: 88
88 availabel at 4 location
Howmany elements you want to enter? 5
Enter 5 values:
1 4 7 9 15Enter element to be searched: 6
6 availabel at -1 location
Howmany elements you want to enter? 10
Enter 10 values:
11 14 17 25 28 45 47 56 67 78Enter element to be searched: 56
56 availabel at 7 location
Recursive Binary Search
#includevoid main()
{
int arr[100],n,val,loc;void read1D();
int bsearch();
printf("\nHowmany elements you want to enter? ");scanf("%d",&n);
read1D(arr,n);
printf("\nEnter element to be searched: ");
scanf("%d",&val);
loc=bsearch(arr,0,n-1,val);
printf("\n%d availabel at %d location",val,loc);
}
int bsearch(int a[],int low,int high,int key){
int mid;
if(low
-
7/24/2019 computer lab programs
50/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 50
else if(keya[mid])
return bsearch(a,mid+1,high,key);}
else
return -1;
}void read1D(int arr[],int n)
{
int i;
printf("\nEnter %d values:\n",n);
for(i=0;i
-
7/24/2019 computer lab programs
51/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 51
int main(){
int arr[MAX],i,n;void quicksort(int*,int,int);
printf("\nEnter the total number of elements: ");
scanf("%d",&n);
getdata(arr,n);
quicksort(arr,0,n-1);display(arr,n);
return 0;}
void getdata(int arr[],int n)
{int i;
printf("\nEnter the elements which to be sort:\n");
for(i=0;i
-
7/24/2019 computer lab programs
52/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 52
int split(int x[10],int lower,int upper){
int pivot,j,temp,i;
pivot=lower;
i=lower;
j=upper;
while(i
-
7/24/2019 computer lab programs
53/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 53
Week 21
Write C program that implement the Merge sort method to sort a given list of integers in ascending
order:
#include
#define MAX 50
void mergeSort(int arr[],int low,int mid,int high);void partition(int arr[],int low,int high);
void getdata(int arr[],int n);
void display(int arr[],int n);
int main()
{
int arr[MAX],i,n;
printf("\nEnter the total number of elements: ");
scanf("%d",&n);
getdata(arr,n);partition(arr,0,n-1);
display(arr,n);
return 0;
}
void getdata(int arr[],int n)
{
int i;
printf("\nEnter the elements which to be sort:\n");
for(i=0;i
-
7/24/2019 computer lab programs
54/55
Srinivas Reddy Amedapu, Full Time Research Scholar, CSE Department
NATIONAL INSTITUTE OF TECHNOLOGY, TRICHY, TAMIL NADU 54
void partition(int arr[],int low,int high){
int mid;
if(low
-
7/24/2019 computer lab programs
55/55
else{
for(k=l;k