COMPUTER PROGRAMMING LAB

Objectives:
   To make the student learn a programming language. To teach the student to write programs in C solve the problems To Introduce the student to simple linear and non linear data structures such as lists, stacks, queues, trees and graphs.

Recommended Systems/Software Requirements:

  Intel based desktop PC ANSI C Compiler with Supporting Editors Week l a) Write a C program to find the sum of individual digits of a positive integer. 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. c) Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. Week 2 a) Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! b) Write a C program toe find the roots of a quadratic equation. Week 3 a) Write C programs that use both recursive and non-recursive functions i) To find the factorial of a given integer. ii) To find the GCD (greatest common divisor) of two given integers. iii) To solve Towers of Hanoi problem. Week 4 a) The total distance travelled by vehicle in ‘t’ seconds is given by distance= ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.) and acceleration (m/sec2). Write C program to find the distance travelled at regular intervals of time given the values of ‘u’ and ‘a’. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of ‘u’ and ‘a’. b) Write a C program, which takes two integer operands and one operator form the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use Switch Statement)

Week 5 a) Write a C program to find both the larges and smallest number in a list of integers. b) Write a C program that uses functions to perform the following: i) Addition of Two Matrices ii) Multiplication of Two Matrices Week 6 a) Write a C program that uses functions to perform the following operations: i) To insert a sub-string in to 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 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 doesn’t contain T. b) Write a C program to count the lines, words and characters in a given text. Week 8 a) Write a C program to generate Pascal’s triangle. b) Write a C program to construct a pyramid of numbers. Week 9 Write a C program to read in two numbers, x and n, and then compute the sum of this geometric progression: 1+x+x2+x3+………….+xn 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<0, then go back and read in the next pair of numbers of without computing the sum. Are any values of x also illegal ? If so, test for them too. Week 10 a) 2’s complement of a number is obtained by scanning it from right to left and complementing all the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100. Write a C program to find the 2’s complement of a binary number. b) Write a C program to convert a Roman numeral to its decimal equivalent.

Week 11 Write a C program that uses functions to perform the following operations: i) Reading a complex number ii) Writing a complex number iii) Addition of two complex numbers iv) Multiplication of two complex numbers (Note: represent complex number using a structure.) Week 12 a) Write a C program which copies one file to another. 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 Write a C program that uses functions to perform the following operations on singly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal Week 14 Write a C program that uses functions to perform the following operations on doubly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways Week 15 Write C programs that implement stack (its operations) using i) Arrays ii) Pointers Week 16 Write C programs that implement Queue (its operations) using i) Arrays ii) Pointers Week 17 Write a C program that uses Stack operations to perform the following: i) Converting infix expression into postfix expression ii) Evaluating the postfix expression Week 18 Write a C program that uses functions to perform the following: i) Creating a Binary Tree of integers ii) Traversing the above binary tree in preorder, inorder and postorder.

Week 19 Write C programs that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers : i) Linear search ii) Binary search Week 20 Write C programs that implement the following sorting methods to sort a given list of integers in ascending order: i) Bubble sort ii) Quick sort Week 21 Write C programs that implement the following sorting methods to sort a given list of integers in ascending order: i) Insertion sort ii) Merge sort Week 22 Write C programs to implement the Lagrange interpolation and Newton- Gregory forward interpolation. Week 23 Write C programs to implement the linear regression and polynomial regression algorithms. Week 24 Write C programs to implement Trapezoidal and Simpson methods.

Text Books:
1. C programming and Data Structures, P. Padmanabham, Third Edition, BS Publications 2. Data Structures: A pseudo code approach with C, second edition R.F. Gilberg and B.A. Forouzan 3. Programming in C, P.Dey & M. Ghosh, Oxford Univ.Press. 4. C and Data Structures, E Balaguruswamy, TMH publications.

Description: Sum of the individual digits means adding all the digits of a number Ex: 123 sum of digits is 1+2+3=6 Algorithm: Step 1: start Step 2: read n Step 3: initialize the s=0 Step 4: if n<0 goto Step 7 Step 5: if n!=0 goto Step 6 else goto step 7 Step 6: store n%10 value in p Add p value to s Assign n/10 value to n Goto Step 5 Step 7: print the output Step 8:stop .Week l AIM :Write a C program to find the sum of individual digits of a positive integer.

if(n<0) printf("The given number is not valid").Enter the value for n: 4733 Sum of individual digits is 17 3.s. printf("enter the vaue for n:\n"). s=0.…………tn be terms in fibinacci sequence t1=0. s=s+p.t2. Write a C program to generate the first n terms of the sequence.p. } printf("sum of individual digits is %d". n=n/10.Program: #include<stdio.h> void main() { int n. Description :A fibonacci series is defined as follows . Enter the value for n: -111 The given number is not valid AIM: A Fibonacci Sequence is defined as follows: the first and second terms in the sequence are 0 and 1.&n). clrscr(). else { while(n!=0) /* check the given value =0 or not */ { p=n%10. Subsequent terms are found by adding the preceding two terms in the sequence.Formula: let t1. scanf("%d".The first term in the sequence is 0 The second term in the sequence is 1.Enter the value for n: 333 Sum of individual digits is 9 2.s).The sub sequent terms 1 found by adding the preceding two terms in the sequence . } Output: 1. t2=1 tn=tn-2+tn-1……where n>2 . } getch().

b=1 Step 3: read n Step 4: if n== 1 print a go to step 7. b go to step 7 else print a. If not goto step 7 c=a+b print c a=b b=c increment I value goto step 6(i) Step 7: stop Flowchart: . else goto step 5 Step 5: if n== 2 print a.algorithm: Step 1: start Step 2: initialize the a=0.b Step 6: initialize i=3 i)if i<= n do as follows.

h> void main() { int a. scanf("%d".b If n ==2 Output a.&n).i.b False False False I=2 i++ C = a+b Output c Stop A=b B= c Program: #include<stdio.n.START A=0. . printf("enter n value"). clrscr().c.b. a=0.b=1 Read n True If n ==1 True Output a Output a.

i<n. 3.b). a=b.7. //LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS PRINTED IN ADVANCE for(i=2. } getch(). Enter n value : -6 01 AIM: Write a C program to generate all the prime numbers between 1 and n.5.b=1. else { printf("%d%d".a. if(n==1) printf("%d". Description: Prime number is a number which is exactly divisible by one and itself only Ex: 2. printf("%d". Enter n value : 7 0 112358 3. Algorithm: Step 1: start Step 2: read n . where n is a value supplied by the user.b). b=c.a). else if(n==2) printf("%d%d".……….a. Enter n value : 5 01 1 2 3 2.c).i++) { c=a+b. } } Output: 1.

Step 3: initialize i=1.c=0 Step 4:if i<=n goto step 5 If not goto step 10 Step 5: initialize j=1 Step 6: if j<=1 do as the follow. If no goto step 7 i)if i%j==0 increment c ii) increment j iii) goto Step 6 Step 7: if c== 2 print i Step 8: increment i Step 9: goto step 4 Step 10: stop Flow chart: .

Start Read n false I=1 I<=n false J=1 J++ false true false If I % j == 0 If fact==2 Output fact Fact ++ true stop Program: #include<stdio.fact.j. clrscr(). printf("enter the number:").h> #include<conio. .h> void main() { int n.i.

n=0.i)/fact(i)) Step 6: print s value Step 7: stop Sub program: Step 1: while x!=0 goto Step 2 Step 2: y=y+x. //THIS LOOP WILL CHECK A NO TO BE PRIME NO.n. x— Step 3: return y Step 4: return to main program .i<=n.&n). n++ goto step 5 Step 5: s=s+(pow(-1.j<=i.scanf("%d".n)*pow(x. OR NOT.j++) { if(i%j==0) fact++.s=0.i). } getch( ).i. i<=10. i=i+2. for(j=1. } Output: Enter the number : 5 2 35 Enter the number : 10 2 35 7 Enter the number : 12 2 3 5 7 11 Week 2 AIM: Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! Algorithm: main program: Step 1: start Step 2: declare x.c Step 3: read x value Step 4: for i=0 . for(i=1. } if(fact==2) printf("\n %d".i++) { fact=0.

c S=0 I=0.n=0 i<=10 S=s+(pow(-1.n)*pow(x.n.i) / fact(i)) Print s Stop Sub Program Fact () While x!=0 Y=y*x x-- Return y Return to main program .Flowchart: Start Read x.I.

} /* calling sub program*/ long fact(int x) { long int y=1. float s=0. printf("\n enter the value of x\t"). while(x!=0) { y=y*x.n.i=i+2. x--.n++) s=s+(pow(-1. } Output: 1.AIM: Write a C program toe find the roots of a quadratic equation. void main() { int x.n)*pow(x.416155 . scanf("%d".&x).n=0.h> #include<math. Description: Nature of roots of quadratic equation can be known from the quadrant = b2-4ac If b2-4ac >0 then roots are real and unequal If b2-4ac =0 then roots are real and equal If b2-4ac <0 then roots are imaginary Program: #include<stdio.i.s).540302 2 Enter the value of x: 2 The result is -0. } return y.h> long fact(int). printf("\n the result is %f".c. /*perform the looping operation*/ for(i=0.i)/fact(i)).Enter the value of x : 1 The result is 0.i<=10. getch(). clrscr().

0.0.b. Goto step 7.5))/2*a Step 4: if b*b-4ac=0 then Root1 = Root2 = -b/(2*a) Step 5: Otherwise Print Imaginary roots.c value Step 3: if b*b-4ac>0 then Root 1= (-b+ pow((b*b-4*a*c).Algorithm: Step 1: start Step 2: read the a.5))/2*a Root 2= (-b-pow((b*b-4*a*c). Step 6: print roots Step 7: stop Flowchart: .

h> void main() { float a.Start Read a.5 false If d > 0 true R1 = ((-b+D) / (2*a)) R1=-b / (2 * a ) If d== 0 R2 = ((-b-D) /(2*a)) Output R1.b.r1.c. R2 Imaginary roots Stop Program: #include<stdio.h> #include<math.0. printf("Enter the values for equation:"). clrscr().r2.d. .c D = pow(b*b-4*a*c).b.

} . else { d=b*b-4*a*c.&a.0000 2.&b.0000 Root= -3. 7.&c). } else if(d==0) { r1=-b/(2*a). /* check the condition */ if(d>0) { r1=(-b+sqrt(d)/(2*a)).r2). printf("%f\n%f\n". } getch(). printf("roots are real and equal\n").25 scanf("%f%f%f". printf("root=%f\n". Enter the values for equation: 2. Enter the values for equation: 1.Output: 1. } else printf("roots are imaginary"). printf("root=%f\n". 6 Roots are real and unequal Root= -6. 9 Roots are real and equal Root= -3. 6. r2=(-b-sqrt(d)/(2*a)).75 Root= -7. r2=-b/(2*a).r2).r1).r1. printf("roots are real and unequal\n"). /* check the condition */ if(a==0) printf("Enter value should not be zero ").

Enter the values for equation: 1.3. 3 Roots are imaginary Week 3 AIM: Write C programs that use both recursive and non-recursive functions i) To find the factorial of a given integer. 2. Description:Factorial of a number is nothing but the multiplication of numbers from a given number to 1 Algorithm: main program: Step 1: start Step 2: read n Step 3: call sub program as f=fact(n) Step 4: print f value Step 5: stop Sub program: Step 1: initialize the f Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3 Step 3: return n*fact(n-1) to main program Flowchart: .

Start Read n Call subprogram F = fact(n) output Stop Sub program Fact () true Return Return to main program If n=0 || n=1 false .

fact(n)). } Output: 1. //calculate the factorial of n return(f). else f=n*fact(n-1).h> int fact(int n) { int f. Description:Factorial of a number is nothing but the multiplication of numbers from a given number to 1 Ex: 5! =5*4*3*2*1= 120 Algorithm: main program: Step 1: start Step 2: read n Step 3: call the sub program fact(n) Step 4: print the f value Step 5: stop . } void main() { int n. Enter the number : 5 Factorial of number: 120 2. printf("factoria of number%d". if((n==0)||(n==1)) // check the condition for the n value return(n). scanf("%d".Program: #include<stdio. getch().h> #include<conio. Enter the number : 3 Factorial of number: 6 3.&n). clrscr(). printf("enter the number :"). Enter the number : 9 Factorial of number: -30336 AIM: Program that use non recursive function to find the factorial of a given integer.

If not goto step 3 Step 3: perform the looping operation as follows For i=1 i<=n.Sub program: Step 1: initialize the f=1 Step 2: if n==0 or n=1 return 1 to main program. i++ Step 4: f=f*i Step 5: return f value to the main program Flowchart: .

Factorial nonrecursive

start

Read i Call subprogram Fact(n) Print output Value of fact Stop Sub program Fact ( ) F = 1, i
If n == 0 || I=1 i++

Return to main program F=f*i

Program: #include<stdio.h> #include<conio.h> int fact(int n) //starting of the sub program { int f=1,i; if((n==0)||(n==1)) // check the condition for n value return(1); else for(i=1;i<=n;i++) // perform the looping operation for calculating the factorial f=f*i; return(f); } void main() { int n; clrscr(); printf("enter the number :"); scanf("%d",&n); printf("factoria of number%d",fact(n)); getch(); } AIM : To find the GCD of two given integers by using the recursive function Description: GCD means Greatest Common Divisor. i.e the highest number which divides the given number Ex: GCD(12,24) is 12 Formula: GCD= product of numbers/ LCM of numbers Output: 1.Enter the number: 7 Factorial of number: 5040 2. Enter the number: 6 Factorial of number: 720 3. Enter the number: 8 Factorial of number: -25216

Algorithm: main program: Step 1: start Step 2: read a,b Step 3: call the sub program GCD(a,b) for print the value Step 4: stop Sub program: Step 1: if n>m return GCD(n,m) Step 2: if n==0 return m else goto step 3 Step 3: return GCD (n,m%n) Step 4: return to main program Flowchart:
Start Read a,b Call sub program Print gcdvalue Stop

Gcd( )

false

If n>m true If n==0

true F

Return Gcd (n,m) Return m Call the same function Return gcd ( n,m%n) Return to main program

Program: #include<stdio.h> #include<conio.h> int gcdrecursive(int m,int n) // starting of the sub program { if(n>m) return gcdrecursive(n,m); if(n==0) return m; else return gcdrecursive(n,m%n); // return to the main program } void main() { int a,b,igcd; clrscr(); printf("enter the two numbers whose gcd is to be found:"); scanf("%d%d",&a,&b); printf("GCD of a,b is %d",gcdrecursive(a,b)); // return to the sub program getch(); }

enter the two numbers whose gcd is to be found:11. i.b is : 5 2.24) is 12 Formula: GCD= product of numbers/ LCM of numbers Algorithm: Main program : Step 1: start Step 2: read a. q.e the highest number which divides the given number Ex: GCD(12.54 GCD of a.Output: 1.b) output stop .13 GCD of a. enter the two numbers whose gcd is to be found:5.b is : 18 3.25 GCD of a. b Call subprogram g=gcd(a.b Step 3: call sub program g=GCD(a.remainder) return to main program Flowchart: start Read a. enter the two numbers whose gcd is to be found:36. remainder Step 2: remainder=p-(p/q*q) Step 3: remainder=0 return q else goto step 4 Step 4: GCD(q.b is : 1 AIM : To find the GCD of two given integers by using the non recursive function Description:GCD means Greatest Common Divisor.b) Step 4: print the g value Step 5: stop Sub program: Step 1: initialize the p=1.

gcdnonrecursive(a.igcd. printf("GCD of %d".b)).h> #include<conio.&a. } . } Program: #include<stdio.int n) { int remainder.h> #include<math. else gcdnonrecursive(n. getch().remainder) Return q Return to main program void main() { int a. if(remainder==0) return n.remainder). scanf("%d%d".h> int gcdnonrecursive(int m. printf("enter the two numbers whose gcd is to be found:"). remainder=m-(m/n*n).&b).Gcd ( ) Remainder=p-(p/q*q) false If remainder==0 True Gcd(q. clrscr().b.

b. for this we use intermediate tower. intermediate=c. c. Here the restriction is not to place a big disk on smaller one . c.Output: 1. enter the two numbers whose gcd is to be found:36. b) Step 2: print the output from a to b Step 3: call the sub program Hanoi recursion(num-1. Algorithm: main program: Step 1: start Step 2: initialize the source=a.b is : 1 AIM: To solve the towers of Hanoi problem by using the recursive function Description:Towers of Hanoi problem means we have three towers Source intermediate destination Sub program: Step 1: if n== 1 call the sub program Hanoi recursion (num-1.25 GCD of a.intermediate and destination are the three towers. enter the two numbers whose gcd is to be found:5.b is : 5 2. c) Step 5: stop .13 GCD of a.b.54 GCD of a. Finally the arrangements in the destination tower must be as same as the disks in the source tower at first.b is : 18 3. We have to transfer all the disks from source to destination towers.a . a. a) Step 4: return to main program Here source . destination = d Step 3: read n Step 4: call the sub program Hanoi recursion (n value. enter the two numbers whose gcd is to be found:11.

intermediate.C.C Print A. B Stop hanoirecursive( ) false If num==1 true Call sbgroram Print A.A)) Return to main program .C.B.destination) A.source.C Call ubprogram Hanoi(num-1.Flowchart: START SOURCE = A INTERMEDIATE = C READ n Call subprogram Hanoi(num.

&no). printf("Enter the no.ndl2).char ndl1.ndl3. Hanoirecursion(num-1.ndl1). Hanoirecursion(no.char ndl2.'B'.h> #include<conio.ndl2.ndl1. scanf("%d".char ndl3) { if(num==1) { printf("Move top disk from needle %c to needle %c".Program: #include<stdio. Enter the no. of disk to be transferred :3 Move top disk from needle a to needle b Move top disk from needle a to needle c Move top disk from needle b to needle c Move top disk from needle a to needle b Move top disk from needle c to needle a Move top disk from needle c to needle b Move top disk from needle a to needle b .'C').ndl3. printf("Move top dis from needle %c to needlle %c". clrscr(). of disk to be transferred:").ndl2). } void main() { int no. getch(). } Hanoirecursion(num-1. } Outputs: 1.h> void Hanoirecursion(int num. else printf("\n recursive"). if(no<1) printf("\n There's nothing to move").ndl1.ndl1. return.'A'.ndl2).

