Anda di halaman 1dari 205

COMPUTER PROGRAMMING LAB MANUAL

I B-TECH

VIDCOMPUTER SCIENCE AND ENGINEERINGIN

SVIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

INDEX
S.No. NAME OF THE PROGRAM PAGE No. 33 34 35 39 40 43 44 45 46 47 51 53 57 59 61 67 68 69 73 74 79
3

week 1.a) Sum of Digits of Given Number b) Fibonacci Series c) Prime Numers between 1 to N week 2. a) Evaluate the expression b) Roots of Quadratic equations week 3.a) Factorial of Given Number using Non-Recursive b) Factorial of Given Number using Recursive c) GCD of Two numbers using Non-Recursive d) GCD of Two numbers using Non-Recursive e) Towers of Hanoi Problem week 4.a) Total Distance Travelled by Vehicle b) Arithmetic Operations using Switch case week 5. a) Largest and Smallest number in a list of Integer b) Matrix Addition c) Matrix Multiplication week 6. a) Insert sub-string in main String b) Delete sub-string in main String c) Given String is Palindrome or not week 7 a) To Display portion of string if string in Main String b)To count no. of characters, words and lines in a Text week 8 a) Pascals Triangle
D KOTESWARARAO, Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Pyrmid of Numbers week 9 Geometric Regression b) To Convert Roman To decimal Number week 11 Complex Numbers b) To Reverse the first n Characters in a File week 13 a) To Display contents of a file b) To Merge two files into a third file week 14 Single Linked List b) Stack implements using Pointers week 16 a) Queue implements using Arrays b) Queue implements using Pointers week 17 a) Infix to Postfix Conversion b) Postfix Evaluation week 18 a) Bubble sort b) Selection sort week 19 a) Linear search using non recursive b) Linear search using recursive c) Binary search using Non-Recursive d) Binary search using Recursive week 20 week 21 Quick Sort Merge Sort week 15 a) Stack implements using Arrays week 12 a) To Copy one File into another File week 10 a) To Find 2s Complement of Given Number

80 83 87 88 93 99 100 103 104 107 117 121 129 133 141 144 148 150 155 156 157 159 163 167
4

D KOTESWARARAO, Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

week22 a) To implement the Lagrange Interpolation week 23 To implement the Linear Regression week 24 a) To implement Simpson Method b) To implement Tr apezoidal Method

171 177 181 187

b) To implement the Newton-Gregory Forward Interpolation 172

D KOTESWARARAO, Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

DOS COMMANDS

D KOTESWARARAO, Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

DOS COMMANDS: 1. Copy con: Definition: Syntax: Example: 2. Type: Definition: Syntax: Example: Definition: Syntax: Example: Definition: Syntax: Example: Definition: Syntax: Example: Definition: Syntax: Example: It is used to give contents of a file. C:\type <file name> C:\type ABC It is used to create a new Directory. C:\md <directory name> C:\md XYZ It is used to change the new Directory. C:\cd <directory name> C:\cd XYZ It is used to remove the existing directory. C:\rd <directory name> C:\rd XYZ This is used to display the list of all existing files, sub directory and directory on the specified storage device. C:\dir C:\dir
9

It is used to create a new file. C:\copy con <file name> C:\copy con ABC F6 (or) Ctrl +Z are used to save file.

3. MD (Make Directory):

4. CD (Change Directory):

5. RD (Remove Directory):

6. DIR (Disk Information Report) :

D KOTESWARARAO, Associate Professor, CSE Dept.

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

7. Date: Definition: Syntax: Example:

It is used to move or modify the current system date. C:\ date C:\date Current date is: Fri 10/23/2009 Enter the new date: (mm-dd-yy) It is used to view or modify the current system time. C:\time C:\time Current time is: 17:08:08.39 Enter the new time: (hh: mm: ss.ms) It is used to delete a file from the storage device. C:\del <file name> C:\del XYZ It is used to create a new file. C:\copy con <file name> C:\copy con ABC F6 (or) Ctrl +Z are used to save file. It is used to edit the existing file and modify the existing file. C:\edit <file name> C:\edit ABC It is used to copy the data from existing file to a new file. C:\copy <existing file name> <new file name> C:\copy ABC DEF It is used to existing file data will be removed to move to a new file. C:\move <existing file name> <new file name> C:\move ABC CBA F6 (or) Ctrl +Z are used to save file.

8. Time: Definition: Syntax: Example:

9. Del (Delete): Definition: Syntax: Example: 10. Del /P: Definition: Syntax: Example: 11. Edit : Definition: Syntax: Example: 12. Copy : Definition: Syntax: Example: 13. Move : Definition: Syntax: Example:

D KOTESWARARAO, Associate Professor, CSE Dept.

10

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

14. Cls: Definition: Syntax: Example: 15. Ren : Definition: Syntax: Example: 16. Ver : Definition: Syntax: Example: 17. help: Definition: Syntax: Example: It is used to clear the out-put screen. C:\cls C:\cls It is used to rename an existing file. C:\ren <old file name> <new file name> C:\ren ABC PSR It display the version of currently used operating system. C:\ver Microsoft Windows XP [Version 5.1.2600] It helps in giving complete information about specified command. C:\help <command name> C:\help copy

******************

D KOTESWARARAO, Associate Professor, CSE Dept.

11

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

12

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

INTRODUCTION
TO

C - LANGUAGE

D KOTESWARARAO, Associate Professor, CSE Dept.

13

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

14

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Introduction: C is a general purpose programming language C Language was developed by Dennis Ritchie. at AT & T Bell Laboratories of USA in 1972. C language was developed based on BCPL (Basic Combined Programming language) called B language Applications of C: C is used for writing
Operating systems Language Translators( Compiles, Interpreters and Assemblers) Editors Worksheets Database Management Systems games

Advantages of C:
1. C is robust language.

That means C language contains rich set of operators and built-in functions and several data types. 2. It is a middle-level language. That means it support both high level and low level language features so It is suitable for developing application software as well as system software. 3. C is a portable language. Portable means Programs written in C in one machine can be easily run on some other machine without any modifications. 4. Fast execution of programs when compared to other languages.

D KOTESWARARAO, Associate Professor, CSE Dept.

15

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

For example compare to BASIC if you want to increment I value 1 to 20000.It takes nearly 60sec but our C language takes only 1sec 5. It has the ability to deal efficiently with bits, bytes, addresses, etc. 6. it is easy to extends itself That means easily add the new features and develop the other programming languages 7. C language have only 32 keywords In C language have only 32 keywords but other languages have above 200 keywords like COBOL so easily remember the keywords to develop the programs 8. It is a structured language. Structured programs means large programs divide into sub programs that is called modules or functions, easy to understand, easy to debug, maintenance is very easy, parallel executions Loading, editing, saving and compiling a C-program: Now before entering the program into the computer, C-compiler should be loaded into the computers main memory. A Turbo-C compiler can be loaded by tying TC at DOS prompt in the subdirectory where Turbo-C compiler has been stored on the hard disk. For example, suppose you have Turbo-C compiler in the Turbo-C directory in the d disk drive, then first of all change the directory to Turbo-C and then type TC (because TC is an executable file having extension TC.EXE). This is called loading of Turbo-C compile into the main memory.

D KOTESWARARAO, Associate Professor, CSE Dept.

16

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Save: Compile: Run: Output: New Program: Open(Load):

F2 Alt+F9 Crtl+F9 Alt+F5 Alt+F and press N F3

How to enter C editor: The procedure can be done as described below:(Note: if your C stored in tc then type cd tc instead of cd turboc2)

D KOTESWARARAO, Associate Professor, CSE Dept.

17

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Whenever you type that command, you are immediately enter to the below editor:

D KOTESWARARAO, Associate Professor, CSE Dept.

18

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

GENERAL OR BASIC STRUCTURE OF A COMPLETE C-PROGRAM Documentation section Link Section Definition Section Global Declaration Section main () { Declaration section Executable section } Sub-program Section Function 1() { User defined program area; } Function 2() { Sub program or subroutine; }

D KOTESWARARAO, Associate Professor, CSE Dept.

19

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

20

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

SYNTAX:

LANGUGAE

D KOTESWARARAO, Associate Professor, CSE Dept.

21

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

22

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Syntax
1) Variable declaration: data_type variable1, variable2,variablen; 2) scanf() scanf(controlstring,&var1,&var2,.&varn); 3) printf() printf(controlstring,var1,var2,.varn); 4) CONDITIONAL CONTROL (OR BRANCHING) STATEMENTS: (a) (b) (c) (d) (e) if statement if-else statement nested if else else if ladder switch statement

a)if statement: The general form of the if statement is: if (expression) Statement; Or if (expression) statement; Or
D KOTESWARARAO, Associate Professor, CSE Dept. 23

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

if (expression) { Statement1; Statement2; Statement3; .. .. Statement-n; } b) if else statement:

the general form of the if else statement is if(expression) { Statement-block-1; } else { Statement-block-2; }

c)

nested if-else statement: The general form of the nested if-else statement is:

if(expression1) { if(expression2)
D KOTESWARARAO, Associate Professor, CSE Dept. 24

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

{ Statement-block-1; } else { Statement-block-2; } } else { Statement-block-3; } d) else if ladder statement: The general form of the else if ladder statement is: if (expression1) { Statement-1; Statement-2; } else if (expression2) { Statement-1; Statement-2; }
D KOTESWARARAO, Associate Professor, CSE Dept. 25

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

else if (expression3) { Statement-; Statement-2; } else { Statement-1 Statement-2 } e) switch statement: The general form of the switch statement is: switch (expression) { case constant1: Statement(s) Break; case constant2: Statement(s) Break; .. .. case constantn: constant(s)
D KOTESWARARAO, Associate Professor, CSE Dept. 26

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Break; default: Statement(s) }

5) LOOPING STATEMENTS: a) for loop b) while loop c) do-while loop a) for Loop: The general form of the for loop is: for (expression1; expression2; expression3) { Statement-1; Statement-2 Statement-3; } Components of for: The three expressions in the for statement are optional. However, even if an expression is not present, the associated semicolon should be present. For example: for ( ; test expression ; )
D KOTESWARARAO, Associate Professor, CSE Dept. 27

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

{ Statement-1; Statement-2; Statement-3; . . } b) while loop: while(expression) { Statement-block; } c) do- while loop do { Statement-block; } while(expression); 6) Break statement break;

D KOTESWARARAO, Associate Professor, CSE Dept.

28

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

7) Continue statement
continue;

8) goto statement
goto label; label: label: . . goto label; .. ..

6) Array variable declaration: data_type array_name[size]; 7) Function definition: return_type function_name(argument_list) { Body of the function; } 8) Function prototype: return_type function_name(argument_list);

D KOTESWARARAO, Associate Professor, CSE Dept.

29

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