indl.’B’.A’. Here the restriction is not to place a big disk on smaller one .stkadd[ ].. for this we use intermediate tower.intermediate and destination are the three towers.add Step 2: declare the top=NULL Step 3: one: If(num==1)then Print the out put value Goto four Step 4: two: Top=top+1 Stkn[top]=num Stksndl[top]=sndl Stkindl[top]=indl Stkdndl[top]=dndl Stkadd[top]=3 Num=num-1 Sndl=sndl .sndl.stkdndl [ ].stksndl[ ].’C’) Step 6:stop Sub program: Step 1: Declare num.AIM: To solve the towers of Hanoi problem by using the non recursive function Description:Towers of Hanoi problem means we have three towers Source intermediate destination Here source . temp.dndl. stkn[ ]. Algorithm: Step 1: start Step 2: declare the no Step 3: read the no value Step 4: if (no<1) Print nothing to move Else Print nonrecursion Step 5:Hanoi non recursion(no. We have to transfer all the disks from source to destination towers.top. Finally the arrangements in the destination tower must be as same as the disks in the source tower at first.

Temp=indl Indl=dndl Dndl=temp Goto one. Goto step 3 Step 6: Four: If(top==NULL) Return to main program Num= stkn[top] Sndl= stksndl[top] Indl= stkindl[top] Dndl=stkdndl[top] Add=stkadd[top] Top=top-1 If(add==3) Goto three. Goto step 5 Else If(add==5) Goto four. Goto step 6. Goto step 3 Step 5: Three: Print the output Top=top+1 Stkn[top]=num Stksndl[top]=sndl Stkindl[top]=indl Stkdndl[top]=dndl Stkadd[top]=5 Num=num-1 temp=sndl sndl=indl Indl=temp Dndl=dndl Goto one. Step 7: return to main program .

.

stkdndl [ ].Hanoi nonrecursion ( ) Declare num. Top = NULL one If num==1 true two Print the value Top=top+1 Stkn[top]=num Stksndl[top]=sndl Stkindl[top]=indl Stkdndl[top]=dndl Stkadd[top]=3 Print value Top=top+1 Stkn[top]=num Stksndl[top]=sndl Stkindl[top]=indl Stkdndl[top]=dndl Stkadd[top]=5 Num=num-1 If add==5 If add==3 If top=NULL four Num= stkn[top] Sndl= stksndl[top] Indl= stkindl[top] Dndl=stkdndl[top] false .sndl.dndl.stksndl[ ].stkadd[ ]. stkn[ ].indl.

dndl=dndl.sndl. int top.Program: #include<stdio.stkadd[100].add. num=num-1. indl=dndl.sndl.dndl).char dndl) { char stkn[100]. temp=sndl. } two: top=top+1.char sndl. temp=indl. stkadd[top]=3. stkadd[top]=5. stksndl[top]=sndl. sndl=sndl.stksndl[100]. goto four.char indl. top=NULL.h> void Hanoinonrecursion(int num. top=top+1.temp. stkdndl[top]=dndl.stkdndl[100]. indl=temp. stksndl[top]=sndl. num=num-1. stkdndl[top]=dndl. . stkn[top]=num.stkindl[100]. goto one.h> #include<conio. sndl=indl. three: printf("\n Move top disk from needle %c to needle %c". dndl=temp. one: if(num==1) { printf("\n Move top disk from needle %c to needle %c". stkn[top]=num. stkindl[top]=indl.dndl). stkindl[top]=indl.

} Output: 1. add=stkadd[top]. printf("Enter the no.&no).goto one. scanf("%d".'B'. num=stkn[top].'A'. else printf("\n nonrecursive"). Hanoinonrecursion(no. if(add==3) goto three. of diss to be transferred:"). if(no<1) printf("\n There's nothing to move"). sndl=stksndl[top]. getch(). } void main() { int no. top=top-1.Enter the no. else if(add==5) goto four. four: if(top==NULL) return. dndl=stkdndl[top].'C'). indl=stkindl[top]. clrscr(). of diss to be transferred:3 nonrecursive Move top disk from needle A to needle C Move top disk from needle A to needle B Move top disk from needle C to needle B Move top disk from needle A to needle C Move top disk from needle B to needle A Move top disk from needle B to needle C Move top disk from needle A to needle C .

Week 4 AIM: The total distance travelled by vehicle in ‘t’ seconds is given by distance= ut+1/2at2 where ‘u’ and ‘a’ are the initial velocity (m/sec.a Step 6: set s to u*k+0.) and acceleration (m/sec2). Algorithm: Step 1:Start Step2 : Read t .5*d*k*k Step 7: Write s Step 8: If(k<=t) and i=1 then Begin Step 8. Description: The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).1 go to step 6 And ElseBegin Step 8.3.2 :read Step 8.3: go to step 4 End Step 9: Stop .3 :if(j=0) then Begin Step 8. Write C program to find the distance travelled at regular intervals of time given the values of ‘u’ and ‘a’.1:Set I to 0 End ElseBegin Step 8. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of ‘u’ and ‘a’.3.dt Step 3: Set i to 1 Step 4:Set k to dt Step 5: Read u.2: Set I to 1 Step 8.3.

Step 10: End Flowchart: .

s).t1. } Output: 1.&t. % and use Switch Statement) Description:To take the two integer operands and one operator from user to perform the some arithmetic operations by using the following operators like +. float s.i<=t2.u. % Ex: 2+3=5 . scanf("%d%d%d%d%d".ENTER THE VALUES OF a.t2:").000000 AIM: Write a C program.-.i.*.&t2).t.h> void main() { int a. clrscr().t.000000 2. for(i=t1.-.t.u. // calculate the total distance printf("\n\nthe distance travelled in %d seconds is %f ".t1.5*a*i*i).u.&u.i=i+t) // performing the looping operation for time intervals { s=(u*i)+(0.Program: #include<stdio.500000 the distance travelled in 4 seconds is 16.t. printf("ENTER THE VALUES OF a. /.*.u.&a.t1.i.t1. performs the operation and then prints the result.t2:0 1 2 3 4 the distance travelled in 3 seconds is 3.t2. } getch(). which takes two integer operands and one operator form the user. /.ENTER THE VALUES OF a.t2:1 2 3 1 5 the distance travelled in 1 seconds is 2. (Consider the operators +.&t1.

b and operator Step 3: if the operator is ‘+’ then R=a+b Go to step 8 Break Step 4: Else if the operator is ‘-‘ then R=a-b Go to step 8 Step 5: Else if the operator is ‘*‘ then R=a*b Go to step 8 Step 6: Else if the operator is ‘/‘ then R=a/b Go to step 8 Step 7: Else if the operator is ‘%‘ then R=a%b Go to step 8 Step 8: write R Step 9:End Flowchart: .Algorithm: Step 1: Start Step 2: Read the values of a.

.

a-b).b. case '/':printf("quotient of two numbers %2d %2d is: %d".a.a/b).a. case '*':printf("product of two numbers %2d %2d is: %d".b.enter two operands:2 3 enter an operator:+ sum of two numbers 2 3 is: 5 2. switch(op) // used to select particular case from the user { case '+':printf("sum of two numbers %2d %2d is: %d". break. printf("enter an operator:"). scanf("%d%d".a.c). case '-':printf("subtraction of two numbers %2d %2d is: %d". default:printf("please enter correct operator").enter two operands:3 4 enter an operator: - .h> Void main() { char op.b. } getch().a*b). printf("enter two operands:"). float a.&op).a. scanf(" %c".a+b). clrscr().&a. break. break.a. break. case '%':printf("reminder of two numbers %2d %2d is: %d". break.b.b.&b).Program: #include<stdio.c. break. } Output: 1.b.

enter two operands:3 5 enter an operator:* product of two numbers 3 5 is: 15 4. If not goto step 8 If a[i]<min Assign min=a[i] Increment i goto Step 7 Step 8: print min.enter two operands:5 2 enter an operator:/ quotient of two numbers 5 2 is: 2 5.subtraction of two numbers 3 4 is: -1 3. If not goto step 5 Read a[i] Increment i Goto step 4 Step 5: min=a[0]. Description:This program contains n number of elements. max=a[0] Step 6: initialize i=0 Step 7: if i<n do as follows. enter two operands:5 2 enter an operator:% reminder of two numbers 5 2 is: 1 Week 5 AIM: Write a C program to find both the larges and smallest number in a list of integers.max Step 9: stop . in these elements we can find the largest and smallest numbers and display these two numbers Algorithm: Step 1: start Step 2: read n Step 3: initialize i=0 Step 4: if i<n do as follows.

.

min. clrscr().i. } printf("maximum value is:%d\n".&a[i]). getch().i<n. printf("enter the array size:"). for(i=0.&n).max). scanf("%d".enter the array size:4 Enter the elements of array 36 13 maximum value is:45 minimum value is:2 2.enter the array size:5 Enter the elements of array 6 2 1 maximum value is:8 minimum value is:1 3. min=a[0]. max=a[0].enter the array size:5 Enter the elements of array-6 9 -9 maximum value is:9 minimum value is:-9 3 8 2 5 AIM: Write a C program that uses functions to perform the following: i)Addition of Two Matrices 2 45 . if(a[i]>max)//check the condition for maximum value max=a[i].i<n.Program: #include<stdio.n.min).i++)// read the elements of an array { if(a[i]<min)// check the condition for minimum value min=a[i].max. } Output: 1. printf("minimum value is:%d\n". for(i=0.h> void main() { int a[10].i++) // read the elements of an array scanf("%d". printf("Enter the elements of array").

Step 6: print Sum of matrix A and B Step 7: if you select case 2 then goto matrix multiplication Step 8: check if n=p. algorithm: Step 1: start Step 2: read the size of matrices A.B – m. If you select case 1 then goto matric addition.ii) Multiplication of Two Matrices Description: program takes the two matrixes of same size and performs the addition an also takes the two matrixes of different sizes and checks for possibility of multiplication and perform multiplication if possible. Else goto Step 7. if not print matrices can not be multiplied Step 9: Otherwise perform the multiplication of matrices Step 10: Print the resultant matrix Step 11: Stop .n Step 3: read the elements of matrix A Step 4: read the elements of matrix B Step 5: select the choice for you want.

.

A B I=0 j=0 j=0 Read B[i][j] Print A[i][j]+B[i][j] If n==p Matrix cannot be mutiplied Stop i=0 j=0 C[i][j]=0 k0 Print C[i][j] C[i][j]=C[i][j]+A[i][k]*B[k][j] j=0 i=0 .

printf("\n**********************************").b[10][10].i<r1. printf("************************************"). printf("\n\t\tMENU").&b[i][j]).m.n. . for(i=0.j++) scanf("%d".a[10][10]. } printf("Enter elements of matrix B:\n"). } printf("\n =====Matrix Addition=====\n"). } switch(ch) { case 1: printf("Input rows and columns of A & B Matrix:").i<r1.j. printf("\n[0]EXIT").h> void main() { int ch. clrscr(). printf("Enter elements of matrix A:\n").&c1). scanf("%d".&a[i][j]). printf("\n\tEnter your choice:\n").q.&r1. for(i=0. printf("\n**********************************").j<c1. printf("\n[2]MULTIPLICATION OF TWO MATRICES"). printf("\n[1]ADDITION OF TWO MATRICES").p.c[10][10]. if(ch<=2 & ch>0) { printf("Valid Choice\n").c1.&ch). scanf("%d%d".k.i.j++) scanf("%d".i++) { for(j=0.Program: #include<stdio.r1.j<c1.i++) { for(j=0.

k<n.++i) for(j=0.q). write_matrix(c.j++) printf("%5d".q).++k) c[i][j]=c[i][j]+a[i][k]*b[k][j].&p.n). if(n==p) { printf("matrices can be multiplied\n"). } /*end else*/ printf("Resultant of two matrices:\n"). read_matrix(a. scanf("%d%d". /*Function call to read the matrix*/ read_matrix(b. case 2: printf("Input rows and columns of A matrix:"). printf("\n").j<q.m.m.m."). scanf("%d%d". printf("resultant matrix is %d*%d\n". } .&n). printf("Input B matrix\n"). printf("Input A matrix\n"). for(i=0. /*Function for Multiplication of two matrices*/ printf("\n =====Matrix Multiplication=====\n").i<r1. } /*end if*/ else { printf("Matrices cannot be multiplied.&q).i++) { For(j=0.q).&m. printf("Input rows and columns of B matrix:").for(i=0. } break. for(k=0.++j) { c[i][j]=0.i<m.p.a[i][j]+b[i][j]).j<c1.

&a[i][j]). for(i=0. } .int n) { int i.j++) printf("%5d". } /*Function read matrix*/ int read_matrix(int a[10][10].j.i++) for(j=0.j<n. printf("\n"). for(i=0.int m. default: printf("\n Invalid Choice").i++) { for(j=0. } getch().j++) scanf("%d". case 0: printf("\n Choice Terminated"). } /*Function to write the matrix*/ int write_matrix(int a[10][10].int m.i<m.a[i][j]).int n) { int i.j<n. exit().j.i<m. break. } return 0. return 0.break.

Output: 1. ************************************ MENU ********************************** [1]ADDITION OF TWO MATRICES [2]MULTIPLICATION OF TWO MATRICES [0]EXIT ********************************** Enter your choice: 1 Valid Choice Input rows and columns of A & B Matrix:2 2 Enter elements of matrix A: 2 2 2 2 Enter elements of matrix B: 2 2 2 2 =====Matrix Addition===== 44 44 ************************************ MENU ********************************** [1]ADDITION OF TWO MATRICES [2]MULTIPLICATION OF TWO MATRICES [0]EXIT ********************************** Enter your choice: 2 Valid Choice Input rows and columns of A matrix:2 3 Input rows and columns of B matrix:2 .

Description: in this program we need to insert a string into another string from a specified position. ************************************ MENU ********************************** [1]ADDITION OF TWO MATRICES [2]MULTIPLICATION OF TWO MATRICES [0]EXIT ********************************** Enter your choice: 2 Valid Choice Input rows and columns of A matrix:2 2 Input rows and columns of B matrix:2 2 matrices can be multiplied resultant matrix is 2*2 Input A matrix 2 2 2 2 Input B matrix 2 2 2 2 =====Matrix Multiplication===== Resultant of two matrices: 88 88 Week 6 AIM:Write a C program that uses functions to perform the following operations: i) To insert a sub-string in to given main string from a given position. .2 Matrices cannot be multiplied.

Algorithm: Step 1: start Step 2: read main string and sub string Step 3: find the length of main string(r) Step 4: find length of sub string(n) Step 5: copy main string into sub string Step 6: read the position to insert the sub string( p) Step 7: copy sub string into main string from position p-1 Step 8: copy temporary string into main string from position p+n-1 Step 9: print the strings Step 10: stop .

Flow chart: Start Read the strings A & B I=0 I<r C[i] = A[i] I ++ S=n+r O=p+n I =p I++ B X= C[i] If t < n A .

g. int x. int t=0.o. char b[10].A B A[i] = B[t] T=t+1 A[o]=x O=o+1 Print output Stop Program: #include <stdio.r=0. gets(a). puts("Enter First String:"). .n.i=0. char c[10].h> #include <conio. int p=0.h> #include <string. clrscr().s.h> void main() { char a[10].

} s = n+r.&p). // Copying the input string into another array while(i <= r) { c[i]=a[i]. printf("Enter the position where the item has to be inserted: "). o = p+n.puts("Enter Second String:"). } printf("%s". a). i=0. r = strlen(a). scanf("%d".enter first string: computer 2. if(t<n) { a[i] = b[t]. t=t+1. gets(b). } Output: 1. o=o+1. } a[o]=x. i++. // Adding the sub-string for(i=p.enter the position where the item has to be inserted:3 comgecputer . n = strlen(b).enter second string: gec 3. getch().i++) { x = c[i].i<s.

Algorithm: Step 1: start Step 2: read string Step 3: find the length of the string Step 4: read the value of number of characters to be deleted and positioned Step 5: string copy part of string from position to end. pos) Stop .AIM: Write a C program that uses functions to perform the following operations: ii) To delete n Characters from a given position in a given string. n. and (position+number of characters to end) Step 6: stop Flow chart: Start Read string Read position. Description: in this program we need to delete a string from the given string at a specified position. no of characters Delchar( string.

int n.h> void delchar(char *x.h> #include <string.&n).Subprogram Delchar( ) If ((a+b-1_<= strlen(x)) Strcpy(&x[b-1]. int b).p. n.&x[a+b-1]) Puts(x) Return to mainprogram Program: #include <stdio. scanf("%d". delchar(string. . scanf("%d".h> #include <conio.pos. clrscr().pos). void main() { char string[10].int a. puts("Enter the string").&pos). gets(string). printf("Enter the number of characters to be deleted"). printf("Enter the position from where to delete").

int a.&x[a+b-1]).getch().enter the string nagaraju Enter the position from where to delete:4 Enter the number of charcters to be deleted3 nagju 2. puts(x). } // Function to delete n characters void delchar(char *x. } } Output: 1. int b) { if ((a+b-1) <= strlen(x)) { strcpy(&x[b-1]. enter the string kaliraju Enter the position from where to delete:0 Enter the number of charcters to be deleted4 Raju AIM: Write a C program to determine if the given string is a palindrome or not Description:if the reverse of a string is equal to original string then it is called palindrome Algorithm: Step 1:start Step 2: read the string Step 3: store reverse of the given string in a temporary string Step 4: compare the two strings Step 5: if both are equal then print palindrome Step 6: otherwise print not palindrome Step 7: stop .

Flow chart: Start Read string Ispalindrome(string) Print not palindrome If(Ispalindrome (string) true Print palindrome Stop .

Ispalindrome( ) Enum Boolean matched= true If len== 0 Return 0 Left = 0 Right=len-1 While(left<right &&matched If(string[left]!=strin g[right]) true Matched=false Left ++ Right -Return matched Return to main program .

true}. printf("****Program to test if the given string is a palindrome****\n"). right=len-1.second & second last & so on */ while(left<right&&matched) { if(string[left]!=string[right]) matched=false. clrscr().string). } } return matched. getch(). printf("Enter a string:"). enum Boolean IsPalindrome(char string[]) { int left. else printf("The given string %s is not a palindrome\n". . } int main() { char string[40].string).Program: #include<stdio.len=strlen(string). return 0. enum Boolean matched=true. if(IsPalindrome(string)) printf("The given string %s is a palindrome\n". scanf("%s". else { left++.right. /* Compare the first and last letter.h> enum Boolean{false.string). left=0. right--. if(len==0) return 0.h> #include<string.

If not goto step 5 Step 5: print the -1 Step 6: stop . found=strstr(S. Enter the string:india The given string india is not a palindrome Week 7 AIM: Write a C program that displays the position or index in the string S where the string T begins. or – 1 if S doesn’t contain T. Enter the string:malayalam The given string malayalam is a palindrome 2. if found print the second string is found in the first string at the position.} Output: 1.T) ii. Algorithm: Step 1: start Step 2: read the string and then displayed Step 3: read the string to be searched and then displayed Step 4: searching the string T in string S and then perform the following steps i.

Flow chart: Start Initialize s[ ].t) no If found yes Print -1 Print the string Found-s Stop . found variables Read first string Display the string Read string to be searched Display the string Found=strstr(s.t[ ].

\n". clrscr(). gets(t). t[20]. char *found.enter the first string: kali Enter the string to be seareched: li second string is found in the first string at2position 2. else printf("-1").h> #include<string.enter the first string: nagarjuna . /* Entering the main string */ puts("Enter the first string: ").h> #include<conio. getch().enter the first string: nagaraju Enter the string to be seareched: raju second string is found in the first string at4position 3.found-s). gets(s).h> void main() { char s[30]. if(found) printf("Second String is found in the First String at %d position.t). /*Searching string t in string s */ found=strstr(s.Program: #include<stdio. } Output: 1. /* Entering the string whose position or index to be displayed */ puts("Enter the string to be searched: ").

Enter the string to be seareched: ma -1 AIM: Write a C program to count the lines. no of words. words and characters in a given text. no of words and no of characters in a given program or given text by using the string function Algorithm: Step 1: Step 2: Step 3: Step 4: Step 5: Step 6: Step 7: Step 8: Start Read the text until an empty line Compare each character with newline char ‘\n’ to count no of lines Compare each character with tab char ‘\t\’ or space char ‘ ‘ to count no of words Compare first character with NULL char ‘\0’ to find the end of text No of characters = length of each line of text Print no of lines. Description:In this program we have to count the no of lines. no of chars Stop .

lines=0 While End==0 true C=0 false If (ctr=getchar())!=’\n’ true Line[c++]=ctr Line[c]=’\0’ true If line[0]=’\0’ false I=0 Words ++ Words.Flow chart: Start Initialize end=0.words=0.chars stop Lines++ If line[i]==’ ‘|| Chars+=strlen(line) Words ++ Print lines.chars=0. .

words = 0. int i. lines). end = 0. printf("Number of lines = %d\n". while((ctr=getchar()) != '\n') line[c++] = ctr.\n\n"). } /* counting lines and characters */ lines = lines +1.c.h> main() { char line[81]. ctr.\n"). PRESS 'RETURN'. } . lines = 0. line[c] = '\0'. printf("WHEN COMPLETED. characters = characters + strlen(line). printf("KEY IN THE TEXT. characters). printf("Number of words = %d\n". words). printf("GIVE ONE SPACE AFTER EACH WORD. while( end == 0) { /* Reading a line of text */ c = 0. characters = 0.i++) if(line[i] == ' ' || line[i] == '\t') words++. } printf ("\n"). printf("Number of characters = %d\n". for(i=0.Program: #include <stdio. /* counting the words in a line */ if(line[0] == '\0') break .\n"). line[i] != '\0'. else { words++.

print white space ii. iv)Goto Step 9 Step 9: increment j. goto Step 8 Step 10: print new line control Step 11: increment m. decrement i iii. WHEN COMPLETED. Always we like those who admire us but we do not like those whom we admire. Admiration is a very short-lived passion.KEY IN THE TEXT. GIVE ONE SPACE AFTER EACH WORD. goto step 4 Step 12: Stop . Description: Pascal’s triangle which is used for a coefficient in the equation in polynominals. Admiration involves a glorious obliquity of vision. Number of lines = 5 Number of words = 36 Number of characters = 205 Week 8 AIM :Write a C program to generate Pascal’s triangle. If not goto Step 10 i)if(j==0||m==0) ii)Initialize b=1 if not b=b*(m-j+1)/j iii)Print white space. goto Step 6 Step 7: Initialize j=0 Step 8: If j=m do as follows. PRESS 'RETURN'. Alogrithm: Step 1: Start Step 2: Initialize m=0 Step 3: Read n Step 4: If m<n goto step 5. but men of sense approve. b . Fools admire. If not goto step 7 i.Output 1.if not goto step 12 Step 5: initialize i=40-m Step 6: If i>0 is true do as follows.

binom=1.h> #include<conio.r.x.p.Flow chart: Start Read p true I=0 I<p R=40-I r-- false Print newline F X=0 Stop x<=i false true Print white space true true false If x==0 || I==0 B=1 B=b*(i-x+1)/x Ptint b Program: #include<stdio. .h> void main() { int i.

r--) // perform the looping operation until 0 printf(" "). scanf("%d".enter the how many lines to print5 1 11 121 1331 14641 . else binom=binom*(i-x+1)/x. } getch(). printf("enter the how many lines to print"). i++. printf(" ").x<=i. for(x=0. 2.x++) { if((x==0)||(i==0)) // check the condition binom=1. } printf("\n").&p). Description: In this program the we have to construct output in the pyramid shape manner Output: 1.} clrscr(). printf("%d". while(i<p) // check the condition { for(r=40-i.r>0.binom). i=0.enter the how many lines to print3 1 11 121 AIM: Write a C program to construct a pyramid of numbers.

I. Step9: Stop Flow chart: . x=35 Step3: read the num Step4:perform the loop operation For(y=0.y.y++) Step5:Gotoxy(x.Algorithm: Step 1: Start Step2: initialize the num.y+1) Step6: perform the loop operation for displaying digits towards the left and right For(i=0-y.i<=y.y<=num. Step8: x=x-2.i++) Step7: print abs(i).

printf("\nEnter the number to generate the pyramid:\n").x=35.y++) .x=35 Read num Y=0 y<=num true Gotoxy(x.Start Initialize num.I.&num).h> #include<conio. for(y=0.i. scanf("%d".y.y.y+1) I=0 i++ X=x-2 Print abs(i) Stop Program: #include<stdio. clrscr().h> void main() { int num.y<=num.