9) Structure: struct tag_name { data_type member1; data_type member2; .. . data_type member1; } structure_variables; 10) Union: union tag_name { data_type member1; data_type member2; .. . data_type member1; `} union variables;

11) Pointer variable declaration: data_type *variable_name;

D KOTESWARARAO, Associate Professor, CSE Dept.

30

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-1

D KOTESWARARAO, Associate Professor, CSE Dept.

31

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

32

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a C program to find the sum of individual digits of a positive integer. #include<stdio.h> #include<conio.h> void main() { int n,r,sum=0; clrscr(); printf("Enter n value "); scanf("%d",&n); while(n!=0) { r=n%10; sum=sum+r; n=n/10; } printf("\nSum of Digits : %d",sum); getch(); }

OUTPUT: Enter n value 478 Sum of Digits : 19

D KOTESWARARAO, Associate Professor, CSE Dept.

33

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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. #include<stdio.h> #include<conio.h> void main() { int n,i,f0=0,f1=1,f2; clrscr(); printf("Enter n value "); scanf("%d",&n); printf("\nFibonacci Sequence up to %d terms :\n\n\n",n); printf("%4d%4d",f0,f1); for(i=3;i<=n;i++) { f2=f0+f1; f0=f1; f1=f2; printf("%4d",f2); } getch(); } OUTPUT: Enter n value 15 Fibonacci Sequence up to 15 terms : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

D KOTESWARARAO, Associate Professor, CSE Dept.

34

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

c) Write a C program to generate all the prime numbers between 1 and n, where n is a value supplied by the user. #include<stdio.h> #include<conio.h> void main() { int n,i,j,c=0; clrscr(); printf("Enter n value "); scanf("%d",&n); printf("\nPrime Numbers between 1 to %d \n\n\n",n); for(i=2;i<=n;i++) { c=0; for(j=2;j<=i/2;j++) { if(i%j==0) { c++; break; } } if(c==0) printf("%4d",i); } getch(); } OUTPUT: Enter n value 100 Prime Numbers between 1 to 100 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

D KOTESWARARAO, Associate Professor, CSE Dept.

35

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

36

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-2

D KOTESWARARAO, Associate Professor, CSE Dept.

37

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

38

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a C program to calculate the following Sum: Sum=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! #include<conio.h> #include<stdio.h> #include<math.h> void main() { float x,term,sum=1; int i, j; clrscr(); printf("Enter x value\n"); scanf("%f",&x); for(j=1,i=2;i<=10;i+=2,j++) { term=term*(x*x)/(i*(i-1)); sum=sum+term*pow(-1,j); } printf("\nSum of series : %f",sum); getch(); } OUTPUT: Enter x value 3 Sum of series : 1.000000

D KOTESWARARAO, Associate Professor, CSE Dept.

39

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a C program to find the roots of a quadratic equation. #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b,c,d,x1,x2,xr1,xr2,xi1,xi2; clrscr(); printf("enter a,b and c values"); scanf("%d%d%d",&a,&b,&c); d=b*b-4*a*c; if(d>0) { x1=(-b+sqrt(d))/2*a; x2=(-b-sqrt(d))/2*a; printf("roots are positive %d %d",x1,x2); } else if(d==0) { x1=x2=-b/2*a; printf(" roots are equals %d %d",x1,x2); } else { xr1=b/2*a; xr2=-b/2*a; xi1=sqrt(abs(d))/2*a; xi2=-sqrt(abs(d))/2*a; printf("roots are imaginary %d %d %d %d",xr1,xr2,xi1,xi2); } getch(); } OUTPUT: enter a,b and c values 1 -5 6 roots are positive 3 2

D KOTESWARARAO, Associate Professor, CSE Dept.

40

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-3

D KOTESWARARAO, Associate Professor, CSE Dept.

41

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

42

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a C program to find the factorial of a given integer using non recursive #include<stdio.h> #include<conio.h> long int fact(long int); void main() { long int n; clrscr(); printf("Enter n value "); scanf("%ld",&n); printf("\nFactorial of %ld is %ld",n,fact(n)); getch(); } long int fact(long int n) { long int f=1; while(n!=0) { f=f*n; n--; } return f; } OUTPUT: Enter n value 5 Factorial of 5 is 120

D KOTESWARARAO, Associate Professor, CSE Dept.

43

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a C program to find the factorial of a given integer using recursive #include<stdio.h> #include<conio.h> long int fact(long int); void main() { long int n; clrscr(); printf("Enter n value "); scanf("%ld",&n); printf("\nFactorial of %ld is %ld",n,fact(n)); getch(); } long int fact(long int n) { if(n==0) return 1; else return (n*fact(n-1)); } OUTPUT: Enter n value 6 Factorial of 6 is 720

D KOTESWARARAO, Associate Professor, CSE Dept.

44

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

c) Write a C program to find GCD of Two Numbers using non-recursive function #include<conio.h> #include<stdio.h> int gcd(int,int); void main() { int m,n; clrscr(); printf("Enter any Two Numbers"); scanf("%d%d",&m,&n); printf("GCD of the given two numbers %d",gcd(m,n)); getch(); } int gcd(int a,int b) { int r; r=a%b; while(r!=0) { a=b; b=r; r=a%b; } return b; } OUTPUT: Enter any Two Numbers 136 48 GCD of the given two numbers 8

D KOTESWARARAO, Associate Professor, CSE Dept.

45

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

d) Write a C program to find the GCD of two integers using recursive functions #include<conio.h> #include<stdio.h> int gcd(int,int); void main() { int m,n; clrscr(); printf("enter any two numbers"); scanf("%d%d",&m,&n); printf("gcd of the given two numbers %d",gcd(m,n)); getch(); } int gcd(int a,int b) { int r; r=a%b; if(r==0) return b; r=gcd(b,r); } OUTPUT: enter any two numbers 34 21 gcd of the given two numbers 1

D KOTESWARARAO, Associate Professor, CSE Dept.

46

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

e) Write a C program to solve Towers of Hanoi problem #include<stdio.h> #include<conio.h> void towers(int,char,char,char); void main() { int n; clrscr(); printf("enter n value"); scanf("%d",&n); towers(n,'A','B','C'); } void towers(int n,char frompeg,char topeg,char auxpeg) { if(n==1) { printf("\n%s%c%s%c","move disk 1 from peg ",frompeg," to peg ",topeg); return; } towers(n-1,frompeg,auxpeg,topeg); printf("\n%s%d%s%c%s%c","move disk ",n," from peg ",frompeg," to peg ",topeg); towers(n-1,auxpeg,topeg,frompeg); } OUTPUT: enter n value4 move disk 1 from peg A to peg C move disk 2 from peg A to peg B move disk 1 from peg C to peg B move disk 3 from peg A to peg C move disk 1 from peg B to peg A move disk 2 from peg B to peg C move disk 1 from peg A to peg C move disk 4 from peg A to peg B move disk 1 from peg C to peg B move disk 2 from peg C to peg A
D KOTESWARARAO, Associate Professor, CSE Dept. 47

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

move disk 1 from peg B to peg A move disk 3 from peg C to peg B move disk 1 from peg A to peg C move disk 2 from peg A to peg B move disk 1 from peg C to peg B

D KOTESWARARAO, Associate Professor, CSE Dept.

48

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-4

D KOTESWARARAO, Associate Professor, CSE Dept.

49

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

50

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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. #include <stdio.h> #include <math.h> void main() { int tim_intrval, counter,time; float accl, distance=0, velos; clrscr(); printf("<===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL===========>"); printf("\n\n\n\t\t\tNO OF TIME INTERVALS : "); scanf("%d",&tim_intrval); for(counter = 1; counter <= tim_intrval; counter++) { printf("\n\t\t\tAT T%d TIME(sec) : ",counter); scanf("%d",&time); printf("\t\t\tVELOCITY AT %d sec (m/sec) : ",time); scanf("%f",&velos); printf("\t\t\tACCLERATION AT %d sec (m/sec^2): ",time); scanf("%f",&accl); distance += (velos*time + (accl*pow(time,2))/2); } printf("\n\n\n\tTOTAL DISTANCE TRAVELLED BY VEHICLE IN %d INTERVALS OF TIME : %f",tim_intrval,distance); getch(); } OUTPUT: <===========PROGRAM FOR CALC TOTAL DISTANCE TRAVELED BY A VECHIAL===========> NO OF TIME INTERVALS : 3
D KOTESWARARAO, Associate Professor, CSE Dept. 51

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

AT T1 TIME(sec) : 45 VELOCITY AT 45 sec (m/sec) : 100 ACCLERATION AT 45 sec (m/sec^2): 25

AT T2 TIME(sec) : 50 VELOCITY AT 50 sec (m/sec) : 150 ACCLERATION AT 50 sec (m/sec^2): 50

AT T3 TIME(sec) : 60 VELOCITY AT 60 sec (m/sec) : 300 ACCLERATION AT 60 sec (m/sec^2): 70

TOTAL DISTANCE TRAVELLED BY VEHICLE IN 3 INTERVALS OF TIME : 243812.5000 00

D KOTESWARARAO, Associate Professor, CSE Dept.

52

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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) #include<stdio.h> #include<conio.h> void main() { int a,b; char ch; clrscr(); printf("enter a and b values"); scanf("%d%d",&a,&b); printf("enter the any operator(+,-,*,/)"); fflush(stdin); scanf("%c",&ch); switch(ch) { case '+': printf("Addition of two numbers : %d",a+b); break; case '-': printf("Subtraction of two numbers : %d",a-b); break; case '*': printf("Multiplication of two numbers : %d",a*b); break; case '/': printf("Division of two numbers :%f",(float)a/b); break; default: printf("invalid choice"); } getch(); } OUTPUT: enter a and b values15 3 enter the any operator(+,-,*,/)* Multiplication of two numbers : 45

D KOTESWARARAO, Associate Professor, CSE Dept.

53

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

54

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-5

D KOTESWARARAO, Associate Professor, CSE Dept.

55

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

56

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a C program to find both the larges and smallest number in a list of integers. #include<stdio.h> #include<conio.h> void main() { int a[20],i,n,max,min; clrscr(); printf("Enter n values "); scanf("%d",&n); printf("\nEnter %d elements ",n); for(i=0;i<n;i++) { printf("\nEnter %d position element : ",i); scanf("%d",&a[i]); } max=min=a[0]; for(i=1;i<n;i++) { if(max<a[i]) max=a[i]; else if(min>a[i]) min=a[i]; } printf("\n Maximum element is :%d",max); printf("\n Minimum element is :%d",min); getch(); } OUTPUT: Enter n values 6 Enter 6 elements Enter 0 position element : 23 Enter 1 position element : 67 Enter 2 position element : 45 Enter 3 position element : 34 Enter 4 position element : 13
D KOTESWARARAO, Associate Professor, CSE Dept. 57

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Enter 5 position element : 87 Maximum element is :87 Minimum element is :13

D KOTESWARARAO, Associate Professor, CSE Dept.

58

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a Program to do Matrix Addition using Function #include<stdio.h> #include<conio.h> void add(int [10][10],int [10][10],int,int); int i,j; void main() { int a[10][10],b[10][10],c[10][10],r1,c1,r2,c2,i,j,k; clrscr(); printf("enter first matrix order\n"); scanf("%d%d",&r1,&c1); printf("enter second matrix order\n"); scanf("%d%d",&r2,&c2); printf("enter First Matrix elements\n"); for(i=0;i<r1;i++) for(j=0;j<c1;j++) scanf("%d",&a[i][j]); printf("enter Second Matrix elements\n"); for(i=0;i<r2;i++) for(j=0;j<c2;j++) scanf("%d",&b[i][j]); if(r1==r2&&c1==c2) { printf("Addition of two matrix is\n"); add(a,b,r1,c1); } else printf("\nMatrix addition is not possible"); getch(); } void add(int a[10][10],int b[10][10],int r,int c) { for(i=0;i<r;i++) { for(j=0;j<c;j++) { printf("%4d",a[i][j]+b[i][j]); }
D KOTESWARARAO, Associate Professor, CSE Dept. 59

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

printf("\n"); } } OUTPUT: enter first matrix order 23 enter second matrix order 23 enter First Matrix elements 123 654 enter Second Matrix elements 678 325 Addition of two matrix is 7 9 11 9 7 9

D KOTESWARARAO, Associate Professor, CSE Dept.

60

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

c) Write a program to perform matrix multiplication #include<stdio.h> #include<conio.h> void read1(int **,int,int); void print(int**,int,int); void mult(int**,int**,int**,int,int,int); int i,j,k; void main() { int **a,**b,**c,r1,c1,r2,c2; clrscr(); printf("Enter First Matrix Order\n"); scanf("%d%d",&r1,&c1); printf("Enter Second Matrix Order\n"); scanf("%d%d",&r2,&c2); a=(int**)malloc(r1*c1*2); b=(int**)malloc(r2*c2*2); c=(int**)malloc(r1*c2*2); printf("Enter First Matrix Elements\n"); read1(a,r1,c1); printf("Enter Second Matrix Elements\n"); read1(b,r2,c2); mult(a,b,c,r1,c1,c2); printf("First Matrix\n"); print(a,r1,c1); printf("Second Matrix\n"); print(b,r2,c2); printf("Result\n"); print(c,r1,c2); getch(); } void read1(int **x,int r,int c) { for(i=0;i<r;i++) for(j=0;j<c;j++)
D KOTESWARARAO, Associate Professor, CSE Dept. 61

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

scanf("%d",(*(x+i)+j)); } void print(int **x,int r,int c) { for(i=0;i<r;i++) { for(j=0;j<c;j++) printf("%4d",*(*(x+i)+j)); printf("\n"); } } void mult(int **x,int **y,int **z,int r1,int c1,int c2) { for(i=0;i<r1;i++) { for(j=0;j<c2;j++) { *(*(z+i)+j)=0; for(k=0;k<c1;k++) *(*(z+i)+j)=*(*(z+i)+j)+(*(*(x+i)+k))*(*(*(y+k)+j)); } } } OUTPUT: Enter First Matrix Order 22 Enter Second Matrix Order 22 Enter First Matrix Elements 22 22 Enter Second Matrix Elements 22 22
D KOTESWARARAO, Associate Professor, CSE Dept. 62

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

First Matrix 2 2 2 2 Second Matrix 2 2 2 2 Result 8 8 8 8

D KOTESWARARAO, Associate Professor, CSE Dept.

63

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

64

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-6

D KOTESWARARAO, Associate Professor, CSE Dept.

65

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

66

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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. #include<stdio.h> #include<conio.h> void insert(char[100],char[50],int); void main() { char s[100],ss[50]; int p; clrscr(); printf("enter the main string\n"); gets(s); printf("enter the sub string\n"); scanf("%s",ss); printf("enter the position\n"); scanf("%d",&p); insert(s,ss,p); getch(); } void insert(char s[100],char ss[50],int p) { int i,j,k; char str[100]; for(i=0,j=0;i<p;i++,j++) str[j]=s[i]; for(i=0;ss[i]!='\0';i++,j++) str[j]=ss[i]; for(i=p;s[i]!='\0';i++,j++) str[j]=s[i]; str[j]='\0'; strcpy(s,str); printf("\nAfter insert main string is %s",s); }

OUTPUT:
enter the main string i you enter the sub string love enter the position 2 After insert main string is i love you
D KOTESWARARAO, Associate Professor, CSE Dept. 67

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) 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. #include<stdio.h> #include<conio.h> void delet(char[100],int,int); void main() { char s[100]; int p,n; clrscr(); printf("enter the main string\n"); gets(s); printf("enter the position\n"); scanf("%d",&p); printf("enter how many character we want delete\n"); scanf("%d",&n); delet(s,p,n); getch(); } void delet(char s[100],int p,int n) { int i,j; for(i=p,j=p+n;s[i]!='\0';i++,j++) s[i]=s[j]; s[i]='\0'; printf("After delete main string is %s",s); }

OUTPUT:
enter the main string koteswararao enter the position 7 enter how many character we want delete 2 After delete main string is koteswarao

D KOTESWARARAO, Associate Professor, CSE Dept.

68

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

c) Write a C program to determine if the given string is a palindrome or not #include<conio.h> #include<stdio.h> #include<string.h> void main() { char str1[20],str2[20]; int d; clrscr(); printf("Enter string\n"); scanf("%s",str1); strcpy(str2,str1); strrev(str2); if(!strcmp(str1,str2)) printf("\n String is palindrome "); else printf("\n String is not palindrome "); getch(); } OUTPUT: Enter string MADAM String is palindrome

D KOTESWARARAO, Associate Professor, CSE Dept.

69

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

70

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-7

D KOTESWARARAO, Associate Professor, CSE Dept.

71

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

72

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a C program that displays the position or index in the string S where the string T begins, or 1 if S doesnt contain T. #include<stdio.h> #include<conio.h> main() { char s[100],t[100],i,j,k; clrscr(); printf("enter the string\n"); gets(s); printf("enter the sub string\n"); scanf("%s",t); for(i=0;i<strlen(s);i++) { for(j=0,k=i;j<strlen(t);j++,k++) { if(s[k]==t[j]) continue; else break; } if(j==strlen(t)) { printf("string is found at %d",index+1); exit(0); } } printf("sub string not found in string"); getch(); } OUTPUT: enter the string COMPUTER PROGRAMMING AND DATA STRUCTURES enter the sub string DATA string is found at 26

D KOTESWARARAO, Associate Professor, CSE Dept.

73

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a C program to count the lines, words and characters in a given text. #include<stdio.h> #include<conio.h> void main() { char text[81],ch; int nwords=0,nlines=0,nchars=0,i; clrscr(); printf("enter text\n"); while(1) { i=0; while((ch=getchar())!='\n') { text[i]=ch; nchars++; if(ch==32) nwords++; i++; } nwords++; nlines++; text[i]='\0'; if(text[0]=='\0') break; } nwords--; nlines--; printf("\n no of characters : %d",nchars); printf("\n no of words : %d",nwords); printf("\n no of lines : %d",nlines); getch(); } OUTPUT: enter text KING IS GREAT WELCOME TO VVIT
D KOTESWARARAO, Associate Professor, CSE Dept. 74

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

no of characters : 28 no of words :6 no of lines : 2

D KOTESWARARAO, Associate Professor, CSE Dept.

75

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

76

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-8

D KOTESWARARAO, Associate Professor, CSE Dept.

77

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

78

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a C program to generate Pascals triangle. #include<stdio.h> #include<conio.h> void main() { int i,j,n,k; static int p[10][10]; clrscr(); printf("\n Pascals Triangle size n="); scanf("%d",&n); printf("\n Pascal's triangle:\n"); for(i=0;i<n;i++) { for(k=0;k<=30-i*2;k++) printf(" "); for(j=0;j<=i;j++) { if(j==0||i==j) p[i][j]=1; else p[i][j]=p[i-1][j-1]+p[i-1][j]; printf("%4d",p[i][j]); } printf("\n"); } getch(); } OUTPUT: Pascals Triangle size n=5 Pascal's triangle: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
D KOTESWARARAO, Associate Professor, CSE Dept. 79

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a C program to construct a pyramid of numbers. #include<stdio.h> #include<conio.h> void main() { int i,j,k,n; clrscr(); printf("Enter any number\n"); scanf("%d",&n); for(i=1;i<=n;i++) { for(k=1;k<20-2*i;k++) printf(" "); for(j=1;j<=i;j++) printf("%4d",i); printf("\n"); } getch(); } OUTPUT: Enter any number 6 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6

D KOTESWARARAO, Associate Professor, CSE Dept.

80

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-9

D KOTESWARARAO, Associate Professor, CSE Dept.

81

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

82

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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,> #include<stdio.h> #include<conio.h> #include<math.h> void main() { int x,n,sum=1,i; clrscr(); printf("enter x and n values\n"); scanf("%d%d",&x,&n); while(n<0) { printf("Exponent value is negative please enter x and n values"); scanf("%d%d",&x,&n); } for(i=1;i<=n;i++) sum=sum+pow(x,i); printf("Sum=%d",sum); } getch(); } OUTPUT: enter x and n values 35 Sum=364

D KOTESWARARAO, Associate Professor, CSE Dept.

83

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

84

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-10

D KOTESWARARAO, Associate Professor, CSE Dept.

85

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

86

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) 2s 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 2s complement of 11100 is 00100. Write a C program to find the 2s complement of a binary number. #include<stdio.h> #include<conio.h> void main() { char b[100]; int i,l; clrscr(); printf("enter the binary number\n"); scanf("%s",b); for(i=0;b[i]!='\0';i++) if(b[i]=='0'||b[i]=='1') continue; else { printf("the given number is not a binary number"); exit(0); } l=strlen(b); for(i=l-1;i>=0;i--) if(b[i]=='0') continue; else break; for(i=i-1;i>=0;i--) if(b[i]=='0') b[i]='1'; else b[i]='0'; printf(" The 2's complement is %s",b); getch(); } OUTPUT: enter the binary number10111010 The 2's complement is 01000110

D KOTESWARARAO, Associate Professor, CSE Dept.

87

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a C program to convert a Roman numeral to its decimal equivalent. #include<stdio.h> #include<conio.h> #include<string.h> #include<stdlib.h> void main() { int *a,len,i,j,k; char *rom; clrscr(); printf("Enter the Roman Numeral:"); scanf("%s",rom); len=strlen(rom); for(i=0;i<len;i++) { if(rom[i]=='I') a[i]=1; else if(rom[i]=='V') a[i]=5; else if(rom[i]=='X') a[i]=10; else if(rom[i]=='L') a[i]=50; else if(rom[i]=='C') a[i]=100; else if(rom[i]=='D') a[i]=500; else if(rom[i]=='M') a[i]=1000; else { printf("\nInvalid Value"); getch(); exit(0); } } k=a[len-1];
D KOTESWARARAO, Associate Professor, CSE Dept. 88

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

for(i=len-1;i>0;i--) { if(a[i]>a[i-1]) k=k-a[i-1]; else if(a[i]==a[i-1] || a[i]<a[i-1]) k=k+a[i-1]; } printf("\nIts Decimal Equivalent is:"); printf("%d",k); getch(); } OUTPUT: Enter the Roman Numeral:XII Its Decimal Equivalent is:12

D KOTESWARARAO, Associate Professor, CSE Dept.

89

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

90

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-11

D KOTESWARARAO, Associate Professor, CSE Dept.

91

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

92

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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.) #include<stdio.h> #include<conio.h> #include<math.h> typedef struct { float real,image; }complex; void print(complex); complex add(complex,complex); complex sub(complex,complex); complex mul(complex,complex); complex div(complex,complex); void main() { complex a,b,c; int ch; clrscr(); printf("enter first complex number\n"); scanf("%f%f",&a.real,&a.image); printf("enter second complex number\n"); scanf("%f%f",&b.real,&b.image); do { printf("\n 1.addition\n 2.subtraction\n 3.multiplication"); printf("\n 4.division\n 5.exit\n"); printf("enter your choice"); scanf("%d",&ch); printf("\nFirst Complex Number"); print(a); printf("Second Complex Number");
D KOTESWARARAO, Associate Professor, CSE Dept. 93

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

print(b); switch(ch) { case 1: printf("Addition of Two Complex Numbers"); c=add(a,b); break; case 2: printf("Subtraction of Two Complex Numbers"); c=sub(a,b); break; case 3: printf("Multiplication of Two Complex Numbers"); c=mul(a,b); break; case 4: printf("Division of Two Complex Numbers"); c=div(a,b); break; case 5: exit(0); } print(c); }while(ch<=5); getch(); } void print(complex x) { char sign='+'; if(x.image<0) { sign='-'; x.image=abs(x.image); } printf(" %0.2f%ci%0.2f\n",x.real,sign,x.image); } complex add(complex x,complex y) { complex temp; temp.real=x.real+y.real; temp.image=x.image+y.image;
D KOTESWARARAO, Associate Professor, CSE Dept. 94

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

return temp; } complex sub(complex x,complex y) { complex temp; temp.real=x.real-y.real; temp.image=x.image-y.image; return temp; } complex mul(complex x,complex y) { complex temp; temp.real=x.real*y.real-x.image*y.image; temp.image=x.real*y.image+x.image*y.real; return temp; } complex div(complex x,complex y) { complex temp; temp.real=(x.real*y.real+x.image*y.image)/(x.real*x.real+x.image*x.image); temp.image=(x.real*y.image+x.image*y.real)/(x.real*x.real+x.image*x.image); return temp; } OUTPUT: enter first complex number 34 enter second complex number 45 1.addition 2.subtraction 3.multiplication 4.division 5.exit
D KOTESWARARAO, Associate Professor, CSE Dept. 95

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

enter your choice1 First Complex Number 3.00+i4.00 Second Complex Number 4.00+i5.00 Addition of Two Complex Numbers 7.00+i9.00 1.addition 2.subtraction 3.multiplication 4.division 5.exit enter your choice5

D KOTESWARARAO, Associate Professor, CSE Dept.

96

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-12

D KOTESWARARAO, Associate Professor, CSE Dept.

97

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

98

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a C program which copies one file to another. #include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2; char ch; fp1=fopen("first.txt","r"); fp2=fopen("second.txt","w"); if(fp1==NULL) { printf("first.txt file unable to open"); exit(0); } while((ch=getc(fp1))!=EOF) putc(ch,fp2); fclose(fp1); fclose(fp2); }

D KOTESWARARAO, Associate Professor, CSE Dept.

99

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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.)*/ #include <stdio.h> #include <conio.h> #include <string.h> #include <process.h> void main(int argc, char *argv[]) { char a[15]; char s[20]; char n; int k; int j=0; int i; int len; FILE *fp; if(argc!=3) { puts("Improper number of arguments."); exit(0); } fp = fopen(argv[1],"r"); if(fp == NULL) { puts("File cannot be opened."); exit(0); } k=*argv[2]-48; n = fread(a,1,k,fp); a[n]='\0'; len=strlen(a); for(i=len-1;i>=0;i--) { s[j]=a[i]; printf("%c",s[j]); j=j+1; } s[j+1]='\0'; getch(); }
D KOTESWARARAO, Associate Professor, CSE Dept. 100

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-13

D KOTESWARARAO, Associate Professor, CSE Dept.

101

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

102

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a c Programme to display the contents of a file


#include<stdio.h> #include<conio.h> void main() { FILE *fp; char ch; clrscr(); fp=fopen("sample.txt","r"); if(fp==NULL) { printf("first.txt file unable to open"); exit(0); } while((ch=getc(fp))!=EOF) putchar(ch); fclose(fp); getch(); }

D KOTESWARARAO, Associate Professor, CSE Dept.

103

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a c Programme to merge two files into a third file(i.e. the contents of the first file followed by those of the second are put in the third file)
#include<stdio.h> #include<conio.h> void main() { FILE *fp1,*fp2,*fp3; char ch; fp1=fopen("first.txt","r"); fp3=fopen("third.txt","w"); while((ch=getc(fp1))!=EOF) putc(ch,fp3); fclose(fp1); fclose(fp3); fp2=fopen("second.txt","r"); fp3=fopen("third.txt","a"); while((ch=getc(fp2))!=EOF) putc(ch,fp3); fclose(fp2); fclose(fp3); }

D KOTESWARARAO, Associate Professor, CSE Dept.

104

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-14

D KOTESWARARAO, Associate Professor, CSE Dept.

105

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

106

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Write a C program that uses functions to perform the following operations on singly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal #include<stdio.h> #include<conio.h> struct node { int info; struct node *next; }; typedef struct node *NODEPTR; NODEPTR p; NODEPTR getnode(); void freenode(NODEPTR p); void ins_start(NODEPTR p,int x); void ins_end(NODEPTR p,int x); void ins_middle(NODEPTR p,int x); int del_start(NODEPTR p); int del_middle(NODEPTR p); int del_end(NODEPTR p); void print(NODEPTR p); void main() { int ch,x; p=getnode(); p->next=NULL; clrscr(); do { printf("\n1.ins_start\n2.ins_middle\n3.ins_end"); printf("\n4.del_start\n5.del_middle\n6.del_end"); printf("\n7.print\n8.exit\n"); printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("enter inserted element"); scanf("%d",&x); ins_start(p,x); break; case 2: printf("enter inserted element");
D KOTESWARARAO, Associate Professor, CSE Dept. 107

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

scanf("%d",&x); ins_middle(p,x); break; case 3: printf("enter inserted element"); scanf("%d",&x); ins_end(p,x); break; case 4: x=del_start(p); printf("deleted element from list %d",x); break; case 5: x=del_middle(p); printf("deleted element from list %d",x); break; case 6: x=del_end(p); printf("deleted element from list %d",x); break; case 7: print(p); break; case 8: exit(0); } }while(ch<=8); getch(); } NODEPTR getnode() { NODEPTR p; p=(NODEPTR)malloc(sizeof(struct node)); return (p); } void freenode(NODEPTR p) { free(p); } void ins_start(NODEPTR p,int x) { NODEPTR q; q=getnode(); q->info=x; if(p->next==NULL) q->next=NULL; else
D KOTESWARARAO, Associate Professor, CSE Dept. 108

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

q->next=p->next; p->next=q; }

void ins_middle(NODEPTR p,int x) { NODEPTR q; int s,i=1; printf("enter insert position"); scanf("%d",&s); while(p!=NULL && i<=s) { p=p->next; i++; } if(p->next==NULL) { printf("INSERTION IS NOT POSSIBLE"); exit(0); } q=getnode(); q->info=x; q->next=p->next; p->next=q; } void ins_end(NODEPTR p,int x) { NODEPTR q; q=getnode(); q->info=x; if(p->next==NULL) q->next=NULL; else { while(p->next!=NULL) p=p->next; q->next=NULL; } p->next=q; } int del_start(NODEPTR p)
D KOTESWARARAO, Associate Professor, CSE Dept. 109

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

{ int x; NODEPTR q; if(p->next==NULL) { printf("deletion is not possible"); exit(0); } q=p->next; x=q->info; p->next=q->next; freenode(q); return x; } int del_middle(NODEPTR p) { int x,i=1,n; NODEPTR q; printf("enter position of the deleted element"); scanf("%d",&n); while(p!=NULL && i<n) { p=p->next; i++; } if(p->next==NULL) { printf("deletion is not possible"); exit(0); } q=p->next; x=q->info; p->next=q->next; freenode(q); return x; } int del_end(NODEPTR p) { int x; NODEPTR q; if(p->next==NULL) {
D KOTESWARARAO, Associate Professor, CSE Dept. 110

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

printf("deletion is not possible"); exit(0); } while((p->next)->next!=NULL) p=p->next; q=p->next; x=q->info; p->next=NULL; freenode(q); return x; } void print(NODEPTR p) { NODEPTR q; q=p; if(p->next==NULL) { printf("LIST IS EMPTY"); exit(0); } q=q->next; while(q!=NULL) { printf("%2d->",q->info); q=q->next; } printf("NULL"); } OUTPUT: 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice1 enter inserted element12 1.ins_start 2.ins_middle
D KOTESWARARAO, Associate Professor, CSE Dept. 111

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice1 enter inserted element23 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice1 enter inserted element45 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice7 45->23->12->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice3 enter inserted element78 1.ins_start 2.ins_middle 3.ins_end
D KOTESWARARAO, Associate Professor, CSE Dept. 112

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice7 45->23->12->78->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice5 enter position of the deleted element2 deleted element from list 23 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice7 45->12->78->NULL 1.ins_start 2.ins_middle 3.ins_end 4.del_start 5.del_middle 6.del_end 7.print 8.exit enter your choice8

D KOTESWARARAO, Associate Professor, CSE Dept.

113

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

114

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-15

D KOTESWARARAO, Associate Professor, CSE Dept.

115

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

116

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write C programs that implement stack (its operations) using i) Arrays #include<stdio.h> #include<conio.h> #define MAXSIZE 100 struct stack { int top; int item[MAXSIZE]; }; struct stack *s; void push(struct stack *s,int x); int pop(struct stack *s); void print(struct stack *s); int empty(struct stack *s); void main() { int op,x; s->top=-1; clrscr(); do { printf(" \n 1.push\n 2.pop \n 3.print \n 4.exit\n"); printf("enter your option"); scanf("%d",&op); switch(op) { case 1: printf(" enter element\n"); scanf("%d",&x); push(s,x); break; case 2: if(!empty(s)) { x=pop(s); printf("\nthe retrived elemenet from stack is %d",x); }
D KOTESWARARAO, Associate Professor, CSE Dept. 117

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

else printf("stack is underflow"); break; case 3: print(s); break; case 4: exit(0); } } while(op<=4); getch(); } void push(struct stack *s,int n) { if(s->top==MAXSIZE-1) printf("stack is full"); else { s->top++; s->item[s->top]=n; } } int pop(struct stack *s) { return s->item[s->top--]; } void print(struct stack *s) { int i; if(!empty(s)) { printf("stack elements"); for(i=s->top;i>=0;i--) { printf("\n ----\n"); printf("| %d |",s->item[i]); }
D KOTESWARARAO, Associate Professor, CSE Dept. 118

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

printf("\n ----"); } else printf("stack is empty"); } int empty(struct stack *s) { if(s->top==-1) return 1; else return 0; } OUTPUT: 1.push 2.pop 3.print 4.exit enter your option1 enter element 15 1.push 2.pop 3.print 4.exit enter your option1 enter element 35 1.push 2.pop 3.print 4.exit enter your option3 stack elements ---| 35 |
D KOTESWARARAO, Associate Professor, CSE Dept. 119

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

---| 15 | ---1.push 2.pop 3.print 4.exit enter your option1 enter element 50 1.push 2.pop 3.print 4.exit enter your option3 stack elements ---| 50 | ---| 35 | ---| 15 | ---1.push 2.pop 3.print 4.exit enter your option2 the retrived elemenet from stack is 50 1.push 2.pop 3.print 4.exit enter your option4

D KOTESWARARAO, Associate Professor, CSE Dept.

120

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write C programs that implement stack (its operations) using ii) Pointers #include<stdio.h> #include<conio.h> struct node { int info; struct node *next; }; typedef struct node *NODEPTR; struct stack { NODEPTR top; }; typedef struct stack *stack; stack s; NODEPTR getnode(); void freenode(NODEPTR p); void push(stack s,int x); int pop(stack s); int empty(stack s); void print(stack s); void main() { int ch,x; s->top=NULL; clrscr(); do { printf("\n1.PUSH\n2.POP\n3.PRINT\n4.EXIT\n"); printf("enter your choice"); scanf("%d",&ch);
D KOTESWARARAO, Associate Professor, CSE Dept. 121

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

switch(ch) { case 1: printf("enter inserted element"); scanf("%d",&x); push(s,x); break; case 2: x=pop(s); printf("poped element from list %d",x); break; case 3: print(s); break; case 4: exit(0); } }while(ch<=4); getch(); } NODEPTR getnode() { NODEPTR p; p=(NODEPTR)malloc(sizeof(struct node)); return (p); } void freenode(NODEPTR p) { free(p); } int empty(stack s) { if(s->top==NULL) return 1; else return 0; } void push(stack s,int x) { NODEPTR q;
D KOTESWARARAO, Associate Professor, CSE Dept. 122

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

q=getnode(); q->info=x; if(s->top==NULL) { q->next=NULL; s->top=q; } else { q->next=s->top; s->top=q; } }

int pop(stack s) { int x; NODEPTR q; if(empty(s)) { printf("stack is underflow"); exit(0); } q=s->top; x=q->info; s->top=q->next; freenode(q); return x; } void print(stack s) { NODEPTR q; q=s->top; if(s->top==NULL) {
D KOTESWARARAO, Associate Professor, CSE Dept. 123

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

printf("STACK IS EMPTY"); exit(0); } printf(" ----\n"); printf("|NULL|\n"); while(q!=NULL) { printf(" ----\n"); printf("| %d |\n",q->info); q=q->next; } printf(" ----"); } OUTPUT: 1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice1 enter inserted element20 1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice1 enter inserted element30 1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice3 ---|NULL| ---| 30 |
D KOTESWARARAO, Associate Professor, CSE Dept. 124

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

---| 20 | ---1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice2 poped element from list 30 1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice3 ---|NULL| ---| 20 | ---1.PUSH 2.POP 3.PRINT 4.EXIT enter your choice4

D KOTESWARARAO, Associate Professor, CSE Dept.

125

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

126

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-16

D KOTESWARARAO, Associate Professor, CSE Dept.

127

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

128

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write C programs that implement Queue (its operations) using i) Arrays #include<stdio.h> #include<conio.h> #define MAX 100 struct queue { int front,rear; int item[MAX]; }; struct queue *q; void insert(struct queue *q,int x); int delete(struct queue *q); void print(struct queue *q); int empty(struct queue *q); void main() { int ch,x; clrscr(); q->front=0; q->rear=-1; do { printf("\n 1.insert \n 2.remove \n 3.print \n 4.exit \n"); printf("enter your choice"); scanf("%d",&ch); switch(ch) { case 1: printf("enter element\n"); scanf("%d",&x); insert(q,x); break; case 2: if(!empty(q)) { x=delete(q); printf("deleted element from queue is %d",x); }
D KOTESWARARAO, Associate Professor, CSE Dept. 129

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

else { printf("queue is underflow\n"); getch(); } break; case 3: if(!empty(q)) print(q); else printf("queue is empty"); break; case 4: exit(0); default : printf("invalid choice"); getch(); } }while(ch<=4); } int empty(struct queue *q) { if(q->rear<q->front) return 1; else return 0; } void insert(struct queue *q,int x) { if(q->rear==MAX-1) { printf("queue is overflow\n"); getch(); } else { q->rear++; q->item[q->rear]=x; }
D KOTESWARARAO, Associate Professor, CSE Dept. 130

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

} int delete(struct queue *q) { return q->item[q->front++]; } void print (struct queue *q) { int i; for(i=q->front;i<=q->rear;i++) printf("%4d",q->item[i]); } OUTPUT: 1.insert 2.remove 3.print 4.exit enter your choice1 enter element 35 1.insert 2.remove 3.print 4.exit enter your choice1 enter element 67 1.insert 2.remove 3.print 4.exit enter your choice1 enter element 12
D KOTESWARARAO, Associate Professor, CSE Dept. 131

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

1.insert 2.remove 3.print 4.exit enter your choice3 35 67 12 1.insert 2.remove 3.print 4.exit enter your choice2 deleted element from queue is 35 1.insert 2.remove 3.print 4.exit enter your choice3 67 12 1.insert 2.remove 3.print 4.exit enter your choice1 enter element 45 1.insert 2.remove 3.print 4.exit enter your choice3 67 12 45 1.insert 2.remove 3.print 4.exit enter your choice4
D KOTESWARARAO, Associate Professor, CSE Dept. 132

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write C programs that implement Queue (its operations) using ii) Pointers #include<stdio.h> #include<conio.h> struct node { int info; struct node *next; }; typedef struct node *NODEPTR; struct queue { NODEPTR front,rear; }; struct queue *pq; NODEPTR getnode(); void freenode(NODEPTR p); void insert(struct queue *pq,int x); int delete(struct queue *pq); int empty(struct queue *pq); void print(struct queue *pq); void main() { int ch,x; pq->rear=pq->front=NULL; clrscr(); do { printf("\n1.insert\n2.delete\n3.print\n4.exit\n"); printf("enter your choice"); scanf("%d",&ch); switch(ch)
D KOTESWARARAO, Associate Professor, CSE Dept. 133

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

{ case 1: printf("enter inserted element"); scanf("%d",&x); insert(pq,x); break; case 2: x=delete(pq); printf("deleted element from list %d",x); break; case 3: print(pq); break; case 4: exit(0); } }while(ch<=4); getch(); } NODEPTR getnode() { NODEPTR p; p=(NODEPTR)malloc(sizeof(struct node)); return (p); } void freenode(NODEPTR p) { free(p); }

void insert(struct queue *pq,int x) { NODEPTR p; p=getnode(); p->info=x; p->next=NULL; if(pq->rear==NULL) pq->front=p;
D KOTESWARARAO, Associate Professor, CSE Dept. 134

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

else (pq->rear)->next=p; pq->rear=p; } int delete(struct queue *pq) { int x; NODEPTR p; if(empty(pq)) { printf("queue is underflow"); exit(0); } p=pq->front; x=p->info; pq->front=p->next; if(pq->front==NULL) pq->rear=NULL; freenode(p); return x; } int empty(struct queue *pq) { if(pq->front==NULL) return 1; else return 0; }

void print(struct queue *pq) { NODEPTR q; q=pq->front; if(pq->front==NULL) { printf("QUEUE IS EMPTY");


D KOTESWARARAO, Associate Professor, CSE Dept. 135

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

exit(0); } while(q!=NULL) { printf("%2d->",q->info); q=q->next; } printf("NULL"); } OUTPUT: 1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element10 1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element20 1.insert 2.delete 3.print 4.exit enter your choice3 10->20->NULL 1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element45
D KOTESWARARAO, Associate Professor, CSE Dept. 136

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

1.insert 2.delete 3.print 4.exit enter your choice3 10->20->45->NULL 1.insert 2.delete 3.print 4.exit enter your choice2 deleted element from list 10 1.insert 2.delete 3.print 4.exit enter your choice3 20->45->NULL 1.insert 2.delete 3.print 4.exit enter your choice1 enter inserted element50 1.insert 2.delete 3.print 4.exit enter your choice3 20->45->50->NULL 1.insert 2.delete 3.print 4.exit enter your choice4

D KOTESWARARAO, Associate Professor, CSE Dept.

137

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

138

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-17

D KOTESWARARAO, Associate Professor, CSE Dept.

139

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

140

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write a C program that uses Stack operations to perform the following: i) Converting infix expression into postfix expression #include<stdio.h> #include<conio.h> #include<ctype.h> #define MAX 100 struct stack { int top; int item[MAX]; }; struct stack *s; char postfix[MAX]; void push(struct stack *s,char a); int pop(struct stack *s); int prec(char op); void intopost(char infix[MAX]); void main() { char infix[MAX]; s->top=-1; clrscr(); printf("enter the infix expression"); gets(infix); intopost(infix); printf("the postfix expression is %s",postfix); getch(); } void intopost(char infix[MAX]) { int i=0,j=0; while(infix[i]!='\0')
D KOTESWARARAO, Associate Professor, CSE Dept. 141

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

{ if(isalpha(infix[i])) postfix[j++]=infix[i]; else { switch(infix[i]) { case '$': case '*': case '/': case '+': case '-': while((s->top!=-1) &&prec(s->item[s->top])>=prec(infix[i])) postfix[j++]=pop(s); push(s,infix[i]); break; case '(':push(s,'('); break; case ')': while(s->item[s->top]!='(') postfix[j++]=pop(s); pop(s); break; default : printf("invalid expression"); getch(); exit(0); } } i++; } while(s->top!=-1) postfix[j++]=pop(s); } int prec(char op) { switch(op) {
D KOTESWARARAO, Associate Professor, CSE Dept. 142

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

case '+': case '-': return (10); case '*': case '/': return (20); case '$': return (30); case '(': return (2); } } void push(struct stack *s,char a) { if(s->top==MAX-1) printf("stack is full"); else s->item[++s->top]=a; } int pop(struct stack *s) { if(s->top==-1) { printf("stack is underflow"); exit(0); } else return (s->item[s->top--]); } OUTPUT: enter the infix expressionA*(B+C)-D the postfix expression is ABC+*D-

D KOTESWARARAO, Associate Professor, CSE Dept.

143

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write a C program that uses Stack operations to perform the following: ii) Evaluating the postfix expression #include<stdio.h> #include<conio.h> #include<math.h> #define MAXCOLS 100 struct stack { int top; int item[MAXCOLS]; }; double eval(char[]); double pop(struct stack *s); void push(struct stack *s,double n); int empty(struct stack *s); int isdigit(char); double oper(int,double,double); void main() { char expr[MAXCOLS]; int position=0; clrscr(); printf("enter postfix expression\n"); while((expr[position++]=getchar())!='\n'); expr[--position]='\0'; printf("\nthe original postfix expression ] %s\n",expr); printf("the result of postfix expression %f",eval(expr)); getch(); } double eval(char expr[]) { int c,position; double opnd1,opnd2,value; struct stack *opndstk; opndstk=(struct stack *)malloc(sizeof(struct stack)); opndstk->top=-1;
D KOTESWARARAO, Associate Professor, CSE Dept. 144

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

for(position=0;(c=expr[position])!='\0';position++) if(isdigit(c)) push(opndstk,(double)(c-'0')); else { opnd2=pop(opndstk); opnd1=pop(opndstk); value=oper(c,opnd1,opnd2); push(opndstk,value); } return (pop(opndstk)); } isdigit(char symb) { return (symb>='0' && symb<='9'); } double oper(int symb,double op1,double op2) { switch(symb) { case '+': return(op1+op2); case '-': return(op1-op2); case '*': return(op1*op2); case '/': return(op1/op2); case '$': return(pow(op1,op2)); default: printf("\nillegal operation "); exit(0); } } void push(struct stack *s,double n) { if(s->top==MAXCOLS-1) printf("stack is overflow"); else {
D KOTESWARARAO, Associate Professor, CSE Dept. 145

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

s->top++; s->item[s->top]=n; } } double pop(struct stack *s) { if(!empty(s)) return (s->item[s->top--]); else { printf("etack is underflow"); exit(0); } } int empty(struct stack *s) { if(s->top==-1) return 1; else return 0; } OUTPUT: enter postfix expression 345+* the original postfix expression 345+* the result of postfix expression 27.000000

D KOTESWARARAO, Associate Professor, CSE Dept.

146

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-18

D KOTESWARARAO, Associate Professor, CSE Dept.

147

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) write a c program that implements the following sorting methods to sort a given list of integers in ascending order. i) Bubble sort #include<stdio.h> #include<conio.h> void bubble(int[],int); void main() { int a[10],i,n; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); bubble(a,n); getch(); } void bubble(int x[10],int n) { int temp,flag=1,i,j; for(i=0;i<n-1&&flag==1;i++) { flag=0; for(j=0;j<n-i-1;j++) { if(x[j]>x[j+1]) { flag=1; temp=x[j]; x[j]=x[j+1]; x[j+1]=temp; } } } printf("sorted elements\n"); for(i=0;i<n;i++)
D KOTESWARARAO, Associate Professor, CSE Dept. 148

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

printf("%4d",x[i]); } OUTPUT: Enter the n value 7 Enter 7 elements 34 89 76 21 10 54 41 sorted elements 10 21 34 41 54 76 89

D KOTESWARARAO, Associate Professor, CSE Dept.

149

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) write a c program that implements the following sorting methods to sort a given list of integers in ascending order. ii) Selection sort
#include<stdio.h> #include<conio.h> void selection(int[],int); void main() { int a[10],i,n; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); selection(a,n); printf("sorted elements\n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); } void selection(int x[10],int n) { int i,index,j,large; for(i=n-1;i>0;i--) { large=x[0]; index=0; for(j=1;j<=i;j++) { if(x[j]>large) { large=x[j]; index=j; } } x[index]=x[i]; D KOTESWARARAO, Associate Professor, CSE Dept. 150

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

x[i]=large; } }

OUTPUT: Enter the n value 5 Enter 5 elements 43 98 67 21 54 sorted elements 21 43 54 67 98

D KOTESWARARAO, Associate Professor, CSE Dept.

151

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

152

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-19

D KOTESWARARAO, Associate Professor, CSE Dept.

153

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

154

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write C program to implement linear search using non recursive function.


#include<stdio.h> #include<conio.h> int lsearch(int[],int,int); void main() { int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter search element"); scanf("%d",&s); key=lsearch(a,n,s); if(key!=-1) printf("Element found at %d position ",key+1); else printf("Element not found "); getch(); } int lsearch(int x[10],int n,int s) { int i; for(i=0;i<n;i++) if(s==x[i]) return i; return -1; } OUTPUT: Enter the n value 5 Enter 5 elements 23 56 73 21 34 Enter search element73 Element found at 3 position

D KOTESWARARAO, Associate Professor, CSE Dept.

155

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write C program to implement linear search using recursive function.


#include<stdio.h> #include<conio.h> int lsearch(int[],int,int); void main() { int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter search element"); scanf("%d",&s); key=lsearch(a,n,s); if(key!=-1) printf("Element found at %d position ",key+1); else printf("Element not found "); getch(); } int lsearch(int x[10],int n,int s) { if(n==-1) return -1; else if(x[n]==s) return n; else lsearch(x,n-1,s); } OUTPUT: Enter the n value 5 Enter 5 elements 23 56 73 21 34 Enter search element73 Element found at 3 position

D KOTESWARARAO, Associate Professor, CSE Dept.

156

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

c) Write C program to implement Binary Search using Non-recursive function


#include<stdio.h> #include<conio.h> int bsearch(int[],int,int); void main() { int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("enter %d elements in sorted order",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter search element"); scanf("%d",&s); key=bsearch(a,n,s); if(key!=-1) printf("element found at %d position ",key+1); else printf("element not found "); getch(); } int bsearch(int x[10],int n,int s) { int low,high,mid; low=0; high=n-1; while(low<=high) { mid=(low+high)/2; if(s==x[mid]) return mid; else if(s<x[mid]) high=mid-1; else low=mid+1;
D KOTESWARARAO, Associate Professor, CSE Dept. 157

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

} return -1; } OUTPUT: Enter the n value 5 enter 5 elements in sorted order 34 45 68 69 76 enter search element69 element found at 4 position

D KOTESWARARAO, Associate Professor, CSE Dept.

158

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

d) Write C program to implement Binary Search using recursive function


#include<stdio.h> #include<conio.h> int brsearch(int[],int,int,int); void main() { int a[10],i,n,s,key; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements in sorting order ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter search element"); scanf("%d",&s); key=bsearch(a,s,0,n-1); if(key!=-1) printf("Element found at %d position ",key+1); else printf("Element not found "); getch(); } int bsearch(int x[10],int s,int low,int high) { int mid; if(low>high) return -1; mid=(low+high)/2; if(s==x[mid]) return mid; else if(s<x[mid]) bsearch(x,s,low,mid-1); else bsearch(x,s,mid+1,high); }
D KOTESWARARAO, Associate Professor, CSE Dept. 159

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

OUTPUT: Enter the n value 6 Enter 6 elements in sorting order 12 23 34 45 56 67 Enter search element 45 Element found at 4 position

D KOTESWARARAO, Associate Professor, CSE Dept.

160

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-20

D KOTESWARARAO, Associate Professor, CSE Dept.

161

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

162

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Write C programs that implement the following sorting method to sort a given list of integers in ascending order: i) Quick sort #include<stdio.h> #include<conio.h> void quick(int a[100],int lb,int ub); int partition(int a[100],int lb,int ub); void main() { int x[100],i,n; clrscr(); printf("Enter how many elements we want"); scanf("%d",&n); printf("Enter %d elements\n ",n); for(i=0;i<n;i++) scanf("%d",&x[i]); clrscr(); printf("Given elements\n"); for(i=0;i<n;i++) printf("%4d",x[i]); quick(x,0,n-1); printf("\n Sorted Elements\n"); for(i=0;i<n;i++) printf("%4d",x[i]); getch(); } void quick(int a[100],int lb,int ub) { int i,j; if(lb>=ub) return; j=partition(a,lb,ub); quick(a,lb,j-1); quick(a,j+1,ub); } int partition(int a[100],int lb,int ub)
D KOTESWARARAO, Associate Professor, CSE Dept. 163

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

{ int s,down,temp,up; s=a[lb]; up=ub; down=lb; while(down<up) { while(a[down]<=s && down<ub) down++; while(a[up]>s) up--; if(down<up) { temp=a[down]; a[down]=a[up]; a[up]=temp; } } a[lb]=a[up]; a[up]=s; return up; } OUTPUT: Given elements 12 34 45 10 5 Sorted Elements 5 10 12 34 45

D KOTESWARARAO, Associate Professor, CSE Dept.

164

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-21

D KOTESWARARAO, Associate Professor, CSE Dept.

165

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

166

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Write C programs that implement the following sorting method to sort a given list of integers in ascending order: i) Merge sort #include<stdio.h> #include<conio.h> void mergesort(int,int); void merge(int,int,int); int a[10],b[10]; void main() { int i,n; clrscr(); printf("Enter the n value "); scanf("%d",&n); printf("Enter %d elements ",n); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(0,n-1); printf("\n Sorted Elements\n"); for(i=0;i<n;i++) printf("%4d",a[i]); getch(); } void mergesort(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; mergesort(low,mid); mergesort(mid+1,high); merge(low,mid,high); } } void merge(int low,int mid,int high) {
D KOTESWARARAO, Associate Professor, CSE Dept. 167

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

int h,i,j,k; h=low;i=low;j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h++; } else { b[i]=a[j]; j++; } i++; } if(h>mid) for(k=j;k<=high;k++,i++) b[i]=a[k]; else for(k=h;k<=mid;k++,i++) b[i]=a[k]; for(k=low;k<=high;k++) a[k]=b[k]; }

OUTPUT: Enter the n value 6 Enter 6 elements 45 23 87 65 34 12 Sorted Elements 12 23 34 45 65 87

D KOTESWARARAO, Associate Professor, CSE Dept.

168

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-22

D KOTESWARARAO, Associate Professor, CSE Dept.

169

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

170

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write C programs to implement the Lagrange interpolation #include<stdio.h> #include<conio.h> #define MaxN 90 void main() { float arr_x[MaxN+1], arr_y[MaxN+1], numerator, denominator, x, y=0; int i, j, n; clrscr(); printf("Enter the value of n: \n"); scanf("%d", &n); printf("Enter the values of x and y: \n"); for(i=0; i<=n; i++) scanf("%f%f", &arr_x[i], &arr_y[i]); printf("Enter the value of x at which value of y is to be calculated: "); scanf("%f", &x); for (i=0; i<=n; i++) { numerator=1; denominator=1; for (j=0; j<=n; j++) if(j!=i) { numerator *= x-arr_x[j]; denominator *= arr_x[i]-arr_x[j]; } y+=(numerator/denominator)*arr_y[i]; } printf("When x=%4.1f y=%7.1f\n",x,y); getch(); } OUTPUT: Enter the value of n: 4 Enter the values of x and y: 23 45 65 37 89 Enter the value of x at which value of y is to be calculated: 3 When x= 3.0 y= 7.0

D KOTESWARARAO, Associate Professor, CSE Dept.

171

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write C programs to implement the Newton- Gregory forward interpolation. #include<stdio.h> #include<conio.h> #define MaxN 100 #define Order_of_diff 4 void main () { float arr_x[MaxN+1], arr_y[MaxN+1], numerator=1.0, denominator=1.0, x, y, p, h, diff_table[MaxN+1][Order_of_diff+1]; int i,j,n,k; clrscr(); printf("Enter the value of n \n"); scanf("%d",&n); printf("Enter the values of x and y"); for(i=0; i<=n; i++) scanf("%f%f", &arr_x[i], &arr_y[i]); printf("Enter the value of x at which value of y is to be calculated"); scanf("%f", &x); h=arr_x[1]-arr_x[0]; for(i=0; i<=n-1; i++) diff_table[i][1]=arr_y[i+1]-arr_y[i];/*Creating the difference table and calculating first order differences*/ for(j=2; j<=Order_of_diff; j++)/*Calculating higher order differences*/ for(i=0; i<=n-j; i++) diff_table[i][j]=diff_table[i+1][j-1] - diff_table[i][j-1]; i=0; while(!(arr_x[i]>x)) /* Finding x0 */ i++; i--; p=(x-arr_x[i])/h; y=arr_y[i]; for (k=1; k<=Order_of_diff; k++) { numerator *=p-k+1; denominator *=k; y +=(numerator/denominator)*diff_table[i][k]; }
D KOTESWARARAO, Associate Professor, CSE Dept. 172

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

printf("When x=%6.1f, y=%6.2f\n",x, y); getch(); } OUTPUT: Enter the value of n 4 Enter the values of x and y 12 23 34 45 56 Enter the value of x at which value of y is to be calculated2 When x= 2.0, y= 3.00

D KOTESWARARAO, Associate Professor, CSE Dept.

173

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

174

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-23

D KOTESWARARAO, Associate Professor, CSE Dept.

175

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

176

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Write C programs to implement the linear regression and polynomial regression algorithms. #include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> float mean(float *a, int n); void deviation(float *a, float mean, int n, float *d, float *S); void main() { float a[20],b[20],dx[20],dy[20]; float sy=0,sx=0,mean_x=0,mean_y=0,sum_xy=0; float corr_coff=0,reg_coff_xy=0, reg_coff_yx=0; char type_coff[7]; int n=0,i=0; clrscr(); printf("Enter the value of n: "); scanf("%d",&n); printf("Enter the values of x and y:\n"); for(i=0;i<n;i++) scanf("%f%f",&a[i],&b[i]); mean_x=mean(a,n); mean_y=mean(b,n); deviation(a,mean_x,n,dx,&sx); deviation(b,mean_y,n,dy,&sy); for(i=0;i<n;i++) sum_xy=sum_xy+dx[i]*dy[i]; corr_coff=sum_xy/(n*sx*sy); printf("Enter the type of regression coefficient as 'x on y' or 'y on x': "); fflush(stdin); gets(type_coff); if(strcmp(type_coff,"x on y")==1) { reg_coff_xy=corr_coff*(sx/sy); printf("\nThe value of linear regression coefficient is %f",reg_coff_xy); } else if(strcmp(type_coff,"y on x")==1) {
D KOTESWARARAO, Associate Professor, CSE Dept. 177

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

reg_coff_yx=corr_coff*(sy/sx); printf("\nThe value of linear regression coefficient is %f",reg_coff_yx); } else printf("\nEnter the correct type of regression coefficient."); getch(); } float mean(float *a, int n) { float sum=0, i=0; for(i=0;i<n;i++) sum=sum+a[i]; sum=sum/n; return (sum); } void deviation(float *a, float mean, int n, float *d, float *s) { float sum=0,t=0; int i=0; for(i=0;i<n;i++) { d[i]=a[i]-mean; t=d[i]*d[i]; sum=sum+t; } sum=sum/n; *s=sqrt(sum); } OUTPUT: Enter the value of n: 3 Enter the values of x and y: 24 56 14 Enter the type of regression coefficient as 'x on y' or 'y on x': y on x The value of linear regression coefficient is 1.750000

D KOTESWARARAO, Associate Professor, CSE Dept.

178

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

WEEK-24

D KOTESWARARAO, Associate Professor, CSE Dept.

179

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

180

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

a) Write C program to implement Simpson method. #include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main() { float x0, xn, h, s,e1,e2, e3; char exp[80], arr[80]; int i,n,l=0; clrscr(); printf("\nEnter an expression: "); gets(exp); puts("Enter x0, xn and number of sub-intervals: "); scanf("%f%f%d", &x0, &xn, &n); h=(xn-x0)/n; if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]='\0'; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn); e3=4*eval(postfix, x0+h); s=log(e1)+log(e2)+log(e3); for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h); } else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn)+4*eval(postfix, x0+h); for (i=3;i<=n-1;i+=2) s+=4*eval(postfix,x0+i*h)+2*eval(postfix, x0+(i-1)*h);
D KOTESWARARAO, Associate Professor, CSE Dept. 181

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

} printf("The value of integral is %6.3f\n",(h/3)*s); return(0); } /*Inserting the operands in a stack. */ void push(float item) { if(top==99) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top++; stack[top]=item; } return; } /*Removing the operands from a stack. */ float pop() { float item; if(top==-1) { printf("\n\tThe stack is empty\n\t"); getch(); } item=stack[top]; top--; return (item); } void push1(char item) { if(top1==79) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top1++;
D KOTESWARARAO, Associate Professor, CSE Dept. 182

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

stack1[top1]=item; } return; } /*Removing the operands from a stack. */ char pop1() { char item; if(top1==-1) { printf("\n\tThe stack1 is empty\n\t"); getch(); } item=stack1[top1]; top1--; return (item); } /*Converting an infix expression to a postfix expression. */ void infix_postfix(char infix[]) { int i=0,j=0,k; char ch; char token; for(i=0;i<79;i++) postfix[i]=' '; push1('?'); i=0; token=infix[i]; while(token!='\0') { if(isalnum(token)) { postfix[j]=token; j++; } else if(token=='(') { push1('('); } else if(token==')') { while(stack1[top1]!='(') { ch=pop1();
D KOTESWARARAO, Associate Professor, CSE Dept. 183

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

postfix[j]=ch; j++; } ch=pop1(); } else { while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1(); postfix[j]=ch; j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) { ch=pop1(); postfix[j]=ch; j++; } postfix[j]='\0'; } /*Determining the priority of elements that are placed inside the stack. */ int ISPriority(char token) { switch(token) { case '(':return (0); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '?':return (0); default: printf("Invalid expression"); } return 0; }
D KOTESWARARAO, Associate Professor, CSE Dept. 184

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

/*Determining the priority of elements that are approaching towards the stack. */ int ICP(char token) { switch(token) { case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '\0':return (0); default: printf("Invalid expression"); } return 0; } /*Calculating the result of expression, which is converted in postfix notation. */ float eval(char p[], float x1) { float t1,t2,k,r; int i=0,l; l=strlen(p); while(i<l) { if(p[i]=='x') push(x1); else if(isdigit(p[i])) { k=p[i]-'0'; push(k); } else { t1=pop(); t2=pop(); switch(p[i]) { case '+':k=t2+t1; break; case '-':k=t2-t1; break; case '*':k=t2*t1; break;
D KOTESWARARAO, Associate Professor, CSE Dept. 185

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

case '/':k=t2/t1; break; default: printf("\n\tInvalid expression"); } push(k); } i++; } if(top>0) { printf("You have entered the operands more than the operators"); exit(0); } else { r=pop(); return (r); } return 0; } OUTPUT: Enter an expression: 3+6*5 Enter x0, xn and number of sub-intervals: 1 5 3 The value of integral is 88.000

D KOTESWARARAO, Associate Professor, CSE Dept.

186

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

b) Write C program to implement Trapezoidal method. #include<stdio.h> #include<conio.h> #include<math.h> char postfix[80]; float stack[80]; char stack1[80]; int top=-1,top1=-1; float eval(char postfix[], float x1); void infix_postfix(char infix[]); main() { float x0, xn, h, s,e1,e2; char exp[80], arr[80]; int i,n,l=0; clrscr(); printf("\nEnter an expression: "); gets(exp); puts("Enter x0, xn and number of subintervals"); scanf("%f%f%d", &x0, &xn, &n); h=(xn-x0)/n; if(exp[0]=='l'&& exp[1]=='o'&& exp[2]=='g') { l=strlen(exp); for(i=0;i<l-3; i++) arr[0]=exp[i+3]; arr[i]='\0'; infix_postfix(arr); e1=eval(postfix,x0); e2=eval(postfix,xn); s=log(e1)+log(e2); for (i=1;i<=n-1;i++) s+=2*log(eval(postfix,x0+i*h)); } else { infix_postfix(exp); s=eval(postfix,x0)+eval(postfix,xn); for (i=1;i<=n-1;i++) s+=2*eval(postfix,x0+i*h);
D KOTESWARARAO, Associate Professor, CSE Dept. 187

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

} printf("Value of the integral is %6.3f\n",(h/2)*s); return(0); } /*Inserting the operands in a stack. */ void push(float item) { if(top==99) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top++; stack[top]=item; } return; } /*Removing the operands from a stack. */ float pop() { float item; if(top==-1) { printf("\n\tThe stack is empty\n\t"); getch(); } item=stack[top]; top--; return (item); } void push1(char item) { if(top1==79) { printf("\n\tThe stack is full"); getch(); exit(0); } else { top1++;
D KOTESWARARAO, Associate Professor, CSE Dept. 188

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

stack1[top1]=item; } return; } /*Removing the operands from a stack. */ char pop1() { char item; if(top1==-1) { printf("\n\tThe stack1 is empty\n\t"); getch(); } item=stack1[top1]; top1--; return (item); } /*Converting an infix expression to a postfix expression. */ void infix_postfix(char infix[]) { int i=0,j=0,k; char ch; char token; for(i=0;i<79;i++) postfix[i]=' '; push1('?'); i=0; token=infix[i]; while(token!='\0') { if(isalnum(token)) { postfix[j]=token; j++; } else if(token=='(') { push1('('); } else if(token==')') { while(stack1[top1]!='(') {
D KOTESWARARAO, Associate Professor, CSE Dept. 189

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

ch=pop1(); postfix[j]=ch; j++; } ch=pop1(); } else { while(ISPriority(stack1[top1])>=ICP(token)) { ch=pop1(); /*Assigning the popped element into the postfix array. */ postfix[j]=ch; j++; } push1(token); } i++; token=infix[i]; } while(top1!=0) { ch=pop1(); postfix[j]=ch; j++; } postfix[j]='\0'; } int ISPriority(char token) { switch(token) { case '(':return (0); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '?':return (0); default: printf("Invalid expression"); break; }
D KOTESWARARAO, Associate Professor, CSE Dept. 190

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

return 0; } /*Determining the priority of elements that are approaching towards the stack. */ int ICP(char token) { switch(token) { case '(':return (10); case ')':return (9); case '+':return (7); case '-':return (7); case '*':return (8); case '/':return (8); case '\0':return (0); default: printf("Invalid expression"); break; } return 0; } /*Calculating the result of expression, which is converted in postfix notation. */ float eval(char p[], float x1) { float t1,t2,k,r; int i=0,l; l=strlen(p); while(i<l) { if(p[i]=='x') push(x1); else if(isdigit(p[i])) { k=p[i]-'0'; push(k); } else { t1=pop(); t2=pop(); switch(p[i]) { case '+':k=t2+t1; break; case '-':k=t2-t1;
D KOTESWARARAO, Associate Professor, CSE Dept. 191

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

break; case '*':k=t2*t1; break; case '/':k=t2/t1; break; default: printf("\n\tInvalid expression"); break; } push(k); } i++; } if(top>0) { printf("You have entered the operands more than the operators"); exit(0); } else { r=pop(); return (r); } return 0; } OUTPUT: Enter an expression: 3+6*5 Enter x0, xn and number of sub-intervals: 1 5 3 The value of integral is 88.000

D KOTESWARARAO, Associate Professor, CSE Dept.

192

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

C - Language Viva Questions

D KOTESWARARAO, Associate Professor, CSE Dept.

193

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

D KOTESWARARAO, Associate Professor, CSE Dept.

194

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

1) What is the computer? Ans: A computer is an electronic device, processing the data according to set of instructions, and produce the results, may be store in the system. 2) What is the CPU? Ans: CPU stands for Central Processing Unit, Responsible for controlling all activities of the computer system. 3) What are the input devices? Ans: keyboard, mouse, joysticketc 4) What are the output devices? Ans: printer, monitor, speaker etc. 5) What is the ALU? Ans: ALU stands for Arithmetic and Logic unit. To perform the Arithmetic and Logic operations. 6) What is the CU? Ans: CU stands for Control unit, to control the all functionality of the system. 7) What is the program? Ans: program is a set of instruction. 8) What is the algorithm? Ans: an algorithm is a step by step procedure to solve the problem. 9) What is the Flowchart? Ans: A graphical representation of the algorithm is called a flowchart. 10) What are the program development steps? Ans: a) problem definition b) Problem design c) Coding d) Debugging e) Maintenance 11) What is the C? Ans: C is a general purpose programming language. 12) Who developed C language? Ans: Dennis Ritchie in 1972. 13) What are the features of C language? D KOTESWARARAO, Associate Professor, CSE Dept. 195

C - Language Viva

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

Ans:

a) C is a robust b) C is a middle level language c) C is a Structured Programming language d) C is a portable e) C is easily extendable f) C program is efficient and fast g) C have only 32 keywords.

14) What are the applications of C language? Ans: System Software like Compiles, interpreter, spread sheets, database ets Application software and Games. 15) What is the identifier? Ans: identifiers refers to name of the variables, functions, and arrays. These are user defined names. 16) What is the keyword? Ans: keywords have special meaning that is define by the system. 17) What is the variable? Ans: variable is date name may be stores the data values, it refers to the memory location. 18) What are the rules to create the variables? Ans : a) Variable name must begin with a letter or underscore. b) Variable consists only letters, digits, underscore. c) The variable should not be a keyword. d) White spaces is not allowed. 19) What is the Data Type? Ans: Data type specifies to the system which type of values to be stores and how much memory to be allocated to the variable. 20) What are the basic data types? Ans: int, char, float, double. 21) What is the size of the each basic data types? Ans: int 2 Bytes, char 1 Bytes, float 4 Bytes, double 8 Bytes. 22) What is the constant? Ans: Constant is fixed value does not change during the execution of the program. 23) What is the integer constant? Ans: An integer constant refers to a sequence of digits. 24) What is the character constant? Ans: A single character constant contains a single character enclosed within pair of single quote marks. D KOTESWARARAO, Associate Professor, CSE Dept. 196

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

25) What is the real constant? A number followed by a decimal point and fractional part. 26) What is the string constant? Ans: A string constant is a sequence of character enclosed in double quotes. 27) What is the expression? Ans: An expression is a combination of variables, constants, and operators arranged as per the syntax of the language. 28) What is the operator? Ans: Operator is symbol that tells to the computer to perform certain operation on the operand. 29) What is the operand? Ans: Operand is variable or constant. 30) What is the sizeof()? Ans: The operator is used to find out the how many bytes allocated for variable, constant or data type. 32) What is the pre increment and post increment? Ans: preincrement means first perform he increment after then perform the operation. postincrement means first perform he operations after then perform the increment. 33) What is the pre decrement and post decrement? Ans: predecrement means first perform he decrement after then perform the operation. postdecrement means first perform he operations after then perform the decrement. 34) What is the header file? Ans: header file contains several built in functions. 35) What is the stdio.h? Ans: Standard input output header file. It provides standard input and output functions. 36) What is the conio.h? Ans: console input and output header file. 37) What is the standard input and output function? Ans: scanf(), printf(). 38) What meant by formatted input and output functions? Ans: formatted input output functions are read or write (print) any type of data values. 39) What are the formatted input and output functions? Ans: scanf(), printf(). D KOTESWARARAO, Associate Professor, CSE Dept. 197

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

40) What are unformatted input and output functions? Ans: unformatted input output functions are used to read or write only characters. Ex: getchar(),getch(),gets(),putchar(),puts().. etc. 41) What is the Block? Ans: a block is a group of statement into a single unit. 42) What are the conditional statements? Ans: a) simple if, b) if-else, c) nested if else d) else if ladder e) switch case. 43) What is the difference between else-if ladder and switch case? Ans: switch case reduce the complexity of the program. 44) What is the loop? Ans: loop is a block of statements which are repeatedly executed for certain number of times. 45) How many loops available in C language? Ans: 3, while, do-while, for. 46) What is the difference between while and do-while? Ans: while is the pretest condition, do-while is the posttest condition. In do-while the statements will be executed at least once. 47) What is the break statement? Ans: exit from the block. 48) What is continue statement? Ans: when execute the continue statement skip the next statement and control goes to next iteration. 49) What is the need of the exit? Ans: exit from the program. 50) What is the array? Ans: Array is a group of related data items that share a common name. (or) array is a collection of homogenous elemements. 51) What is the difference between array and ordinary variable? Ans: ordinary variable can store one value at a time but array variable can stores many values at a time. 52) What is the index? ans: index a variable. it can be used to access the element in the array. 53) What is the range of the index in general? Ans: 0 to n-1. D KOTESWARARAO, Associate Professor, CSE Dept. 198

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

54) What is the string? Ans: a sequence of characters is called string. 54) What is the function? Ans: function is a self contained block, it perform the particular task. 55) What are the advantages of the function? Ans: a) reduce the length of the program. b) easy to understand c) easy to debug d) easy to maintain e) reusability. 56) What is meant by actual arguments?? Ans: which are the arguments passed in the calling function is called actual arguments. 57) What is meant formal arguments? Ans: which are the arguments declared in the called function is called actual arguments. 58) What is the prototype? Ans: function prototypes tells the compiler the number and type of arguments that are to be passed to function and the type of value that is to be returned by the function. 59) How to call the function? Ans: using function name. 60) What meant by storage classes? Ans: storage classes specifies the scope and life time of the variable. 61) How many storage classes available in C language? ans: 4, auto, extern, static, register. 62) What is the difference between auto and extern? Ans: auto means the variables access within that function, extern means the variable access throughout the program. 63) What is the recursive function? Ans: A function call itself is called a recursive function. 64) What is the Pointer? Ans: Pointer is a variable which stores the address of the another variable. 65) What are the advantages of the Pointer? Ans: a) pointers are more efficient in handling the data tables. b) pointers reduce the length and complexity of the program D KOTESWARARAO, Associate Professor, CSE Dept. 199

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

c) Increasing the execution speed of the program d) dynamic memory allocations e) easily handling the data structures 66) How to allocate dynamic memory for the variable? Ans: malloc() 67) How to delete dynamic memory for the variable? Ans: free() 68) What is the Structure? Ans: A Structure is a collection of one or more variables, possibly of different data types. 69) What is the difference between array and structure? Ans: Array is a collection of similar data items but structure is a collection of different data items. 70) How to access the structure variable? Ans: structure variable can be accessing using . or -> operator. 71) What is the union? Ans: Union is a collection of one or more variables, possibly of different data types. 72) What is the difference between structure and union? Ans: main difference between structure and union is allocation of the memory. In union all members of a union use the same memory location but the structure using different locations. 73) What is the preprocessor? Ans: preprocessor is program to process the data before pass to the compiler. 74) What is the need of the preprocessor? Ans: fast execution of the program. 76) What is the file? Ans: A file is collection of records. The main purpose of the file is to store the data permanently. 77) Which function to be used to open the file? Ans: fopen() 78) What are the modes to be used in the files? Ans: r read, w write , a append. 79) What is the compiler? Ans: compiler is program it translates the source program into object program.

D KOTESWARARAO, Associate Professor, CSE Dept.

200

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

80) What interpreter? Ans: Interpreter is program it translates the source program into object program. 81) What is the difference between compiler and interpreter? Ans: compiler translate the source program at a time but the interpreter translate line by line. 82) What is the bit? Ans: bit is a binary digit. Example: o and 1. 83) What is the byte? Ans: group of 8 bits is called Byte. 84) What is the operating system? Ans: The system software that controls the computer. 85) What is the software? Ans: Software is the set of programs and applications that tell the computer what to do. 86) What is the hardware? Ans: Hardware is physical component of a system, basically anything that you can touch with your fingers. 87) What is the data? Ans: Information processed by a computer. 88) What is a Stack? Ans: Stack is a collection of ordered items in which insertion and deletion can be performed at one end is called as top of the stack. 89) What is a Queue? Ans: Queue is a collection of ordered items in which insertion can be performed at one end is called as rear and deletion can be performed other end is called as front. 90) What are the operations performed on Stacks? Ans: push and pop 91) what are the operations performed on Queue? Ans: insertion and Deletion. 92) What is Push()? Ans: Insert the element at the top of the stack 93) What is Pop()? Ans:Delete the top element at the top of the stack. D KOTESWARARAO, Associate Professor, CSE Dept. 201

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

94) what are the applications of Stack? Ans: a) Infix to Postfix conversion b) Postfix Evaluation c) Paranthesis matching d) Function calls e)recurssion 95) What are the applciations of Queue? ans: simulation b) CPU scheduling c) Client/server systems d) page replacement algorithms. 96) What is a Data structures? Ans: Data structures specifies way of organizes the data in memory. 97) What ia an Abstract Data Type ? Ans: abstract data type is a mathematical model of data structures. 98) what is Linked list? Ans: Linked list is a collection of nodes each node having link with another node(s). 99) What is Single Linked list? Ans: Single linked list is a collection of nodes, each node having two fields one is info, another one is next field. 100) What is Double Linked List? Ans:Double linked list is a collection of nodes, each node having three fields one is info, another two are next and prev fields. 101) what is the drawback of Single Linked list? Ans: only forward accessing, it is not possible to accessing backward. 102) What is the difference between array and linked list? Ans: array is a static, linked list is a dynamic 103) what are the types of Data structures? Ans: 1.Linear Data Structures 2. Non-Linear Data Structures. 104) What is mean by searching? Ans: searchin means to find the element is present or not in the list 105) what is meant by sorting? Ans: sorting means arrange the elements in particular order. 106) What is Space complexity? Ans: It is defined as the amount of memory need to run to completion by an program. 107) what is Time complexity? Ans: It is defined as the amount of Computer time need to run to completion by an program. D KOTESWARARAO, Associate Professor, CSE Dept. 202

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

108) what is Time complexity of Linear search? Ans: Best Case: O(1) Average Case: O(n) Worst Case: O(n) 109) what is Time complexity of Binary search? Ans: Best Case:O(1) Average Case:O(logn) Worst Case:O(logn) 110) what is Time complexity of Quick sort? Ans: Best Case:O(nlogn) Average Case:O(lnogn) Worst Case:O(n2) 111) what is Time complexity of Merge sort? Ans: Best Case: O(lnogn) Average Case: O(lnogn) Worst Case: O(lnogn) 112) what is Time complexity of Bubble sort? Ans: O(n2) 113) what is Time Complexity of Insertion sort? Ans: O(n2) 114) what is Infix expression? Ans: The operator between two operands. 115) what is Postfix expression? Ans: The operator after two operands 116) what is Time complexy of selection sort? 117) What is Prefix expression? Ans: The operator precending two operands

DOS - acronym for Disk Operating System - used in IBM PCs.


RAM - acronym for Random-Access Memory

ROM - acronym for Read Only Memory; memory that can only be read from and not written to.
UPS - acronym for Uninterruptible Power Source D KOTESWARARAO, Associate Professor, CSE Dept. 203

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

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.

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

D KOTESWARARAO, Associate Professor, CSE Dept.

204

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

COMPUTER PROGRAMMING LAB MANUAL

FOR ANY QUERIES PLEASE CONTACT

D KOTESWARARAO Associate Professor, Dept. of CSE, VVIT. Phone No: 9441544277 Email Id:d_koteswararao@yahoo.co.in ThankQ ***************************

D KOTESWARARAO, Associate Professor, CSE Dept.

205

VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Anda mungkin juga menyukai