+xn Description: In this program we have to read the two numbers and the calculate the sum of this geometric progression in above mention . /*for displaying digits towards the left and right of zero*/ for(i=0-y.y-coordinate)*/ gotoxy(x. Algorithm: Step 1: Start Step 2: read values of x and n.i++) printf("%3d". i=1 Step 3: check for n & X i)if n<=0 || x<=0 ii)print values are not valid Week 9 .y+1).enter the number: 4 0 101 21012 3210123 432101234 0 101 21012 3210123 } 2. x=x-3.{ /*(x-coordinate.i<=y. getch(). sum-1.abs(i)). } Output: 1.enter the number: 3 AIM : To read in two numbers x and n and then compute the sum of this geometric progression 1+x+x2+x3+……….

x.n Read x.i) Print sum Stop .iii)read values of x and n Step 4: perform the loop operation i)for(i=1.i++) then follows ii)sum=sum+pow(x.i) Step 5: print sum Step 6: Stop Flow chart: Start Initialize Sum.i<=n.n false If n<=0||x<=0 true Sum = 1 false I=1 i<=n Print not valid true Sum=sum+pow(x.I.

} Output: 1.Enter the values for x and n:0 1 Value is not valid . if(n<=0 || x<=0) { printf("Value is not valid\n").s_sum).i). } printf("Sum of series=%d\n". printf("Enter the values for x and n:").&x.i.n.Program: #include<stdio. } getch(). s_sum=1.h> void main() { int s_sum. for(i=1.x.i++) { s_sum=s_sum+pow(x.Enter the values for x and n:2 3 Value is valid Sum of series=15 2.i<=n.h> #include<conio.h> #include<math. clrscr().Enter the values for x and n:4 9 Value is valid Sum of series=21845 3. scanf("%d %d".&n). } else { printf("Value is valid\n").

a[i]!=’\0’. And finally add the 1 to the converted number.i++) ii)if(a[i]!=’0’&&a[i]!=’1’) then displayed the number is not valid.Week 10 Aim: 2’s complement of a number is obtained by scanning it from right to left and complementing all the bits after the first appearance of a 1.c=1 if not goto step 5 Step 5: if(c==1&&b[i]==’0’) is true then i)b[i]=’1’. Write a C program to find the 2’s complement of a binary number. iii)Exit the loop Step 6: call sub program ‘complemt(a)’ Step 7: stop Sub program: Step 1: initialize the variable I. if it is true then follows.i--) ii)if(a[i]==’0’) then b[i]=’1’ else iii)b[i]=’0’ Step 4: for(i=l-1.i>=0. if not goto i)for(i=l-1.c=1 .i>=0. if not goto step 7 i)for(i=0. c=0 if not goto Step 6 Step 6: if(c==1&&b[i]==’1’) then b[i]=’0’. Then we will get the 2’s complement number.i--) is true i)if(i==l-1) then ii)if(b[i]==’0’) then b[i]=’1’ else iii)b[i]=’0’.c=0. Algorithm: main program Step 1: Start Step 2: declare the subprogram “complement(char *a)” Step 3: initialize the variable i Step 4: read the binary number Step 5: perform the loop operation. Description: In this program the given binary number is first covert the numbers 0 to1 and 1 to 0. if it is true then follows.b[160 Step 2: 1=strlen(a) Step 3: perform the loop operation. Thus 2’s complement of 11100 is 00100. enter the correct number.

a[ ] Read a false I=0 i++ true false If a[i]!=’0’&&a[i]!=’1’ Call sub program Complement(a) true Number is not valid Stop .Flowchart: Step 7: displayed b[l]=’\0’ Step 8: print b and return to main program Start Initialize I.

Complement( ) Initialize .I.b[ ] 1=strlen(a) I=i-1 false I-true If a[i]==’0’ B[i]=]0] true B[i]=’1’ I=l-1 i-C false If i==l-1 false If c==1&& true true true b[i]=’0’ If b[i]==’0’ B[i]=’1’ A B[i]=’1’ C=0 true C=1 B .c=0.

} . exit(0). printf("Enter the binary number").h> void complement (char *a). for(i=0. clrscr().h> #include<conio. int i.C A true B[i]=’0] C=1 B If c==1 && b[i]==1 B[i]=’\0’ Print ‘b’ Return to main program Program: #include <stdio. i++) { if (a[i]!='0' && a[i]!='1') { printf("The number entered is not a binary number. gets(a).a[i]!='\0'. Enter the correct number"). void main() { char a[16].

i. i--) { if(i==l-1) { if (b[i]=='0') b[i]='1'. for (i=l-1. c=1. } for(i=l-1. else b[i]='0'. getch(). c=0. char b[16]. i--) { if (a[i]=='0') b[i]='1'. c=1. l=strlen(a). i>=0. i>=0. else { b[i]='0'.} complement(a). c=0. } void complement (char *a) { int l. . } }} b[l]='\0'. } else if (c==1 && b[i]=='1') { b[i]='0'. } } else { if(c==1 && b[i]=='0') { b[i]='1'.

Description: In this program we have to take the roman value.printf("The 2's complement is %s". Ex: X=10 Algoritham: Step 1: Start Step 2: read the roman numerical as string Step 3: find length of roman numerical Step 4: for each charcter in the string i)if(char=I) then decimal=1 ii)if(char=V) then decimal=5 iii)if(char=X) then decimal=10 iv)if(char=L) then decimal=50 v)if(char=C) then decimal=100 vi)if(char=D) then decimal=500 vii)if(char=M) then decimal=1000 viii) otherwise invalid character Step 5: repeat step 4 until the length of the string Step 6: k=char[length-1] Step 7: for each character of decimal string i)if(decimal[i]>dec[i-1]) then k=k-decimal[i-1] ii)else if(decimal[i]=decimal[i-1 or decimal[i]<decimal[i-1) then k=k+decimall[i1] Step 8: repate step 7 until the length of decimal string Step 9: print decimal value Step 10: Stop . b). } Output: 1.Enter the binary number101010 The 2's complement is 010110 Enter the binary number11111 The 2's complement is 00001 Enter the binary number2222 The number entered is not a binary number. Enter the correct number Aim: Write a C program to convert a Roman numeral to its decimal equivalent. This value is converted into a it’s equivalent decimal number.

Flow chart: Start Read roman value Len=strlen(rom) I=0 i++ K=a[len-1] If rom[i]==’I’ false true A[i]>a [i-1] True A[i]=1 I=len-1 i— true If rom[i]==’V’ A[i]=5 F k=k-a[i-1] true false true If rom[i]==’X’ A[i]=10 A[i]==a[ i1]||a[i] <a[i-1] false B true C k=k+ai-1] A .

h> #include<string.i.h> #include<stdlib.k.h> #include<conio.len.B A true If rom[i]==’L’ A[i]=50 C Print k false true If rom[i]==’C’ A[i]=100 Stop false true If rom[i]==’D’ A[i]=500 false true If rom[i]==’C’ A[i]=1000 Program: #include<stdio.j.h> void main() { int *a. Print invalid .

else if(rom[i]=='L') a[i]=50. } printf("\nIts Decimal Equivalent is:"). for(i=0.i++) // loop will continue until I is not graterthan length. . for(i=len-1. else if(a[i]==a[i-1] || a[i]<a[i-1]) k=k+a[i-1]. else if(rom[i]=='X') a[i]=10.i<len. else if(rom[i]=='V') a[i]=5.char *rom. else if(rom[i]=='C') a[i]=100. getch(). scanf("%s".rom). len=strlen(rom).i>0. else { printf("\nInvalid Value").i--) // loop will continue until I lessthan zero { if(a[i]>a[i-1]) // check the condition k=k-a[i-1]. } } k=a[len-1]. printf("Enter the Roman Numeral:"). clrscr(). exit(0). else if(rom[i]=='D') a[i]=500. { if(rom[i]=='I') a[i]=1. else if(rom[i]=='M') a[i]=1000.

goto step 4 Step 6: if choice=2 then multiplication operation will perform and it contains following steps .imgpart.imgpart+w2. getch().k). ii) w. first one is real part and second one is imaginary part(2+3i). Algorithm: Step 1: Start Step 2: declare structure for complex numbers Step 3: read the complex number Step 4: read choice Step 5: if choice=1 then addition operation will perform and it contains following steps i) w.realpart = w1.) Description: In this program the complex number means it contains the two parts .realpart+w2.imgpart = w1.printf("%d".by taking these two complex numbers we can perform the addition and multiplication operation. } Output: Enter the Roman Numeral:D Its Decimal Equivalent is:500 Enter the Roman Numeral:X Its Decimal Equivalent is:10 Enter the Roman Numeral:23 Invalid Value Week 11 Aim :Write a C program that uses functions to perform the following operations: i) Reading a complex number ii) Writing a complex number iii) Addition of two complex numbers iv) Multiplication of two complex numbers (Note: represent complex number using a structure.realpart.

imgpart). Step 9: Stop .realpart)-(w1.imgpart=(w1. goto step 4 Step 7: if choice=0 then exit operation will perform Step 8:if w.realpart).realpart*w2.realpart*w2.imgpart*w2.i) w.imgpart>0 then print realpart+imgpart else Print realpart. ii) w.imgpart*w2.imgpart)+(w1.realpart=(w1.

realpart w.imgpart+w2.imgpart=(w1.realpart+w.realpart=(w1.imgpart If option=mul false If w.realpart) w.imgpart=w1.realpart+w2.imgpart>0 Print real part w.imgp art i Stop .Flow chart: Start Declare structure Read option false If option=(add or mul) true Read real part of first number image part of first number Read real part of second number false If option=add w.realpart=w1.realpart*w2.imgpart*w2.imgpart) true Print w.

h> void arithmetic(int opern).h> #include<math. void main() { int opern. default: main(). if(opern>2) { printf("invalid option"). } else { switch(opern) { case 0: exit(0). struct comp { double realpart. w. double imgpart. } void arithmetic(int opern) { struct comp w1. clrscr().Program: #include<stdio. }.&opern). printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b"). printf("\n Enter two Complex Numbers (x+iy):\n Real Part of First Number:"). } } getch(). case 1: case 2: arithmetic(opern). w2. printf("\n\n \t\t\t***** MAIN MENU *****"). scanf("%d". .

w.realpart*w2.realpart = w1. printf("\n Imaginary Part of First Number:").w. scanf("%lf". switch(opern) { /*addition of complex number*/ case 1: w.realpart+w2. /*multiplication of complex number*/ case 2: w.imgpart = w1.imgpart).imgpart*w2. else printf("\n Answer = %lf%lfi".&w2.imgpart). w.realpart). printf("\n Real Part of Second Number:").w.imgpart*w2.imgpart.realpart).scanf("%lf". } Output: ***** MAIN MENU ***** Select your option: 1 : ADD 2 : MULTIPLY 0 : EXIT Enter your Option [ 1] Enter two Complex Numbers (x+iy): Real Part of First Number:2 . break. getch().&w1.imgpart+w2.w.realpart)-(w1.realpart.imgpart>0) printf("\n Answer = %lf+%lfi".imgpart).imgpart)+(w1.&w2. printf("\n Imaginary Part of Second Number:"). scanf("%lf".w.imgpart). } if (w. main(). break.realpart*w2.imgpart).realpart.realpart=(w1.imgpart=(w1.&w1.realpart. scanf("%lf".realpart).

000000i ***** MAIN MENU ***** Select your option: 1 : ADD 2 : MULTIPLY 0 : EXIT Enter your Option [ 3] invalid option ***** MAIN MENU ***** Select your option: .Imaginary Part of First Number:2 Real Part of Second Number:2 Imaginary Part of Second Number:2 Answer = 4.000000+8.000000i ***** MAIN MENU ***** Select your option: 1 : ADD 2 : MULTIPLY 0 : EXIT Enter your Option [ 2] Enter two Complex Numbers (x+iy): Real Part of First Number:2 Imaginary Part of First Number:2 Real Part of Second Number:2 Imaginary Part of Second Number:2 Answer = 0.000000+4.

then print source file can not be open Step 6: open destination file in write mode Step 7: if NULL pointer. Algorithm: Step 1: Start Step 2: read command line arguments Step 3: check if no of arguments =3 or not. then print destination file can not be open Step 8 : read a character from source file and write to destination file until EOF Step 9: Close source file and destination file Step 10: Stop . If not print invalid no of arguments Step 4: open source file in read mode Step 5: if NULL pointer.1 : ADD 2 : MULTIPLY 0 : EXIT Enter your Option [ 0] Week 12 Aim . Write a C program which copies one file to another. Discription :In this program we have to use the file functions to perform the copy operation from one file to another file.

ft) Program: #include <stdio.”r” Print target file can not be opened If ft==NULL Fclose(fs) Ch=fgetc(fs) false Fclose(fs) Stop Fclose(ft) If ch==EOF false Fputc(ch.h> #include <conio.h> #include <process.”w” false Fs=fopen(arg[1].h> true .Flow chart: Start Read arguments from command line true If arg!=3 Print invalid no of arguments true Print source file can not be opened If fs=NULL false Ft=fopen(arg[2].

getch().void main(int argc. } ft = fopen(argv[2]."w").c } while(1) { ch=fgetc(fs).} puts("Target file cannot be opened. clrscr(). if (ch==EOF) // check the condition if the file is end or not break.").*ft. } fs = fopen(argv[1]. fclose(ft). char *argv[]) { FILE *fs.c this is source text ouput.").c Command line arguments source.c source.ft). fclose(fs)."). exit(0). else fputc(ch.c ouput. } fclose(fs). if(fs==NULL) { puts("Source file cannot be opened. if (ft==NULL) // check the condition if the file pointer is NULL or not { Output: source. if(argc!=3) { puts("Invalid number of arguments. exit(0). exit(0). char ch. ."r").

then print file cannot be open Step 6: Store no of chars to reverse in k K= *argv[2]-48 Step 7: read the item from file stream using fread Step 8: Store chars from last position to initial position in another string(temp) Step 9: print the temp string Step 10: Stop .c Invalid number of arguments.this is source text ouput.) Description: This program perform the reverse operation of n characters in the file Algorithm: Step 1: Star Step 2: read the command line arguments Step 3: check if arguments=3 or not If not print invalid no of arguments Step 4: open source file in read mode Step 5: if NULL pointer. (Note: The file name and n are specified on the command line.c this is source text Command line arguments source. Aim : Write a C program to reverse the first n characters in a file.

Flow chart: Start Read command line args false If argc!=3 true Print file can not be opened If fp==NULL Fp=fopen(argv[1].k.”r” Print invalid no of args false k=*argv[2]-48 n = fread(a.1.fp) a[n]='\0' I = len-1 i— false S[j+1]=’\0’ S[j]=a[i] Print s[j] Stop J++ true .

s[j]).i--) { s[j]=a[i]. . if(argc!=3) { puts("Improper number of arguments.h> #include <string. getch()."). FILE *fp. char n.h> void main(int argc."). int j=0. n = fread(a.Program: #include <stdio. } } s[j+1]='\0'.1.i>=0. } fp = fopen(argv[1].h> #include <conio. int k.} k=*argv[2]-48.k. j=j+1. printf("%c". char *argv[]) { char a[15].fp). len=strlen(a). int len.h> #include <process. exit(0). exit(0). int i. for(i=len-1. a[n]='\0'. char s[20]."r"). if(fp == NULL) { puts("File cannot be opened.

Output: source.c this is source ouput.c Command line arguments source.c ouput.c source.c this is source ecruos si siht Command line arguments source.c Invalid number of arguments.

Week 13 AIM: Write a C program that uses functions to perform the following operations on singly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal Description: In this program we have to create a single linked list, insert the elements into that list ,delete the some elements from that list and then perform the sorting operation and traversal operation on that created linkedlist Algorithm : Step 1: Start Step 2: Declare a structure named linked-list Step 3: Declare the pointers next, first, fresh, ptr Step 4: Print main menu Step 5: Read choice Step 6: Switch(choice) Step 7: If(choice==1) 7.1 Assign fresh=malloc(size of (node)) 7.2 Read the element fresh->data 7.3 Read the choice where to insert 7.4:Switch(choice) 7.4.1: If choice==1 7..4.2: Call the function IBegin() 7.4.3: If choice==2 7.4.4: Call the function Iend() 7.4.5: If choice==3

7.4.6: Call the function Imiddle() Step 8: If(choice==2) 8.1: Read the position to delete 8.2: Switch(choice) 8.2.1: If choice==1 8..2.2: Call the function DBegin() 8.2.3: If choice==2 8.2.4: Call the function Dend() 8.2.5: If choice==3 8.2.6: Call the function Dmiddle() Step 9: If choice==3 9.1 Call function view Step 10: If choice==4 10.1 Exit() Step 11: Start insert function Step 12: If(first==null) Step 13: First->data=e Step 14: First->next=null Step 15: Else declare new node Step 16:fresh->data=e Step 17: If choice=1 Step 18: frsh->next=first Step 19: first=fresh Step 20:if choice=2 Step 21: ptr=first Step 22: ptr->next=fresh Step 23: fresh->next=full Step 24: If choice =3 Step 25: Enter the position Step 26:at p-1 node Step 27: fresh->next= ptr->next Step 28: ptr->next=fresh Step 29: for delete function Step 30: If first!=null Step 31: Enter the position to delete Step 32: If choice=1 Step 33: d=first->data Step 34: first=first->next Step 35: if choice=2 Step 36: ptr=first Step 37: Traverse to last node

Step 38: d=ptr->next->data Step 39: ptr ->next=ptr->next->next Step 40: Print d value Step 41: for function view Step 42: for ptr=first and ptr!=null and ptr=ptr->next Step 43: Print ptr->data Step 44: End Flowchart: Program: # include<stdio.h> # include<malloc.h> int ch,i,n,j,p,item; /* VARIABLE DECLARATION */ /* START OF STRUCTURE DEFINITION */ struct link {

int data; struct link *next; }*start,*new,*l,*l1,*start1,*t; /* END OF STRUCTURE DEFINITION */ /* START OF MAIN FUNCTION */ main() { clrscr(); start=NULL; start1=NULL; printf(" **** MENU **** "); printf("\n 1.Insertion\n 2.Deletion\n 3.Traverse\n 4.Search\n 5.Sort\n 6.Merge\n 7.Reverse\n"); while(1) { printf("enter the choice:"); scanf("%d",&ch); switch(ch) { case 1: insert(); break; case 2: delete(); break; case 3: traverse(); break; case 4: search(); break; case 5: sort(); break; case 6: merge(); break; case 7: reverse(); break; case 8:exit(); } } getch(); }

} if(ch==2) { printf("enter the position to place item:").&p). new->next=l->next. scanf("%d". for(i=1.start\n2. start=new. } else { printf("1. new->data=item. l->next=new. new->next=NULL.&ch). . if(start==NULL) { new->next=NULL. printf("enter the place to place the item:"). if(ch==1) { new->next=start. scanf("%d".middle\n3. scanf("%d". } if(ch==3) { while(l->next!=NULL) l=l->next.i++) l=l->next. start=new.i<p-1.end\n").&item). printf("enter the item to be inserted:")./* END OF MAIN FUNCTION */ /* START OF INSERT FUNCTION */ insert() { l=start. new=malloc(sizeof(struct link)).

l->data). printf("enter the place to delete the item:").l->next=new.l->next!=NULL. else { printf("1.middle\n3.end\n"). else printf("\n%7d->".l=l->next) if(l==start) printf("\nstart:%d->". if(ch==1) { item=start->data. } } /* END OF DISPLAY FUNCTION */ /* START OF DELETE FUNCTION */ delete() { l=start. if(start==NULL) printf("NO ITEMS IN THE LIST\n"). if(l->next==NULL) printf("\n last:%d->\n". } } } /* END OF INSERT FUNCTION */ /* START OF DISPLAY FUNCTION */ traverse() { if(start==NULL) printf("LIST IS EMPTY\n"). scanf("%d".l->data).&ch). else { for(l=start.start\n2. .l->data).

l->next=l->next->next. printf("deleted item is:%d\n".i<p-1. scanf("%d".&p). start=start->next.item). printf("deleted item is:%d\n". } if(ch==3) { if(l->next==NULL) { item=l->data. printf("deleted item is:%d\n".i++) l=l->next. l=l->next. if(l->next==NULL) { item=l->data. l=start=NULL. l->next=NULL.item).item). } } } } .} if(ch==2) { printf("deleted item is:%d\n". printf("enter the position to delete item:"). item=l->next->data.item). } else { for(i=1.item). item=l->next->data. printf("deleted item is:%d\n". l=start=NULL. } else { while(l->next->next!=NULL) l=l->next.

} if(f==1) printf("item %d found at position :%d\n". break. scanf("%d".item. } } /* END OF SEARCH FUNCTION */ /* START OF SORT FUNCTION */ sort() { int t. if(start==NULL) printf("LIST IS EMPTY").l1->next!=NULL.&item). else printf("item %d not found\n".l!=NULL.item). else { for(l1=start.l1=l1->next) { . if(start==NULL) printf("LIST IS EMPTY").i). else { for(l=start.l=l->next.i=1.i++) if(l->data==item) { f=1. printf("enter the search item:").} /* END OF DELETE FUNCTION */ /* START OF SEARCH FUNCTION */ search() { int f=0.

l->data=l->next->data. if(start1==NULL) start1=new. printf("enter the item to be inserted:"). } printf("\n"). . for(j=1. new=malloc(sizeof(struct link)).l=l->next) if(l->data > l->next->data) { t=l->data. scanf("%d". scanf("%d".l->data). } } printf("THE SORTED ORDER IS:").start\n2. l->next->data=t.l!=NULL. } /* END OF SORT FUNCTION */ /* START OF MERGE FUNCTION */ merge() { printf("enter no of elements to be inserted in second list :"). for(l=start. else { printf("1. scanf("%d".end\n"). new->next=NULL.l->next!=NULL.&item).l=l->next) printf("%3d".&ch).j<=n.&n). new->data=item.j++) { l1=start1. if(ch==1) { new->next=start1.for(l=start. printf("enter the place to place the item:").middle\n3.

} } printf(" *** LIST IS MERGED *** \n").&p). scanf("%d".i++) l1=l1->next.l1=l1->next) { l->next=l1.i<p-1. l1->next=new. for(i=1. } { printf("enter the position to place item:"). } /* END OF MERGE FUNCTION */ /* START OF REVERSE FUNCTION */ reverse() { if(ch==2) . l=l->next.start1=new. } } } if(start==NULL) start=start1. else { l=start. while(l->next!=NULL) l=l->next. l1->next=new.l1->next!=NULL. } { if(ch==3) while(l1->next!=NULL) l1=l1->next. new->next=l1->next. for(l1=start1.

l1=t=NULL.middle 3. } start=t. l=l->next.Merge 7. } } /* END OF REVERSE FUNCTION */ ***** OUTPUT ***** **** MENU **** 1.Search 5.Insertion 2.if(start==NULL) printf("LIST IS EMPTY\n").start 2. while(l!=NULL) { l1=t. t->next=l1.end enter the place to place the item:1 enter the choice:1 enter the item to be inserted:3 . else { l=start. printf(" *** LIST IS REVERSED ***\n").Traverse 4. t=l.Reverse enter the choice:1 enter the item to be inserted:1 enter the choice:1 enter the item to be inserted:2 1.Deletion 3.Sort 6.

middle 3.1.end enter the place to place the item:1 enter the item to be inserted:7 1.end enter the place to place the item:2 enter the position to place item:3 enter the choice:3 start:2-> 1-> 4-> last:3-> enter the choice:4 enter the search item:4 item 4 found at position :3 enter the choice:6 enter no of elements to be inserted in second list :3 enter the item to be inserted:5 enter the item to be inserted:6 1.start 2.middle 3.middle 3.end enter the place to place the item:2 enter the position to place item:2 *** LIST IS MERGED *** enter the choice:3 start:2-> 1-> 4-> 3-> .start 2.start 2.start 2.end enter the place to place the item:3 enter the choice:1 enter the item to be inserted:4 1.middle 3.

middle 3.start 2.start 2.end enter the place to delete the item:2 enter the position to delete item:4 deleted item is:5 enter the choice:3 start:2-> .start 2.6-> 7-> last:5-> enter the choice:7 *** LIST IS REVERSED *** enter the choice:3 start:5-> 7-> 6-> 3-> 4-> 1-> last:2-> enter the choice:4 enter the search item:1 item 1 found at position :6 enter the choice:5 THE SORTED ORDER IS: 1 2 3 4 5 6 7 enter the choice:2 1.end enter the place to delete the item:1 deleted item is:1 enter the choice:2 1.middle 3.middle 3.end enter the place to delete the item:3 deleted item is:7 enter the choice:2 1.

middle 3.middle 3.middle 3.start 2.3-> 4-> last:6-> enter the choice:2 1.start 2.end enter the place to delete the item:2 enter the position to delete item:2 deleted item is:6 enter the choice:2 1.end enter the place to delete the item:1 deleted item is:2 enter the choice:2 1.start 2.start 2.end enter the place to delete the item:2 enter the position to delete item:2 deleted item is:4 enter the choice:3 start:3-> last:6-> enter the choice:2 1.middle 3.end enter the place to delete the item:1 deleted item is:3 enter the choice:3 LIST IS EMPTY enter the choice:2 NO ITEMS IN THE LIST enter the choice:8 .

1: Create a new mode Step 16. *pre Step 3: Declare *start.3: new -> next=start Step 16. insert the elements in to a doubly linked list.4: start->pre=new Step 16.2: new -> data=e Step 16.1: call insert function if choice==1 6.3: call view function if choice==3 Step 7: Stop Step 8: Start of insert function Step 9: Read e Step 10: If start==null Step 11: Create a new node Step 13: Start->next=null Step 14: Start->pre=null Step 15: read choice. where to insert Step 16: if choice==1 Step 16. delete the elements from that list and finally perform the traversal operation ALGORITHM : Step 1: Start Step 2: Declare a structure with *next.: i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways Description : In this program we have to create a doubly linked list.2: call delete function if choice==2 6.*l as structure pointers Step 4: Print main menu Step 5: Read choice Step 6: Switch choice 6.6: Start->new Step 12: Start->data=e .Week 14 AIM: Write a C program that uses functions to perform the following operations on doubly linked list.5: new->pre=null Step 16. *new .

4: l=l->next 24.3: start->pre=null Step24: if choice==2 24.7: new -> pre=l 17.9: l-> next=new 17.5: increment I by 1 24.2: start=start->next 23.1: l=start 18.2: l=start 17.7: l->next=new 18.4: create a new mode 18.Step 17: otherwise if choice==2 17.4: incrent i 17.5: l=l->next 17.5: new->data=e 18.2: l=start 24.6: temp=l-next->data Step23: if choice==1 .10: l->next->pre=new Step 18: if choice==3 18.3: l=l->next 18.3: while i<(p-1) 17.1: temp=start->data 23.8: new->next=new 17.1: read position 24.1: read position p 17.8: new->pre=l Step19: end of insert function Step20: start of deletion function Step21: write menu Step22: read choice 23.6: new -> data =e 17.2: while l->next!=null 18.6: new->next=null 18.3: while (i=1 <p-1) 24.

4: temp=l->next->data 25.7: l->next=l->next->next 24.8: l->next->pre=l Step25: if choice==3 25.4: l=l->pre 30.5: write l->data Step31: end of function view Flowchart: .24.3: write l-> data.2: while l->next->next!= null 25.3: l=l->next 25.2: while l!=start 30.1: read l=start 25.2: while (l->next!= null) 29.4: write l->data Step30: if choice==2 30.3: write l->data 30.1: l=next 29.5: l->next=null Step26: end of delete function Step27: start of view function Step28: read choice Step29: if choice==1 29.1: l=start 30. l=l->next 29.

case 2:delete(). clrscr(). struct link *prev. scanf("%d".*temp. switch(ch) { case 1:insert(). struct link *next.Program: #include<stdio.j.h> #include<malloc. /* VARIABLE DECLARATION */ /* START OF MAIN FUNCTION */ main() { start=NULL. }*start. start1=NULL.merge\n7. printf(" **** MENU ****").*l.sort\n6.i. while(1) { printf("enter your choice:").*start1.*l1.p.Insertion\n2. printf("\n1. break.h> /* START OF STRUCTURE DEFINITION */ struct link { int data. case 3:display().*new.r everse\n8. case 4:search().n. break. break.Traverse\n4.search\n5.ch.Deletion\n3. .exit\n").*t. /* END OF STRUCTURE DEFINITION */ int item.&ch).

case 6:merge(). case 7:reverse(). } else { printf("1. scanf("%d". new->data=item. printf("enter an item to be inserted:"). new=malloc(sizeof(struct link)). break.start\n2. . break.&ch). start=new. start=new. if(start==NULL) { new->prev=NULL. printf("enter the place to insert item:"). case 5:sort(). new->next=NULL.middle\n3.&item).end\n"). case 8:exit(). new->prev=NULL. } break. break. } /* END OF MAIN FUNCTION */ /* START OF INSERT FUNCTION */ insert() { l=start.} getch(). scanf("%d". if(ch==1) { new->next=start.

&ch). l->next=new. new->prev=l. else { printf("1. start=start->next.i<p-1. } } } /* END OF INSERT FUNCTION */ /* START OF DELETE FUNCTION */ delete() { l=start.middle\n3. scanf("%d".} if(ch==2) { printf("enter the position to place item:"). } if(ch==3) { while(l->next!=NULL) l=l->next. for(i=1. new->next=l->next. printf("deleted item is :%d". l->next=new.start\n2. .&p). new->prev=l.end"). new->next=NULL.item). if(start==NULL) printf("*** LIST IS EMPTY ***"). scanf("%d". printf("enter the place to delete the item:").i++) l=l->next. if(ch==1) { item=start->data.

item). l->next->prev=l. l->next=l->next->next. l=start=NULL. scanf("%d". printf("deleted item is:%d". printf("deleted item is :%d".start->prev=NULL.&p). printf("deleted item is:%d".item). } else { for(i=1. } else { while(l->next->next!=NULL) l=l->next.i<p-1. printf("deleted item is:%d".item). l=start=NULL.item). item=l->next->data. } if(ch==2) { printf("enter the position to delete an item:"). l->prev=NULL. } } if(ch==3) { if(l->next==NULL) { item=l->data. } } } . item=l->next->data.i++) l=l->next. l->next=NULL. if(l->next==NULL) { item=l->data.

if(start==NULL) printf(" *** LIST IS EMPTY *** "). } } /* END OF DISPLAY FUNCTION */ /* START OF SEARCH FUNCTION */ search() { int f=0.i++) if(item==l->data) { f=1.l!=NULL.} /* END OF DELETE FUNCTION */ /* START OF DISPLAY FUNCTION */ display() { if(start==NULL) printf("*** LIST IS EMPTY ***\n").l=l->next) if(l==start) printf("\nstart:%d". for(l=start.i=1.l->data). else printf("\n %8d". if(l->next==NULL) printf("\n last:%d". else { for(l=start.l->next!=NULL.l=l->next. else { printf("enter the search item:"). break.l->data). scanf("%d".&item). } if(f==1) .l->data).

} printf("\n"). for(l=start.item). else { for(l1=start.printf("item %d found at position %d". } } /* END OF SEARCH FUNCTION */ /* START OF SORT FUNCTION */ sort() { int t.l->next!=NULL. for(j=1.i). l->next->data=l->data.l!=NULL. scanf("%d".l1->next!=NULL. else printf("item %d not found in list".l->data).j<=n. } /* END OF SORT FUNCTION */ /* START OF MERGE FUNCTION */ merge() { printf("enter number items to be inserted in second list:").item. l->data=t. if(start==NULL) printf(" *** LIST IS EMPTY *** "). } printf("THE SORTED ORDER IS:").l1=l1->next) for(l=start.l=l->next) printf("%3d".l=l->next) if(l->data > l->next->data) { t=l->next->data.j++) .&n).

} else { printf("1.i<p-1.i++) l1=l1->next.{ l1=start1.middle\n3. start1=new.&item). new->next=NULL. start1=new. new->next=NULL. if(ch==1) { new->next=start1. } if(ch==2) { printf("enter the position to place item:"). new->data=item. new->prev=l1. printf("enter the place to insert item:"). } if(ch==3) { while(l1->next!=NULL) l1=l1->next. new->next=l1->next. new->prev=l1.&ch). } .start\n2. if(start1==NULL) { new->prev=NULL. for(i=1. printf("enter an item:").end\n"). scanf("%d". l1->next=new. scanf("%d". new->prev=NULL.&p). scanf("%d". l1->next=new. new=malloc(sizeof(struct link)).

l1->next!=NULL. } /* END OF MERGE FUNCTION */ /* START OF REVERSE FUNCTION */ reverse() { if(start==NULL) printf(" *** LIST IS EMPTY ***\n ").l1=l1->next) { l->next=l1. printf(" *** LIST IS REVERSED *** \n"). t->next=l1.} } if(start==NULL) start=start1. for(l1=start1. while(l->next!=NULL) l=l->next. else { l=start. } } printf(" *** LIST IS MERGED *** \n"). } }/* END OF REVERSE FUNCTION */ . t=l. } start=t. l=l->next. l1=t=NULL. l=l->next. while(l!=NULL) { l1=t. else { l=start.

end enter the place to insert item:2 enter the position to place item:2 enter your choice:3 .middle 3.end enter the place to insert item:3 enter your choice:1 enter an item to be inserted:40 1.exit enter your choice:1 enter an item to be inserted:10 enter your choice:1 enter an item to be inserted:20 1.sort 6.start 2.merge 7.middle 3.Insertion 2.start 2.search 5.middle 3.Deletion 3.end enter the place to insert item:1 enter your choice:1 enter an item to be inserted:30 1.Output: **** MENU **** 1.middle 3.end enter the place to insert item:2 enter the position to place item:3 enter your choice:1 enter an item to be inserted:50 1.start 2.Traverse 4.reverse 8.start 2.

start 2.start: 20 50 10 40 last: 30 enter your choice:6 enter number items to be inserted in second list:3 enter an item:60 enter an item:70 1.end enter the place to insert item:3 enter an item:80 1.end enter the place to insert item:1 *** LIST IS MERGED *** enter your choice:3 start:20 50 10 40 30 80 60 last:70 enter your choice:4 enter the search item:80 item 80 found at position 6 enter your choice:4 enter the search item:10 item 10 found at position 3 enter your choice:7 *** LIST IS REVERSED *** .start 2.middle 3.middle 3.

start 2.start:20 30 50 60 last: 70 enter your choice:2 1.middle 3.middle 3.end enter the place to delete the item:1 deleted item is :10 enter your choice:2 1.start 2.start 2.middle 3.end enter your choice:3 start:70 60 80 30 40 10 50 last: 20 enter your choice:5 THE SORTED ORDER IS: 10 20 30 40 50 60 70 80 enter your choice:2 1.end enter the place to delete the item:2 enter the position to delete an item:3 deleted item is:40 enter your choice:3 .end enter the place to delete the item:3 deleted item is:80 enter your choice:2 1.start 2.middle 3.

middle .end enter the place to delete the item:2 enter the position to delete an item:3 deleted item is:50 enter your choice:2 1.middle 3.middle 3.end enter the place to delete the item:2 enter the position to delete an item:1 deleted item is:30 enter your choice:2 1.start 2.middle 3.start 2.start 2.start 2.end enter the place to delete the item:2 enter the position to delete an item:3 deleted item is:50 enter your choice:2 1.start 2.enter the place to delete the item:2 enter the position to delete an item:4 deleted item is:60 enter your choice:2 1.end enter the place to delete the item:4 enter your choice:3 start:20 30 50 last: 70 enter your choice:2 1.middle 3.

start 2. Push operation is used to insert the elements into a stack and pop operation is used to remove the elements in to a stack ALGORITHM FOR INSERTING AN ELEMENT IN A STACK: Function Push(s.top.end enter the place to delete the item:1 deleted item is :20 enter your choice:3 last:70 enter your choice:2 1.x) Step 1: [Check for stack overflow] If top>=n Then printf(“stack overflow”) Return Step 2: [Increment Top] Top<-top+1 Step 3: [ Insert element] S[top]<-x Step 4:[finished] .3.middle 3. Here they stack operation are push and pop.end enter the place to delete the item:1 deleted item is :70 enter your choice:3 *** LIST IS EMPTY *** enter your choice:2 *** LIST IS EMPTY *** enter your choice:8 Week 15 AIM: Write C programs that implement stack (its operations) using i) Arrays Description: In this program we have to implement the stack operation by using the arrays.

Return ALGORITHM FOR DELETING AN ELEMENT FROM A STACK: Function POP(s.top) Step 1: [Check for stack underflow] If top=0 Then printf(“stack underflow”) Exit Step 2: [Decrement Top] Top<-top-1 Step 3: [Return former top element of stackwwwww] Return(S[top+1]) Step 4:[finished] Return Flowchart: .

Program: .

item. EXIT\n"). while(1) { printf(" *** MENU ***\n 1. PUSH\n 2. printf("enter your choice from menu:"). scanf("%d". . case 2:pop(). case 3:traverse().h> # define size 4 int choice. break. TRAVERSE\n 4. POP\n 3. default:printf("wrong choice\n"). break. break.&choice).top=0. case 4:exit().# include <stdio. switch(choice) { case 1:push(). main() { clrscr().a[size]. } } getch().

scanf("%d". } } . top--. printf("the deleted item from stack is %d\n". } } pop() { if(top==0) printf("*** stack is empty ***\n"). else { printf("enter the item to be pushed into the stack:"). else { item=a[top]. top++.item).&item). a[top]=item.} push() { if(size==top) printf("*** stack is full ***\n").

} } Input/Output: *** MENU *** 1. PUSH 2.i).a[i].i<=top.a[i].traverse() { int i. POP 3. else { printf("*** stack display ***\n").i). else printf("%d at %d\n".i++) if(i==top) printf("%d at %d ->top\n". if(top==0) printf("**** stack is empty ****"). TRAVERSE 4. for(i=1. EXIT enter your choice from menu:1 enter the item to be pushed into the stack:11 *** MENU *** .

EXIT . POP 3. EXIT enter your choice from menu:1 enter the item to be pushed into the stack:12 *** MENU *** 1. POP 3. POP 3. TRAVERSE 4. TRAVERSE 4. EXIT enter your choice from menu:1 enter the item to be pushed into the stack:14 *** MENU *** 1. TRAVERSE 4. EXIT enter your choice from menu:1 enter the item to be pushed into the stack:13 *** MENU *** 1. POP 3.1. PUSH 2. TRAVERSE 4. PUSH 2. PUSH 2. PUSH 2.

TRAVERSE 4. PUSH 2.enter your choice from menu:1 *** stack is full *** *** MENU *** 1. EXIT enter your choice from menu:2 the deleted item from stack is 14 *** MENU *** 1. PUSH 2. EXIT . POP 3. TRAVERSE 4. PUSH 2. POP 3. POP 3. TRAVERSE 4. EXIT enter your choice from menu:3 *** stack display *** 11 at 1 12 at 2 13 at 3 14 at 4 ->top *** MENU *** 1.

EXIT enter your choice from menu:2 the deleted item from stack is 12 *** MENU *** 1. PUSH 2. EXIT enter your choice from menu:2 *** stack is empty *** *** MENU *** .enter your choice from menu:2 the deleted item from stack is 13 *** MENU *** 1. POP 3. TRAVERSE 4. POP 3. EXIT enter your choice from menu:2 the deleted item from stack is 11 *** MENU *** 1. POP 3. TRAVERSE 4. PUSH 2. TRAVERSE 4. PUSH 2.

TRAVERSE 4. PUSH 2. Push operation is used to insert the elements into a stack and pop operation is used to remove the elements in to a stack. TRAVERSE 4.1. PUSH 2. POP 3. Here they stack operation are push and pop. POP 3. EXIT enter your choice from menu:4 AIM: Write C programs that implement stack (its operations) using ii) Pointers Description: In this program we have to implement the stack operation by using the pointers. Algorithm: Step 1: Start Step 2: Declare the structure for the stack pointers. Step 3: Define the push function Step 4: Define the pop function Step 5: Define the display function Step 6: Read the choice . EXIT enter your choice from menu:3 **** stack is empty **** *** MENU *** 1.

remove the TOP cell. Step 10: Stop . Otherwise. Step 9: if choice=display Display all the elements in the Stack. print stack is empty.Step 7: if choice = push Create a cell for the TOP cell in the stack. If so. Place the date in the TOP cell Place the TOP pointer to the new cell Step 8: if choice=pop Check if empty stack.

num2=0 While(1) Read choice Push( ) Pop( ) Display( ) Exit( ) break break break break stop Switch choice .Flowchart: Start Define structure for st_point pointer Define push( ) Define pop( ) Define display ( ) Read choice. num1=0.

PUSH( ) Read element.st_pointer M=(struct st_point*)malloc(sizeof(struct st_point)) M ->elec=j M -> l=t T=m Return to main program Pop ( ) F T If t=NULL Print stack is empty I = t->ele T =t -> l Return i Return to main program .

h> . } #include<stdio. struct st_point *l.h> struct st_point { int ele.Display ( ) Define st_point * pointer= NULL Pointer = t While(pointer!= NULL) true Print the element Pointer=pointer->1 Return to main program Program: #include<conio.

printf("\n[2] Using Pop Function"). int i. printf("\n[1] Using Push Function").num2=0.&choice). void push_ele(int j). scanf("%c".*t. printf("\n[3] Elements present in Stack"). int pop_ele(). while(1) { clrscr(). printf("\n\t\t MENU ").num1=0. void main() { char choice. int i. printf("\n[4] Exit\n"). printf("\n\tEnter your choice: "). printf("======================================"). . printf("\n======================================"). fflush(stdin). void display_ele().

num2). printf("\n\tElement to be popped: %d\n\t".&num1). break. getch(). break. push_ele(num1). getch(). } . break. } case 2: { num2=pop_ele(1).switch(choice-'0') { case 1: { printf("\n\tElement to be pushed:"). display_ele(). scanf("%d". } case 3: { printf("\n\tElements present in the stack are:\n\t").

m->l=t. } } } /*Inserting the elements using push function*/ void push_ele(int j) { struct st_point *m.\n"). return. m->ele=j. break. break.case 4: exit(1). m=(struct st_point*)malloc(sizeof(struct st_point)). default: printf("\nYour choice is invalid. t=m. } /*Removing the elements using pop function*/ int pop_ele() { .

pointer->ele). } return 0. pointer=t.if(t==NULL) { printf("\n\STACK is Empty. t=t->l."). pointer=pointer->l. getch(). } . } /*Displaying the elements */ void display_ele() { struct st_point *pointer=NULL. exit(1). while(pointer!=NULL) { printf("%d\t". } else { int i=t->ele. return (i).

} Output: ====================================== MENU ====================================== [1] Using Push Function [2] Using Pop Function [3] Elements present in Stack [4] Exit Enter your choice: 1 Element to be pushed:23 MENU ====================================== [1] Using Push Function [2] Using Pop Function [3] Elements present in Stack [4] Exit ====================================== .

Enter your choice: 3 Elements present in the stack are: 23 ====================================== MENU ====================================== [1] Using Push Function [2] Using Pop Function [3] Elements present in Stack [4] Exit Enter your choice: 2 Element to be popped: 23 ====================================== MENU ====================================== [1] Using Push Function [2] Using Pop Function [3] Elements present in Stack [4] Exit Enter your choice: 4 Exit the program .

Here they Queue operation are push and pop.R) Step 1: [Underflow] If F=0 Then printf(“Queue underflow”) Return(0) Step 2: [Delete element] y<-q[f] Step 3: [Is Queue Empty?] If F=R Then F=R=0 Else F=F+1 Step 4:[Return element] Return(r) .N.F.Y) Step 1: [overflow] If R>=N Then printf(“ overflow”) Return Step 2: [Increment rear pointer] R<-R+1 Step 3: [ Insert element] Q[R]<-y Step 4: [Is front pointer properly set?] If F=0 Then f<-1 Return ALGORITHM FOR DELETING AN ELEMENT FROM A STACK: Function QDELETE(Q.Week 16 AIM: Write C programs that implement Queue (its operations) using i) Arrays ii) Pointers Description: In this program we have to implement the Queue operation by using the arrays. Push operation is used to insert the elements into a Queue and pop operation is used to remove the elements in to a Queue. ALGORITHM FOR INSERTING AN ELEMENT IN TO A QUEUE: Function QINSERET(Q.F.R.

Flowchart: .

rear++. while(1) { printf("*** MENU ***\n 1. main() { clrscr(). a[rear]=item.h> # define size 4 int front=0.item. scanf("%d". DELETION\n 3. case 4:exit(). case 3:traverse(). break.&choice). break. } insertion() { if(rear==size-1) printf("*** queue is full ***\n"). scanf("%d".Program: # include <stdio. else { printf("enter item into queue:"). INSERTION\n 2.TRAVERSE\n 4. default:printf("*** wrong choice ***\n").a[size]. case 2:deletion(). } } getch(). EXIT\n").rear=-1.&item).choice. switch(choice) { case 1:insertion(). printf("enter your choice:"). break. . } } deletion() { if(front==rear+1) printf("*** queue is empty ***\n").

DELETION 3.a[i]. INSERTION 2.i<=rear. DELETION 3.a[i].i). TRAVERSE 4.i). if(front==rear+1) printf("*** queue is empty ***\n"). front++.} traverse() { int i. } *** MENU *** 1. } } else { item=a[front].i).i). printf("the deleted item from queue is %d\n". INSERTION 2.a[i]. TRAVERSE 4. EXIT enter your choice:1 enter item into queue:11 *** MENU *** 1. else { for(i=front. else if(i==front) printf("%d at %d ->front\n". else if(i==rear) printf("%d at %d ->rear\n".item).a[i].i++) if(i==front && rear==i) printf("%d at %d ->front=rear\n". else printf("%d at %d\n". EXIT Output: .

INSERTION 2. TRAVERSE 4. DELETION 3. INSERTION 2. EXIT enter your choice:1 *** queue is full *** *** MENU *** 1. TRAVERSE 4.enter your choice:1 enter item into queue:12 *** MENU *** 1. DELETION 3. DELETION 3. INSERTION 2. TRAVERSE 4. EXIT enter your choice:1 enter item into queue:13 *** MENU *** 1. DELETION 3. INSERTION 2. TRAVERSE 4. TRAVERSE 4. EXIT enter your choice:3 11 at 0 ->front 12 at 1 13 at 2 14 at 3 ->rear *** MENU *** 1. EXIT enter your choice:1 enter item into queue:14 *** MENU *** 1. DELETION 3. INSERTION 2. EXIT enter your choice:2 the deleted item from queue is 11 *** MENU *** 1. INSERTION 2. TRAVERSE . DELETION 3.

INSERTION 2. INSERTION 2. EXIT enter your choice:3 *** queue is empty *** *** MENU *** 1. INSERTION 2. TRAVERSE 4. DELETION 3. TRAVERSE 4. EXIT enter your choice:2 the deleted item from queue is 14 *** MENU *** 1. DELETION 3. EXIT enter your choice:2 *** queue is empty *** *** MENU *** 1. TRAVERSE 4. EXIT enter your choice:2 the deleted item from queue is 12 *** MENU *** 1. DELETION 3. INSERTION 2. TRAVERSE 4. EXIT enter your choice:2 the deleted item from queue is 13 *** MENU *** 1. DELETION 3. TRAVERSE 4.4. INSERTION 2. DELETION 3. EXIT enter your choice:4 .

AIM: Write C programs that implement Queue (its operations) using ii) Pointers Description: In this program we have to implement the Queue operation by using the pointers. Here they Queue operation are push and pop. Push operation is used to insert the elements into a Queue and pop operation is used to remove the elements in to a Queue. Algorithm: Step 1: Start Step 2: define structure for queue Step 3: read choice Step 4: if choice = insert i)read the element ii)create a data structure iii)if empty queue then front of queue pinter points to newly created data structure iv)otherwise end of the queue points to newly created data structure Step 5: if choice= remove i)check if queue is empty . if so, print queue is empty ii)otherwise read the element pointed by front of the queue temp pointer points to front of queue iii)front of queue points to next element iv)free the element pointed by temp pointer v)return the element vi)print the element Step 6: if choice = display i)check of empty queue if so, print queue empty ii)otherwise print the elements from front of the queue until the end of the queue step 7: if choice=exits stop

Flowchart: start
Define the structure for queue

B T

Read the choice If choice=insert

F Read the element to be inserted
Queue ->ele=ele Queue ->n=NULL

F T F

If choice=remov If !e_que() Print queue is empty

T F T T

If f_ptr=NULL

F+ptr=queue B Ptr->n=queue Ptr=f_ptr ptr ->n!=NULL

B Print j F B

If choice=display

If choice=exit stop

T T F

Ptr=f_ptr Print invalid choice A

A T
If empty queue

F

Print empty queue If ptr!=NULL

T

Print ptr -> ele

B

Ptr= ptr-> n

program: #define true 1 #define false 0 #include<stdio.h> #include<conio.h> #include<process.h> struct q_point { int ele; struct q_point* n; }; struct q_point *f_ptr = NULL; int e_que(void); void add_ele(int); int rem_ele(void); void show_ele(); /*main function*/ void main()

{ int ele,choice,j; while(1) { clrscr(); printf("\n\n****IMPLEMENTATION OF QUEUE USING POINTERS****\n"); printf("=============================================="); printf("\n\t\t MENU\n"); printf("=============================================="); printf("\n\t[1] To insert an element"); printf("\n\t[2] To remove an element"); printf("\n\t[3] To display all the elements"); printf("\n\t[4] Exit"); printf("\n\n\tEnter your choice:"); scanf("%d", &choice); switch(choice) { case 1: { printf("\n\tElement to be inserted:"); scanf("%d",&ele); add_ele(ele); getch(); break; } case 2: { if(!e_que()) { j=rem_ele(); printf("\n\t%d is removed from the queue",j); getch(); } else { printf("\n\tQueue is Empty."); getch(); } break;

} case 3: show_ele(); getch(); break; case 4: exit(1); break; default: } } printf("\n\tInvalid choice."); getch(); break; } /* Function to check if the queue is empty*/ int e_que(void) { if(f_ptr==NULL) return true; return false; } /* Function to add an element to the queue*/ void add_ele(int ele) { struct q_point *queue = (struct q_point*)malloc(sizeof(struct q_point)); queue->ele = ele; queue->n = NULL; if(f_ptr==NULL) f_ptr = queue; else { struct q_point* ptr; ptr = f_ptr; for(ptr=f_ptr ;ptr->n!=NULL; ptr=ptr->n);

ptr->n = queue; } } /* Function to remove an element from the queue*/ int rem_ele() { struct q_point* queue=NULL; if(e_que()==false) { int j = f_ptr->ele; queue=f_ptr; f_ptr = f_ptr->n; free (queue); return j; } else { printf("\n\tQueue is empty."); return -9999; } } /* Function to display the queue*/ void show_ele() { struct q_point *ptr=NULL; ptr=f_ptr; if(e_que()) { printf("\n\tQUEUE is Empty."); return; } else { printf("\n\tElements present in Queue are:\n\t"); while(ptr!=NULL) { printf("%d\t",ptr->ele); ptr=ptr->n; }

} } Output: ****IMPLEMENTATION OF QUEUE USING POINTERS**** ============================================== MENU ============================================== [1] To insert an element [2] To remove an element [3] To display all the elements [4] Exit Enter your choice:1 Element to be inserted:23 Enter your choice: 3 Elements present in Queue are: 23 ****IMPLEMENTATION OF QUEUE USING POINTERS**** ============================================== MENU ============================================== [1] To insert an element ****IMPLEMENTATION OF QUEUE USING POINTERS**** ============================================== MENU ============================================== [1] To insert an element [2] To remove an element [3] To display all the elements [4] Exit

Here we made use of stack operations. push it onto the stack else if stack is empty or the operator has higher priority than the operator on the topof stack or the top of the stack is opening parenthesis then push the operator onto the stack else . the item that is inserted last will be the firt item remove.[2] To remove an element [3] To display all the elements [4] Exit Enter your choice:2 23 is removed from the queue ****IMPLEMENTATION OF QUEUE USING POINTERS**** ============================================== MENU ============================================== [1] To insert an element [2] To remove an element [3] To display all the elements [4] Exit Enter your choice:4 Exit Week 17 AIM: Write a C program that uses Stack operations to perform the following: i) Converting infix expression into postfix expression Description: In this program we have to covert the given infix expression to postfix expression and the finally evaluating that postfix expression. i. for each character in the input string If input string is an operand. The property of stacks is last in first out. start Step 2. ALGORITHM: Step 1. append to the output if the input string is a left paranthesis . first initialize the stack to be empty Step 3..e.

if the end of the input string is encountered .h> #include<ctype. printf("enter the string"). for(i=0. int top=-1. Pop the opening parenthesis from the stack and discard it. main() { char in[20]. Step 5.i<l. pop operators from the stack and append the operators to the output until an opening parenthesis is encountered.pop the operator from the stack and append to the output Step 4.i++) if(isalpha(in[i])) post[j++]=in[i]. l=strlen(in). else { if(in[i]=='(') push(in[i]). Step 6.j=0. else { while(priority(in[i])<=priority(str[top])) post[j++]=pop().post[20].j. push(in[i]). clrscr(). stop Program: #include<stdio.l. else if(in[i]==')') while((ch=pop())!='(') post[j++]=ch. gets(in). then iterate the loop until the stack is not empty. Pop the stack and append the remaining input string to the output.ch.h> static char str[20].h> #include<string. . int i. if the input string is a closing parenthesis .

getch(). } pop() { return(str[top--]). post[j]='\0'. } push(char c) { str[++top]=c.} } while(top!=-1) post[j++]=pop(). case'$': return 3. case'*': case'/': return 2. printf("\n equivalent infix to postfix is:%s". } return 0.post). } Output: enter the string(a+b)-(c-d)*e/f equivalent infix to postfix is:ab+cd-e*f/enter the stringa+b/c*d equivalent infix to postfix is:abc/d*+ . } priority (char c) { switch(c) { case'+': case'-': return 1.

.h> #include<ctype.2: op2=pop() Step 7: c=op2+op1 Step 8: Push(c) Step 9: c=op2-op1 Step 10: Push(c) Step 11: c=pow(op2.op1) Step 12: Push(c) Step 13: c=op2/op1 Step 14:Push(c) Step 15: Print the result Step 16:Push(int x) Step 17:Increment top by 1 Step 18: s1.1:op1=pop() Step 6.op1.top— Step 23:return x Step 24: Stop Program: #include<stdio.h> int stk[10].top=0. clrscr().top3)=x Step 19:pop() Step 20: Read x Step 21: x1=s1.AIM: Write a C program that uses Stack operations to perform the following: ii) Evaluating the postfix expression ALGORITHM: Step 1: Start Step 2: Assign top=-1 Step 3: Read the input expression Step 4: for i=0. main() { char postexp[10].i.op2.top] Step 22:s1.item[s1.1:Push(ch) Step 6: otherwise Step 6.s[i]!=’\0’ in steps of 1 Step 5: If isdigit(ch) Step 5.item(s1.

getch().i++) { if(isdigit(postexp[i])) push(postexp[i]-48).':exit(). } } } printf("the result of postfixexpression is: %d". stk[top]=x. break. case '%':push(op1%op2). break.pop() { return(stk[top--]). switch(postexp[i]) { case '+':push(op1+op2). break. case '-':push(op1-op2). case '. break. } . for(i=0. case '*':push(op1*op2). else { op1=pop(). break. gets(postexp). case '/':push(op1/op2).postexp[i]!='\0'. } push(int x) { top++. } printf("enter the postfix expression:").pop()). op2=pop().

ii)Preorder traversal: Here root is visitedfirst follwed by left child and finally by right child.Output: enter the postfix expression:234+the result of postfix expression is: 5 Week 18 AIM: Write a C program that uses functions to perform the following: i) Creating a Binary Tree of integers ii) Traversing the above binary tree in preorder. ALGORITHM: Step 1: Start Step 2: Define a structure btree Step 3: Type define struct btree as node Step 4: While(tree). begin Step 5: Print MENU Step 6: Write your choice Step 7: If choice=1 Step 8: Enter your no of nodes Step 9:Read nodes n Step 10: for i=1 to n in steps of 1 do Step 11: Print enter item Step 12: Read item Step 13: Root =call create (root. The binary tree consist of almost two childrens.end for Step 14: if choice=2 Step15: Read element to be deleated Step 16: Call delete(root. iii)Postorder traversal: Here left child is visited first followed by right child finally by the root. The proparty of a binary tree is the value at the root must be grater than the left node and less than the right node. A tree can can be traversed in three major ways i)Inorder traversal: here left child is visited first followed by root and finally by right child. inorder and postorder. Tree is a recursive data structure and recursive programming techniques are popularly used in trees. item) end for Step 17: If choice=3 Step 18: Call preorder(root) . Binary tree is bit special. item). Description: The data structure tree is of non linear type.

t) Step 10: T->rc=call isert(e->lc.Step 19: Call postorder(root) Step 20: Call inorder(root) Step 21: Break.t) Step 11:Return t Step 12: Stop For DELETION function Step 1: Start Step 2: x=d Step 3: while x!=null Step 4: If x->data =t Strep 5:Break Step 6: Parent =x Step 7: if t<x->data Step 8: t=t lc Step 9: t=lrc Step 10: If xlc!=null &&xrc!=null Step11: parent =x Step12: If parent==null Step 13: parentlc==null Step 14: parentrc==null Step 15: If p->lc=x->rc Step 16: If p->rc=x->rc Step 17: While insert->lc=null . end of switch Step 22: Stop For insert function Step 1: Start Step 2: If t= null Step 3: Allocate memory to temp Step 4: Temp->data =item Step 5: Temp-> lc=null Step 6: Temp->rc=null Step 7: return t to main and end Step 8: If item< (l->data) Step 9: T->lc=call insert(e->lc.

Step 18: Insert=insert->la Step 19:x->data=insert->data Step 20:x=insert Step 21: Return d Step 22: Stop For INORDER function Step 1: Start Step 2: If t!=null Step 3: Call inorder(t->lc) Step 4: Write t->data Step 5: Call inorder(t->rc) Step 6: Stop For POSTORDER function Step 1: Start Step 2: If t!=null Step 3: Call postorder(t->lc) Step 4: Call postorder(t->rc) Step 5: Write data Step 6: Stop For PREORDER function Step 1: Start Step 2: If t!=null Step 3: Write data Step 4: Call postorder(t->lc) Step 5: Call postorder(t->rc) Step 6: Stop .

Flowchart: .

.

clrscr(). } break. for(i=1. scanf("%d".i<=n.&ele). main() { int ch. int top=-1.i++) { scanf("%d". t=insert(t. struct bstnode *lc.top1=-1. else { printf("INORDER :"). while(1) { printf("\n **** M E N U **** \n").h> struct bstnode { int data.*b[20]. switch(ch) { case 1: printf("Enter how many elements u want to insert:"). printf("Enter your choice: ").EXIT\n"). struct bstnode *t.*rc.ele.n.ele).INSERT\n2.i.NON-RECURSIVE TRAVERSE\n4. case 2: /* RECURSSIVE TRAVERSE */ if(t==NULL) printf("**** TREE IS EMPTY ****").RECURSSIVE TRAVERSE\n3.Program: #include<stdio.&ch).h> #include<alloc.*insert(). printf("1.*a[20].&n). scanf("%d". .*pop(). printf("Enter tree elements: "). }*root. t=root=NULL.

y). printf("\nPREORDER :"). nrpostorder(t). return(x). } else { if(y<x->data) x->lc=insert(x->lc. case 3: /* NON-RECURSSIVE TRAVERSE */ if(t==NULL) printf("TREE IS EMPTY").int y) { if(x==NULL) { x=malloc(sizeof(struct bstnode)).y). x->data=y. printf("\nPREORDER :"). printf("\nPOSTORDER :"). nrinorder(t). x->lc=NULL. postorder(t). } } } struct bstnode *insert(struct bstnode *x. preorder(t). case 4: exit(). } break.inorder(t). else x->rc=insert(x->rc. . x->rc=NULL. printf("\nPOSTORDER :"). } break. else { printf("INORDER :"). nrpreorder(t).

x->data).x->data). preorder(x->lc). postorder(x->rc). } while(top>-1) .x->data).} } inorder(struct bstnode *x) { if(x!=NULL) { inorder(x->lc). inorder(x->rc). printf("%3d". printf("%3d". } } nrinorder(struct bstnode *x) { struct bstnode *l. } } preorder(struct bstnode *x) { if(x!=NULL) { printf("%3d". } } postorder(struct bstnode *x) { if(x!=NULL) { postorder(x->lc). preorder(x->rc). l=l->lc. do { while(l!=NULL) { push(l). l=x.

if(l->rc!=NULL) push(l->rc).{ l=pop(). } else l=NULL. b[++top1]=l->rc. break. l=l->lc. }while(l!=NULL). } . } nrpreorder(struct bstnode *x) { struct bstnode *l. do { while(l!=NULL) { push(l). if(l->rc!=NULL) { push(l->rc). l=x. do { printf("%d".l->data). if(l->rc!=NULL) { l=l->rc.l->data). if(l==NULL&&top>-1) l=pop(). } nrpostorder(struct bstnode *x) { struct bstnode *l. l=x. } }while(l!=NULL). printf("%d".

Insert 2. } } while(top>-1).l->data). Insert 2.Delete 3. } push(struct bstnode *y) { top+=1.l=l->lc. } struct bstnode *pop() { return a[top--]. }while(l!=NULL&&top>-1). if(l!=b[top1]) printf("%3d". break.Insert 2.Delete 3.Delete 3. } do { l=pop().Traversal Enter the element 92 Enter your choice 1. else { top1-=1.Delete 3.Traversal Enter the element 12 Enter your choice 1.Traversal Enter the element 123 Enter your choice . } Output: Enter your choice 1. a[top]=y. Traversal Enter the element 26 Enter your choice 1.Insert 2.

The key which is to be searched is compared with each element of the list one by one.n. start 2. ALGORITHM: 1.1. If the end of list is reached it means that the search has failed and key has no matching in the list.Delete 3. print successful search 9. for i=1 to n increment in steps of 1 3.Traversal 3 InorderSequence: 12 26 92 123 128 135 Preorder sequence:92 26 12 123 135 128 Postorder sequence: 12 26 128 135 12 92 Week 19 Aim: Write C programs that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers : i) Linear search Description: The linear search is most simple serching method.Delete 3. if search equal to 0 goto step 7 otherwise goto step 8 7. return i 5.Insert 2. print unsuccessful search 8. It does not expect the list to be sorted. Read the element(x) to be searched 5.Traversal Enter the element 135 Enter your choice 1. return 0 6.x) 6. stop . If a match exists. Start 2.Insert 2.Traversal Enter the element 128 Enter your choice 1. search<--linear(a.Insert 2.Delete 3. if m equal to k[i] goto step 4 otherwise goto step 2 4. for i=1 to n increment in steps of 1 Read the value of ith element into array 4. stop LINEAR FUNCTION 1. the search is terminated. Read the value of n 3.

Flowchart: start Read n i=1 i<=n i++ Read a[i] Read b C=linear search(a.b) T If c!=0 F Print c Print element is not found stop .

scanf("%d".&a[i]). clrscr(). printf("enter range for array:"). } else if(i==n) .key.&n). printf("enter the search element:").i++) T If m==k[i] T stop F Program: #include<stdio.j. printf("enter elements into array:").n.key.i++) { if(key==a[i]) { printf("element %d found at %d".i<=n. scanf("%d".start F For(i=1.i<=n.i).h> Void main() { int i.a[10]. for(i=0.&key).i<n.i++) scanf("%d". break. for(i=0.

Read the value of n 3. } Output: enter range for array:4 enter elements into array:56 43 12 88 9 enter the search element:9 element 9 found at 4 enter range for array:5 enter elements into array:23 12 56 34 3 8 enter the search element:3 element 3 found at 4 Description: Binary search is a vast improvement over the sequential search.printf("element %d not found in array". } getch(). ALGORITHM: BINARY SEARCH 1. for i=1 to n increment in steps of 1 Read the value of ith element into array Aim :Write C programs that use both recursive and non recursive functions to perform the following searching operations for a Key value in a given list of integers : ii) Binary search . binary search fails. the item in the list must be in assorted order. The approach employed in the binary search is divid and conquer. If the list to be sorted for a specific item is not sorted.key). Start 2. For binary search to work.

n. if low<= high repeat through steps 4 to 9 otherwise goto step 10 4.high to n.stop . print successful search 9. test to 0 3.return mid 10. search<--binary(a.if m>k[mid] goto step 8 otherwise goto step 9 8. return 0 11.4.if m<k[mid] goto step 6 otherwise goto step 7 6.print unsuccessful search 8.assign mid+1 to low 9.assign mid-1 to high goto step 3 7.x) 6.assign (low+high)/2 to mid 5. start 2. initialise low to 1 . stop BINARY SEARCH FUNCTION 1. Read the element(x) to be searched 5. if search equal to 0 goto step 7 otherwise goto step 8 7.

Flowchart: .

.

high.mid.&a[i]). } . for(i=0. if(a[mid]==key) { printf("element %d found at %d".Program: Output: enter range for array:4 enter elements into array:12 23 34 45 the search element:45 element 45 found at 3 #include<stdio.n. high=n-1.i<n. break.i++) scanf("%d".i++) { mid=(low+high)/2. for(i=0. scanf("%d".&n).a[10]. clrscr().i<n. low=0.key.low.key. scanf("%d". printf("enter range for array:"). printf("the search element:").h> Void main() { int i. } getch().mid).&key). if(i==n-1) printf("element %d not found in array".key). else low=mid+1. printf("enter elements into array:"). } if(key<a[mid]) high=mid.

initialise last to n 3. Algorithm: Bubble Sort: 1. If first elements is less than second one. read the value of n 3. they are left undistrurbed. The order of bubble sort algorithm is O(n2).enter range for array:5 enter elements into array:1 34 56 78 88 the search element:45 element 45 not found in array Week 20 Aim :Write a C program that implements the following sorting methods to sort a given list of integers in ascending order i) Bubble sort Description: Bubble sort is the simplest and oldest sorting technique. for i= 1 to n increment in steps of 1begin 4.initialise ex to 0 5. This method takes two elements at a time. If the first element is greater then second one then they are swapped. for i= 1 to n increment in steps of 1 print the value of ith element in the array 6.if ex!=0 assign last-1 to lastend for loop 12. stop BUBBLE SORT FUNCTION 1. But bubble sort is an inefficient algorithm. start 2.assign k[i] to temp assign k[i+1] to k[i] assign temp to k[i+1] increment ex by 1 end-ifend inner for loop 11. start 2. The procedure continues with the next two elements goes and ends when all the elements are sorted.if k[i]>k[i+1] goto step 7 otherwise goto step 5 7.n)) 5. stop begin . call function to sort (bubble_sort(a. for i= 1 to n increment in steps of 1 Read the value of ith element into array 4. It compare these two elements.for i= 1 to last-1 increment in steps of 1begin 6.

Flowchart: .

n.i<n.Program: #include<stdio.&a[i]).a[i]).i<n. getch().&n). clrscr(). for(i=0. printf("enter the range of array:").h> main() { int i.j.j++) if(a[i]>a[j]) { t=a[i].t.i++) printf("\t%d".a[5].i++) for(j=i+1. scanf("%d". for(i=0. } 3 23 34 56 Output: enter the range of array:3 enter elements into array:3 2 1 the sorted order is: 12 enter the range of array:5 enter elements into array:56 23 34 12 8 the sorted order is: 812 .j<n.i++) scanf("%d". } printf("the sorted order is:"). printf("enter elements into array:").i<n-1. a[j]=t. a[i]=a[j]. for(i=0.

i to pivot 4.e.upperbound) end if 14. if lowerbound < upperbound repeat through steps 3 to 13 otherwise goto step 14 begin 3.call function qsort(x.Aim : Write a C program that implements the following sorting methods to sort a given list of integers in ascending order ii) Quick sort Description: This method is invented by hoare.call function qsort(x. assign lowerbound to i. if i< j goto step 10 other wise goto step 4 Begin 10.j-1) 13. assign j-1 to j end if 9.call function to swap k[i] and k[j] end if end if 11. if k[j] > k[pivot] repeat through step 8 otherwise goto step 9 begin 8. start 2. The method is based on divide and conquer technique. if a[i]<=k[pivot] and i< upperbound repeat through step 6 otherwise goto step 7 begin 6. Usually the first element is considerd to be the pivot element. considered to be fast method to sort the elements. Now move the pivot element to its correct position in the list. The process is reapplied to each of these partitions till we got the sorted list of elements. The elements to the left and pivot element are less that this while the elements to the right of pivot are greater than the pivot. i. Algorithm: 1. In this method the list is divided into two baesd on an element called pivot element..j+1. the entire list is divided into various partitions and sorting is applied again and again on the partition. if i<j repeat through steps 5 to 10 otherwise goto step _ Begin 5.call function to swap k[pivot] and k[j] 12. stop . assign i+1 to i end if 7.lowerbound.upperbound to j. The method is also called partition exchange sorting.

&n). scanf("%d".h> Void main() { int x[10]. clrscr().i.Flowchart: Program: #include<stdio.n. . printf("enter no of elements:").

x[j]=t. getch(). while(i<j) { while(x[i]<=x[pivot] && i<last) i++.i<=n. quicksort(x.i++) printf("%3d".i<=n.j. if(i<j) { t=x[i].1.n).&x[i]). x[i]=x[j].int first. if(first<last) { pivot=first. for(i=1.t. j=last.i.i++) scanf("%d". } } t=x[pivot].n).x[i]).j+1. x[pivot]=x[j]. quicksort(x. } quicksort(int x[10]. i=first.printf("enter %d elements:". printf("sorted elements are:"). while(x[j]>x[pivot]) j--.last).j-1).first. x[j]=t.int last) { int pivot. for(i=1. }} OUTPUT : enter no of elements:6 enter 6 elements:23 . quicksort(x.

j. If the condition true then goto if condition 6) If the condition if(arr[i]>arr[j]) is true then do the following steps i)temp=arr[i] ii)arr[i]=arr[j] iii)arr[j]=temp 7) increment the j value 8) perform the loop operation for the displaying the sorted elements. If the condition is true print the array elements and increment the I value. 5) Read the loop and check the condition. Else goto step 4 4) Read the loop and check the condition. 9) print the sorted elements 10) stop . the 0th element is compared with all other eements. If the 0th element is found to be greater than the compared element then they are interchanged. to sort the data in ascending order. If the condition true then goto next loop.temp and arr[] 3) Read the loop and check the condition. In this method. Algorithm: 1) Start 2) Initiliaze the variables I.12 45 34 21 87 sorted elements are: 12 21 23 34 45 87 Week 21 Aim : Write C program that implements the following sorting method to sort a given list of integers in ascending order: i) Selection sort Description: This is the simplest method of sorting.

j.satrt FLOW CHART Initialize the i.temp.arr[] I=0 I<=4 Print the arr[i] elements i++ I=0 I<=3 J=j+1 J<=4 j++ i++ If arr[i]>arr[j] Temp=arr[i] Arr[i]=arr[j] I=0 I<=4 stop Print arr[i] I++ .

i<=3.h> Void main() { Int arr[5]={25. Arr[i]=arr[j]. } Output: 1) Section sort Array before sorting: 25 17 3113 2 Array after sorting: 2 13 17 25 31 2) section sort Array before sort 25 31 30 12 1 Array after sort 1 12 25 30 31 .i++) Printf(“%d\t”. Int I. Printf(“\n array before sorting:\n”). Printf(“selection sort\n”).i++) Printf(“%d\t. For(i=0. For(i=0. } } } Printf(“\n\n array after sortong:\n”).temp.i++) { For(j=j+1.31.17. Getch(). Arr[j]=temp.arr[i]).i<=4. For(i=0.2}.j<=4.Program: #incude<stdio.13.j. Clrscr().j++) { If(arr[i]>arr[j]) { Temp=arr[i].arr[i]”).i<=3.h> #incude<conio.

mid) Step5:merge_sort(x.start) Step6: performing the looping operation For(j=0.j++) then its true then goto step9 Step9: check the condition i)if(mrg2<=start-end) is true goto ii). Algorithm: main program Step1: Start Step2: declare the merge sort function Step3: Declare the array and their size and initailaze the j=0 Step4: read the array elements and then sort these elements. If not goto Step12.mrg2=0.mid+1. If not goto step10.j++) Step8: Stop Subprogram Step1:initialize the array excuting[MAX_ARY] and j=0. and then merged back together to form the final sorted list.mid=0.j<SIZE.j<Max_ary. If not goto step11 iii)If(executing[mrg1]>executing[mrg2]) is true then follows. Like most recursive sorts. for( j=0. X[j+end]= executing[mrg2++] Step10: x[j+end]=executing[mrg1++]. ii)If(mrg1<=mid-end) is true goto iii). and places them in separate arrays.j++) then its true Executing[j]=x[end+1] Mrg1=0.mrg1=0.end.j<SIZE.Aim :Write C program that implement the following sorting method to sort a given list of integers in ascending order: ii)Merge sort Description: The merge sort splits the list to be sorted into two equal halves. Each array is recursively sorted. the merge sort has an algorithmic complexity of O(n log n).size=start-end+1 Step2: check the condition if(end==start) then return Step3: calculate the mid value Mid=(end+start)/2 Step4: call themerge_sort(x. If not goto Step11 Step11: x[j+end]= executing[mrg2++] . Step6: call the merge sort function Step7: display the array elements after merge sort by using the following stament. Step7: calculate the mrg2=mid-end+1 Step8: performing the looping operation For(j=0. Step5: read the array elements before the merge sort and then display the elements.

Step12: x[j+end]=executing[mrg1++] Step13: return to main program Flow chart: Start Define merge_sort function Declare the array and array size Read the array elements False J=0 j<MAX_ARY True Display the elements before merge sort j++ Mergesort(ary.MAX_ARY-1) J=0 False j++ True Display the elements after merge sort Stop .0.

mid) Call merge_sort(x.mid+1.start) j=0 False j++ j=0 F T If mrg2<=start-end False X[j+end]=executing[mrg1++] True If mrg1<=mid-end False X[j+end]=executing[mrg2++] True j++ Executing[j]=x[end+j] Mrg1=0 True False X[j+end]=executing[mrg1++] If executing[mrg1]> executing[mrg2] Return to main program X[j+end]=executing[mrg2++] True .end.Merge_sort() Size=start-end+1 Mid=0 Mrg1=0 Mrg2=0 If end==start Mid=(end+start)/2 Call merge_sort(x.

1). int mid = 0. ary[j]).end + 1. int main(void) { int ary[MAX_ARY]. for(j=0. int mrg1 = 0. MAX_ARY . int start).j++) scanf("%d". j < MAX_ARY. mid + 1. int executing[MAX_ARY]. printf("\n").h> #include <stdlib. j++) executing[j] = x[end + j]. j++) printf(" %d". mid). for(j = 0. mrg1 = 0. printf("\n\nEnter the elements to be sorted: \n"). j < MAX_ARY. end. getch(). if(end == start) return. /* array after mergesort */ printf("After Merge Sort :"). for(j = 0. int end. int mrg2 = 0. start). merge_sort(ary. mrg2 = mid .Program: #include <stdio. .end + 1. printf("\n").j<MAX_ARY.h> #define MAX_ARY 10 void merge_sort(int x[]. int end. } /* Method to implement Merge Sort*/ void merge_sort(int x[]. merge_sort(x. 0. int j = 0. j++) printf(" %d". int start) { int j = 0. mid = (end + start) / 2. ary[j]).&ary[j]). merge_sort(x. for(j = 0. j < size. /* array before mergesort */ printf("Before :"). const int size = start .

j < size.end) if(executing[mrg1] > executing[mrg2]) x[j + end] = executing[mrg2++]. If (j≠i) then prodfunc=prodfunc X(x-xj) / (xi-xj) endfor Sep 8. Algorithm: Step 1. for J=1 to (n+1) in steps of 1 do Step 7. else x[j + end] = executing[mrg2++]. else x[j + end] = executing[mrg1++]. sum Step 10.n Step2. else x[j + end] = executing[mrg1++]. Sum=0 Step 4.fi end for {the above statements reads x. j++) { if(mrg2 <= start . Sum=Sum+fi x Prodfunc {sum is the value of f at x} end for Step 9. Read x. Week 22 AIM: Write C programs to implement the Lagrange interpolation .end) if(mrg1 <= mid . Profvnc=1 Step 6. for i=1 to (n+1) is steps of 1 do Read xi. for i=1 to (n+1) in steps of 1 do Step 5. Write x.s and the corresponding values of f is } Step 3. STOP .Output: Enter the elements to be sorted: 8234157690 Before : 8 2 3 4 1 5 7 6 9 0 After Merge Sort : 0 1 2 3 4 5 6 7 8 9 Enter the elements to be sorted: 7654843213 Before : 7 6 5 4 8 4 3 2 1 3 After Merge Sort : 1 2 3 3 4 4 5 6 7 8 } } for(j = 0.

Flowchart: .

i<n. } Output: Enter the value of n 4 Enter the value to be found 2.sum).sum.h> Main() { float y. Scanf(“%f”.f[20].&n).625000 1 2 3 4 . x[20]. Printf(“enter the values of xi’s & fi’s”).pf.Program: #include<stdio.j. Printf(“\n sum =%f ”. Printf(“enter the value of n”).&y). Printf(“enter the value to be found”).h> #include<math.n. } Sum += f[i] *pf.500000 Sum = 15. Scanf(“%d”.y). For(j=0.i++) { Pf=1.5 Enter the values for xi’s & fi’s 1 8 27 64 X = 2.j++) { If(j!=i) Pf *= (y-x[j])/(x[i] – x[j]). For(i=0.j<n. } Printf(“\nx = %f ”. Int I.

SUMY Step15: STOP .1: k←k-1 Step9.2:for j=0 to k do ∆iyj←∆i-1 yj+1-∆i-1yj Step10: Sumy←y0 Step11: Pvalue←1 Step12: Fact value←1 Step13: for l=1 to (n-1) do Step13.2: factvalue←factvaluex1 Step13.yi Step4: read x Step5: h←xi-x0 Step6: p←(x-xo)/n Step7: for j=0 to n-2 do ∆1yj←yj+1-∆i-1 Step8: k←n-2 Step9: for i=2 to (n-1)do Step9.AIM: Write C programs to implement the Newton. Algorithm: Step1: START Step2: Read n Step3: for i=0 to (n-1) do read xi.4: Sumy←Sumy+term Step14: Print x.3: term←(pvalue x ∆ly) / factvalue Step13.Gregory forward interpolation.1: Pvalue←pvalue x (p-(l-1)) Step13.

Flowchart: .

Term = pvalue* d[l][0] / factvalue. j++) d[i][j] =d[i-1][j+1] – d[i-1][j]. for(i=2. &y[i]). j.h> Main() { Int i. k. } . z. l++) { Pvalue *= (p-(l . Sumy += term. h = x[1] – x[0]. p. k=n-2. h. factvalue.i<n. for(j=0. y \n”. } Printf(“\n y value at z = %f is %f”.Program: #include<stdio. Scanf(“%d”. i<n.1)).n). term. l<n. sumy). Factvalue *= 1. p = (z . Printf(“\n enter z”). y[25]. Printf(“enter %d values for x. For(i=0. for(j=0. j++) d[i][j] =y[j+1] – y[j]. j<n-2. Scanf(“%f”. l. } For(l=1. z.&z). Printf(“enter the value of n”).i++) Scanf(“%f %f”.x[0] )/ h. pvalue.&n). d[25][25].h> #include<math. Float sumy. Float x[25]. n. i++) { k++. j<=k. &x[i].

Output: Enter n 7 Enter 7 data values for x.y Step 8. Sumxsq=0 Step 4. denom=n x sumxsq – sumx x sumx Step 13. fori=1 to n do Step 7. a1=(n x sumxy-sumx x sumy)/ denonm Step 15.a0 Step 16. STOP .000000 is 36. Sumx=0 Step 3.Sumxsq=Sumxsq+x2 Step 10. Sumx=sumx+x Step 9. a0=(sumy x sumxsq – sumx x sumxy) / denom Step 14. Sumy=0 Step 5. Read x. Write a1.Sumxy=sumxy+x x y end for Step 12. Read n Step 2. Sumxy=0 Step 6.756710 Week 23 AIM: Write C programs to implement the linear regression Algorithm: Step 1.Sumy=Sumy+y Step 11. y 1921 1931 1941 1951 1961 1971 1981 35 42 58 84 120 165 220 Enter z 1925 Y value at z = 1925.

Flowchart: .

sumxsq. sumxy. Float sumx.Program: #include<stdio. For(i=0.i<n.h> Main() { Int n. 2). &y). sumy. Sumx=0.a0). x. denom. A0 = (sumy * sumxsq –sumx *sumxy)/denom.&n). 2). a0. } Output: Enter the n value 7 12 25 47 510 612 815 919 Y = 1. Scanf(“%d”. A1 = (n * sumxy –sumx *sumy)/denom.&x.i++) { Scanf(“%f %f”. a1. } Denom = n * sumxsq – pow(sumx. Sumy +=y. Sumx +=x. y. Printf(“y= %fx + %f”. Sumxy=0. Sumsq=0. Sumxy +=x * y.096154 . Sumy=0.I. Printf(“enter the n value”).a1.h> #include<math. Sumsq += pow(x.980769x + 0.

sumy = 0. sumx3 = 0. Algorithm: Sep 1: Strart Step 2: Read n Step 3: Initialize sumx = 0.3) Sumx4 = sumx4 + pow(x. sumxy = 0.4) Sumy = sumy + y Sumxy = Sumxy + x*y Sumxsqy = Sumxsqy + pow(x.2) Sumx3 = sumx3 + pow(x. sumx4 = 0. sumxsq =0 Step 4: Intialize i=0 Step 5: Repeat steps 5 to 7 until i<n Step 6: Read x.2) *y Step 8: Increment I by 1 Step 9: Assign a[0][0] = n a[0][1] = n a[0][2] = n a[0][3] = n a[1][0] = n a[1][1] = n a[1][2] = n a[1][3] = n a[2][0] = n a[2][1] = n a[2][2] = n a[2][3] = n Step 10: Intialize i=0 Step 11: Repeat steps 11 to 15 until i<3 Step 12: Intialize j=0 Step 13: Repeat step 13 to 14 until j<=3 Step 14: Write a[i][j] Step 15: Increment j by 1 Step 16: Increment I by 1 Step 17: Initialize k =0 Step 18: Repeat steps 18 to 27 until k<=2 .AIM: Write C programs to implement polynomial regression algorithms. sumxsq = 0.y Step 7: Sumx = sumx + x Sumxsq =sumxsq + pow(x.

b[0] Stop . b[i] Increment I by 1 Write b[2].b[i].Step Step Step Step Step Step Step Step Step Step Step Step Step Step Step Step Step 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: Intialize i=0 Repeat step 20 to 26 until i<=2 If I not equal to k Asign u=a[i][k]/a[k][k] Intialize j=k Repeat steps 24 and 25 until j<=3 Assign a[i][j] = a[i][j] – u *a[k][j] Increment j by 1 Increment i by 1 Increment k by 1 Initialize I =0 Repeat steps 31 to 33 until i<3 Assign b[i] = a[i][3]/a[i][i] Write I.

Flowchart: .

sumxsqy.0. Sumxsqy = 0. &n). } A[0][0] = n. Sumx3 = 0. Sumy = 0.2). i++) { Scanf(“%f %f”. Sumxsq += pow(x.h> #include<math. Scanf(“%d”. b[20].h> main() { Int n. Sumx4 += pow(x. &x. A[0][3] = sumy. u=0. A[1][2] = sumx3. x.Program: #include<stdio. a[20][20]. Sumy +=y. A[0][2] = sumxsq. Sumxsqy += pow(x. Sumxy += x * y. A[1][3] = sumxy. I.3). Sumx4 = 0. A[2][1] = sumx3. j. i<n. k. Float sumx. sumxy.4). Sumx +=x. sumy. A[0][1] = sumx. Sumx3 += pow(x. For(i=0. A[2][2] = sumx4. Sumx = 0. &y). sumxsq. . A[1][0] = sumx.2) *y. Sumxsq = 0. A[1][1] = sumxsq. Sumxy = 0. Printf(“\n Enter the n value”). y. sumx4. A[2][0] = sumxsq. Float sumx3.

i<=2. i<3. b[i]). for(i=0. k++) { For(i=0. j++) Printf(“%10.4f”.i<3. I. j++) A[i][j]=a[i][j] – u * a[k][j].4 fx +%10. Printf(“\n”). j<=3. For(j = k. } Printf(“\n”).a[i][j]). j<=3.b[2]. k<=2. } Output: Enter the n value 10 -4 -3 -2 -1 0 1 2 3 4 21 12 4 1 2 7 15 30 45 .b[0]).b[i].i++) { If(i!=k) U=a[i][k]/a[k][k].4fx +10. Printf(“\nx[%d] = %f”.i++) { B[i] = a[i][3]/a[i][i]. i++) { for(j=0. } } For(i=0. Printf(“y= %10.2f”.A[2][3] = sumxsqy. } For(k=0.

SI = (f(x1) + f(x2))/2.996970 X[2] = 1.00 2. Stop ./i1/ Step 15.00 85.00 5. x=x+h Endfor Step 10.i0=i1 Step 13.9979x + 2. Step 4. I = h-si Step 5.67 10. h=x2-x1 Step 3. SI= SI + f(x) Step 9. Write I1.00 125. i1 = h.9848xsq + 5 5. h=h/2 { Note that the internal has been halved above and the number of points where the function has to be computed is doubled} Step 12.i Step 16.h. i=21 Step 11.00 X[0] = 2.00 125.00 85. x=x1 + h/2 Step 7.984848 Y=1.si Step 14.00 204.030303 X[1] = 2. for J= 1 to I do Step 8.00 3193. until / I1-i0 / <=c.00 1333. x2. Read x1. i=1 Repeat Step 6.00 85.0303 Week 24 AIM: Write C programs to implement Trapezoidal method. Algorithm: Step 1. e { x1 and x2 are the two end points of the internal the allowed error in integral is e} Step 2.00 513.

Flowchart: .

int i.b. printf("enter the values of x:").b. } printf("\n enter the values of y:").i<=n-1.i++) { x[i]=x[i-1]+h.b.i<=(n-1). x[0]=a.i++) { scanf("%f".integral.integral). } sum=sum+y[b]. printf("approximate integral value is: %f". for(i=0.h> main() { float h. for(i=1. printf("enter the value ofa. } h=(b-a)/n.h> #include<math. integral=sum*(h/2).i++) { scanf("%f".&x[i]). for(i=0. sum=sum+2*y[i].a.&y[i]).n:"). } .n 1 2 3 #include<stdio.i<=(n-1). getch().sum=0.y[20].&b.&n).Program: Output: Enter the values of a.n.x[20]. clrscr(). scanf("%f %f %f".&a.

s4=s4+f(x) Step 14. Write In. s2=s2+s4 {s2 stores already computed functional value and s4 the value computed in the new nitration } Step 10. s2=0 Step 6.h. (h/3) Step 19. In =(s+4s4).e Step 2. (h/3) Repeat Step 9.Enter the values of x: 1 2 3 Enter the values of y: 1 2 3 Approximate integral value is 2. h=(x2-x1)/2 Step 3. STOP . until |In-Io|≤e. h=h/2 Step 16. x=x+h Step 15. /in Step 20. s4=f(x1+h) Step 7. for j=1 to I do Step 13. AIM: Write C programs to implement Simpson method. io=in Step 18. Read x1. in= (s1+2s2+4s4) . s4=0 Step 11. i=2 Step 4. I0=0 Step 8. si=f(x1) + f(x2) Step 5.166667 . x=x1+h/2 Step 12. i=2i Step 17. Algorithm: Step 1.x2.i Step 21.

Flowchart: .

a=x[0]. } .&b.&a. if(i%2==0) { sum=sum+4*y[i].itgl).itgl.n.i++) { scanf("%f".sum=0. for(i=0. for(i=0.i++) { scanf("%f". } printf("\n enter the values of y"). int i. b=x[n]. printf("enter the values of x").&y[i]). } else { sum=sum+2*y[i]. clrscr(). scanf("%f%f%f".x[20].h> #include<conio. } } itgl=sum*(h/3). printf("integral value%f".a.b. printf("enter the values of a.i++) { x[i]=x[i]+h.i<=n.&x[i]).h> #include<math.b.n"). getch().i<=(n-2). } h=(b-a)/n.Program: #include<stdio. for(i=0.y[20].h> main() { float h.i<=n.&n).

b.555556 .n 1 2 3 Enter the value of x 4 5 6 7 Enter the values of y 8 9 1 2 Integral value is 5.Output: Enter the values of a.

e) Executing: Running the program the run time errors may occur during the execution of programs even though it is free from syntax errors. remaining all stages related its computer. The program always must guarantee to produce correct results. These errors can be know only after output is executed. problem specification: the program must be thoroughly understand the problemand the input output and special processing specification represent the most important information collection during this phase.e. f) Testing and validation: Once program is written . b) Using planning tools: The solution method is described step by step whensolution method had been outlined. SPARSE NOTATIONS FOR ALGORITHAMS: 1) General form of PROCEDURE Procedure NAME (parameter list) Declarations Statements end NAME 2) General form of FUNCTION . a program must be tested and then validated.C++. typing errors(syntax errors) are found quickly at the time of compiling the program most C implementations will generate diagnostic messages when syntax errors are detected during compilation. c) Coding: It is relatively easier to convert the algorithm into a program in a computer language i. Syntactic & Run time errors generally produce error messages whenprogram executed. C. It must be represent by using alorithmnotations or flow charts symbols. These are easy to find and can be corrected.c) are purely manual process. These are existence of logically incorrect instructions. Solution: The solution method is to be developed. The logical error which is very difficult to detect. d) Compiling: Translate the program into machine code. ALGORITHM: A method of representing the step by step logical procedure for solving program in natural language is algorithm.Viva-voice PROGRAM DEVELOPMENT STAGES: a) Program design: At this stage define a problem and its solution.In above stages a). b).

= .y.false 7) Logical operators: and . 5) Assignment of variables <variable>  <expression> 6) Boolean values: true . Boolean a. : cond :n : else : s n+1 end case 10) Iterative statements: a) while condition do srepeat b) loop s until condition repeat c) for vble <. <= .FLOWCHART: There is another way to write the steps involved in any process . char c. > 9) Conditional statement: a) if cond then s1 else s2 endif b) case : cond 1: s1 : cond 2 : s2 …………. The symbols form a diagram that represents the steps in a pictorial fashion similar to an algorithm. >=. d. This is also very easy to understand such a diagram is flow chart. not 8) Relational operators: < .this is by making use of various symbols . or .b. Output print(argument list). .start to finish by increment do s repeat 11) Input read (argument list). Function NAME (parameter list) Declarations Statements end NAME 3) For comment line: // This is comment// 4) Variable declaration : integer x .

The most common symbols using drawing flow charts are given below: FLOWCHART SYMBOLS: Oval Parallegram Rectangle Terminal Input/output Process Start/stop symbol Input/output Any processing can be performed Show’s data output in the form of document Decision Used to connect different parts of flowchart Sub program Iteration Joins two symbols Document Hard copy Diamond Decision Circle Double sided Rectangle Hexagon <---------------Arrow Connector Sub program Iteration Flow Special Symbols: . They are good communication devices and also helps in algorithm maintenance.Flowcharts help us understand the logic of an operation easily.

.

HOT KEYS IN TURBO 'C': F1 Provides context-sensitive help for the items selected. F2Save the current file to disk. . and places the cursor on the File option of the menu bar. F3 Window to enter file name to load. Pressing any key removesthe version copyright notice. STARTING TO TURBO 'C': Double clicking the mouse's left button on a Turbo C icon or By clicking start. selecting programs and then clicking Turbo C or Selecting MS-DOS prompt mode and change the directory to TC or Turbo C and then type TC at the DOS prompt and press Enter. F6Switch to active window.Turbo C opens and displays a main menu. F5 Toggle: Zoom or Unworn active window. F7Move to previous error.

F10 Go to the top line of main menu bar. Alt-XExit from the Turbo C to DOS/Windows desktop. b) Digits 0. ALT-F5 To view the results. a. Alt-EGo to Edit mode. Global declaration section: It declares Global variables which are useful in all functions of the program. c ……z. c) Special character: @.OBJ.C…….#. .1. b. Alt-FPull-down File menu. Ctrl-F9Run the current file.h> main() { int i=10. printf("%d". Alt-RPull-down Run menu (may Compile Link first).B.^…………. Alt-F9Compile current file to . Alt-OPull-down Options menu. Definition section: It defines all Symbolic constants. } Documentation section: It consists with name of the program and other details. Sub program section: It contains the entire user defined functions called in main function.Z.. 3)Special characters.i).2……. Character set: The Character set in C are grouped into 1(Letters. main( ) function section: It consists with declaration part and executable part. EscTo cancel the running command. a) Alphabets: A. Alt-D Pull-down Debug menu.F8Move to next error.$. Link section: It provides header files to the computer to link library functions.2)Digits.9.etc. C SYNTAX Structure of 'C' Program: Documentation section Link section Definition section Global declaration section main( ) function section { Declaration part Execution part } Sub program section Function 1 Function 2 ………… ex: /*name of the program*/ #include<stdio.

2. Ex: 149. 0 through 9 preceded by . It is also represented in exponential notation Ex: the value 4356. 'a' .35678e3.768 to 32.45.7e+308 Variables: It is a data name that may be used to store value.digits.78. 1) Integer constant: It refer to a sequence of digits.4e+38 -1. Variable name may consist of letters . 2) Real constants: the quantities which are represented by numbers containing fractional part. Ex: "india" .4e-38 to 3. +45.-980. Ex: '2' . "2*3".Key words: These are pre defined words have a fixed meanings.767 -128 to 128 -3. "n".78 can represented as 4. All key words must be written in lower case letters. underscore(_) characters subject to the following conditions: 1) Variable must begin with letter. Data Types: Data type int char float double size 2 bytes 1 byte 4 bytes 8 bytes range -32. 5) Back slash character constant(Escape sequences): '\n'for new line '\t'for horizontal tabulator '\v 'for vertical tabulator '\0'Null character. 4) String constant: It is a sequence of characters enclosed in double quotes. Ex:0. . Autobreakcasecharconst charcontinuedefaultdodouble elseenumexternfloatfor gotoifintlongregister returnshortsignedsizeofstatic structswitchtypedefunionunsigned voidvolatilewhile Constants: Constants in C refer to fixed values do not change during the execution of the program.or +. 3) Single character constant: It contains a character enclosed within a pair of single quote marks.7e-308 to1.

Ex : float marks. Ex: typedef int units. Declaration of storage classes: a) Automatic variable: Local variable known to only to the function in which is declared default is auto . Ex: extern int number. Syntax : volatile data-type variable. 3) Upper case and Lower case are significant. 5) White space is not allowed. b) Extern variables: Global variable known to all functions in the file. Ex: enum day { Monday. e) Declaring variable as volatile :The value of variable may be changed by some external reasons from out side. b) User defined type : C supports a feature known as type definition that allows user to define an identifier.. which exists and retains its value even after the control is transferred to the calling function. Ex : volatile int date . syntax :auto data-type variable.……Sunday}.variable n. d) Declaring variable as constant: The value of variable can be made to remain constant.. . Tuesday. syntax:const data-type variable = value.value n}. Ex : const int max = 40. Syntax :static data-type variable. Ex :static int x. 4) Variable name should not be key word. Syntax:extern data-type variable. c) Static variables: Local variable. It is used to crossing files. a) Primary type : Syntax : data-type variable1.….2) Variable name should not be more than 8 characters. Syntax :typedef data-type identifier. Ex : auto int number. c) Enumerated data type: It contains enumeration constants represented by identifiers.……. Declaration of variables: Declaration tells to the compiler variable name with specifying data type. Syntax: enum identifier { value 1.value 2.variable2.

<< (left shift) . Syntax: abstract typedef < integer . --var (pre).> . = operator c) Relational operators: These are used to compare two operands and then depend on their relation certain decision are made.5result = 7 Abstract data type: It is a tool which specifies the logical properties of a data type . integer > RATIONAL Defining symbolic constants: This is useful when a constant will be used number of places in a program. d) Increment operators: Used for incrementing by 1. if the operation are of different it types the lower type is converted to the higher type to before operation proceed. Type casting: C allows. < . Syntax : (type-name) expression/value. Syntax : data-type variable-name =constant.==. | (or) . var-. .. >= .!= .14159 Operators: a) Arithmetic operators: +. Assigning values to variables: Value can be assigned to variables using the assignment operator. which is stored in the CPU register. Ex :register int x. % b)Assignment operators: Use to assign result of expression to variable. local variables are more priority than global. Multiple assigning : Ex :x = y= z = max. / . Syntax :register data-type variable. # define symbolic-name value Ex : #define MAX 100 # define PI 3. ++var (pre) . Ex: int units =123. & (and). ** Very local variables are more priority than local. * . . The result must be in either TRUE or FALSE.d) Register variables: Local variable. Logical not operator changes TRUE to FALSE vise versa.(post) f) Bit operators: These are low level operators it works only on integers used for testing and shifting bits. ^(ones compliment) g) Logical operators :These operators are used test more than one condition.>>(right shift) .var++ (post) e) Decrement operators: Used for decrementing by 1. It is fast access variable. <= . Ex: x = (int) 7.

Input syntax: char variable = getchar(). Ex: printf ("%c".argN).&& (and) . Control flow structures: 1)Selective control structure(conditional structures): a) Simple if statement: syntax: if(condition) { statement-block.……&var n). numerical values. arg1. ! ( Logical not) f) comma operator: It is used to combine two related expressions. . Ex: scanf("%d". a) Formatted I/O Functions: Used to transfer single character.…………. } b) if-else statement syntax: if(condition) { statement-block.&var2.) Precedence of operators: Operatorpriority */%high +-low Note: evaluate from left to right Format descriptor: %dfor signed decimal integer %ufor unsigned decimal integer %sfor string %ffor float %cfor character Input / Output functions: These functions permit the transfer data between computer and the standard input/output devices. &x). Input syntax: scanf( "control string ". arg2. || (or) . Input syntax: gets(string). (. c) String I/O Functions: Used to transfer strings. Output syntax : puts(string). b)Character I/O Functions: Used to transfer single character. Output syntax: putchar(char variable). a). strings. Output syntax printf( "control string". & var1.

} else { statement-block. break. } if(condition 1) syntax: if(condition1) { if(condition 2) { statement-block. Syntax: switch (expression) { case value1: statement 1.block. } else { statement-block. } else { statement-block. .} else { statement. } c) nested if-else statement statement-block. } d) switch statement :It provides an alternative of several if statements. c) else if statement syntax: { } elseif(condition 2) { statement-block.

……………. break. a) while loop: syntax: while(expression) { statement: } b) do . } e) Ternary operator ( ? : ): It is used replace the statements in the form if-then-else syntax: conditional expression ? expression 1: expression 2 Ex: flag = (x<0) ? 0 : 1 Iterative control structure(Repetitive structures): These structures repeats sequence of instructions until condition satisfies. condition .while loop: The statements in the loop are executed and the testing done at the end of loop. if(condition) continue.case value2: statement 2. Default: statements. syntax: do { statement. } while(condition).. c) for loop: syntax: for ( initialization . } d) break statement: It transfer the control to the end of the construct. f) goto statement: It transfer control to the statement that . ……………. e) continue statement: It transfers the control to next iteration by skipping following statements. break. if(condition) break. increment) { statement.

Initialization : data-type array-name[row-size][column-size]={list of values}.. b) Two dimensional array : It is a list of items can be given a variable name using two subscripts. a) String declaration: syntax: char string-name[size].s2) . Pointers: It is nothing but a variable that contains the address of another variable. String: String is an array of characters terminated by the null character. Ex: char city[10]. String lengthint n=strlen(string).32 }. Initialization: Ex : char city[10]= "hyderabad". a) Multi dimensional array: syntax: data-type array-name [s 1][s 2][s3]……. Ex: int *p. (float pointer) .4.5}}. String comparisonint n= strcmp(s1.4. Ex:int number [5] = {1. b) String functions: String concatenationstrcat(s1. Ex: float table [4][5][2].[sn]. Syntax: data-type array-name [row-size][column-size]. Initialization: syntax: data-type array-name [array-size]={list of values}.{4.8.3.s2). if( condition) goto label Arrays: An array is a group of related data items that share a common name a) One dimensional array: It is a list of items can be given a variable name using only one subscript. syntax : data-type array-name [array-size].follows the label where it is defined. (integer pointer) char *p. Ex: int number[10].9}. Ex: int table[2][4]={ {1.s2).8.6.6. (character pointer) float*p.10. String copyingstrcpy(s1. Pointer&variable: Declaration of pointer variable: Syntax: data-type * pointer-variable. Ex: int table[2][4].

data-type member2. ………. Ex: void * e. Pointers &functions: Declaration of pointer to function.a. int(*p)(double). Pointers to pointer: Syntax : data-type ** pointer variable.. int*p. p=&a. p=test.list). Pointers &structures: Declaration of pointer to structures: Syntax : Struct tag { data-type member1. )ex : (*ptr). Ex: int a=10. .(link is a pointer to structure) }. e= &a. Structure with pointers: Ex: struct node { int data. p=&a. Pointers & arrays: Declaration of array of pointers: Syntax: data-type *variable[size]. Syntax: return-type data-type (*fptr)(fun. Ex : int test (double). struct node * link.arg. Accessing structure contents by pointer variable a) Dot operator ( . b) Arrow operator (->) ex: ptr-variable-> member1. Declaration of void pointer : Syntax: void * pointer-variable. Ex: char *ptr[5].member1.Initialization: Syntax: pointer-variable= &variable. Declaration of pointer to array: Ex: (int *p) [5]. }: struct tag * p.

executable statement1. Functions: It is meant for performing a specific task which is repeated each time the program calls it. Syntax: function-name(formal arg-list) argument declaration: { local variable declaration. arg v) argc(argument counter) : It is an integer variable which contains number of arguments. Actual arguments: These parameters used in calling function. ……………………. . Syntax: main(arg c. These must be variable. Structures: It is a collection of logically related data items of different types using a single name..}. } Function declaration(function call): Syntax: function-name (actual arguments). Command line Arguments: It is a parameter applied to a program when program is invoked. Functions are two types 1) Library functions 2) User defined functions. Structure allocates separate memory space for each member.int rollno: data-type variable 2. syntax: struct structure-nameEx: struct student {{ data-type variable 1.Structure definition: It creates a format of structure variables that may be used in structure. Formal arguments: These parameters used in called function. ……………… return(expression).float marks. }. argv(argument vector):It is an array of string. They may be constant or variable. it contains arguments passed from command line.…………. argv[0] always specifies name of the program. Function definition(called function): It introduces a new function by declaring type of value it returns and parameters.

Accessing structure contents: Syntax : Dot operator ( . Union save the memory space but it can handle only one member at a time. which is at.variable2……. sizeof(data-type)). Structure member. structure-member. b) File opening: Syntax: fp = fopen("filename". 67. Arrow operator(->) pointer to structure. Memory Allocation Functions: Memory allocation classified in to two categories. a) Allocating a block of memory: Syntax: ptr = (cast-type*) malloc(n* (sizeof(data-type)). c) Releasing the used space: Syntax: free(ptr). Structure initialization: ex : struct student s1={ 123 . d) Altering the size block: Syntax : ptr = realloc(ptr. b) Allocating multiple blocks of memory: Syntax: ptr=(cast-type*) calloc (n. Union declaration: syntax : union union-name variable.s2. .Structure declaration: It allocates a memory for declared structure. 2) Dynamic memory allocation which is at run time. syntax: struct structure-name variable1. ………………… }.remaining files are ASCII files a) Declaration of file pointer: Syntax: FILE *fp. 1) Static memory allocation. data-type member 2.mode). fread() files are binary files . Union: It permits several different data types to be stored in the same memory space on single name.89}. Union definition: Syntax: union union-name { data-type member 1. ex: struct student s1. ) structure-name . compiles time. File Handling Functions: File is a collection of similar type of records.s3. fwrite(). newsize).

AND. using C programming language under MS_DOS operating system.list).COM. Test status of file an error has been detected. .size of struct.offset.EXE."control string". .fp).DAT.DOC. fputc( character.no. fread( &structure.position). . hence the following DOS commands helps to use the operating system File: A file consists of group of characters.BAT Directory: A directory is nothing but group of files like: Ex: File namefile sizedate of creationtime of creation CMRIT.PAS. fputs(string.C. fwrite( &structure.c) File closing: if mode = r Open in reading mode. To reposition the pointer at the beginning of file rewind(fp). fprintf(fp. f) Records i/o with files: Used to perform i/o operations on binary files. To shift in current position on a file.list). of records. Syntax: fclose(fp).no. . d) Character i/o from files Syntax: fgetc(fp). fseek(fp. DOS CAMMANDS : The book is summarizes the main features and used of data structures. long int n=ftell(fp). = w Open in writing mode. h) Random access to files: To returns current offset position in the file.size of one record.fp).of records. ExtensionEx: TXT.fp). . A file consists of two parts: 1. f) Formatted i/o with files fscanf(fp. Syntax: ferror(fp). File nameEx: STAFF 2."control string". i) Error handling: Test end of file condition..fp). e) String i/o with files: syntax: fgets( string. Syntax: feof(fp).length.CPP.00AM . . .fp).EXE 2227-08-0312.

Internal Commands: These commands are generally loaded when an operating system itself is loaded into the memory.txt your.txt cmrcet. MEM etc.txt c:\tc COPY c: \my.Syntax: DATE DIR: This command displays contents in the directory. Ex: CLS. REN etc. DATE. A sub-directory is nothing but directory with in a DirectorySyntax: MD<Directory Name> . It will changes the filename. MS-DOS Prompt: C: \ MYDIR TIME: This command displays time of system and ask the user to change if he wants Syntax: TIME DATE: This command displays date of the system and asks the user to change if he wants. txt a: TYPE: Displays the content of the file Syntax: TYPE <FILE> Ex: TYPE AUTOEXEC. So these commands are stored in computer main memory. Ex: FORMAT.txt COPY my. External Commands 1.. Stored in Secondary memory like disk. Syntax: COPY <File1> <File2> COPY <File1><Directory Name> COPY < Disk1 > < Disk> Ex: COPY my. 2. Internal Commands 2.BAT REN: Renames one file to another name.DOS Commands: There are basically two types of commands: 1. TIME.txt CLS: Clears the screen Syntax: CLS Ex: CLS CD: Changes the directory Syntax: CD <Directory Name> Ex: CD CMRIT Changes from any directory to Parent directory CD\ Changes from any Directory to Root directory MD: Creates a new sub-directory. TYPE. DIR. Syntax: REN <File!> <File2> Ex: REN my. Syntax: DIR </Options> COPY : By using this command a user can copy various file into another file and also copy a group files from one directory to other directory and also copy a group files from one disk to the other disk. External Commands: These commands are stored in Disk Files.

Sign up to vote on this title
UsefulNot useful