C&DS Lab Manual

  • Upload
    aepuri

  • View
    44

  • Download
    4

Embed Size (px)

Citation preview

Department of Information TechnologyC and Data Structures Lab manual Lab Manual for the Academic Year 20072008

I B.Tech

Guru Nanak Engineering CollegeIbrahimpatnam, R R District 501 506 (A. P.)

In-charge Principal Prepared by:

HOD

Approved & Reviewed by:

Issued by:

w.e.f Date:

Guru Nanak Engineering CollegeIbrahimpatnam, R R District 501 506 (A. P.)

Department of Information Technology Lab Manual for the Academic Year 2007-08 (In accordance with JNTU syllabus)SUBJECT SUBJECT CODE SEMESTER STREAM INSTRUCTOR PROGRAMMERSREDDY

: : : : : :

C & DS LAB CS 05442 I Information Technology 1. NISHANTA 2. SREEKANTH1) M.B. NEELAKANTESHWARA

2) G.SRINIVASULU

Head IT

Guru Nanak Engineering CollegeIbrahimpatnam, R R District 501 506 (A. P.)

Suggestions from Principal:

Enhancement if any:

Comments:

3

INDEXS. No.1 2 3 4 5

CONTENTIntroduction About Lab Guidelines to Students List of Lab Exercises Syllabus Programs (JNTU) Solutions for Programs References

Page. No5 7 8 11 202

Result: ________________ charge

Signature of the faculty In-

5

INTRODUCTION ABOUT LABThere are 66 systems (Compaq Presario) installed in this Lab. Their configurations are as follows: Processor RAM Hard Disk Optical Drive Monitor Mouse Key Board Network Topology Network Interface card : : : : : : : : : AMD Athelon 1.67 GHz 256 MB 40 GB 52X CD-ROM 15 CRT Color Monitor Optical Mouse 105 MMX Key Board

Star Topology Present

Software 1All systems are configured in DUAL BOOT mode i.e., Students can boot from Windows XP or Linux as per their lab requirement. This is very useful for students because they are familiar with different Operating Systems so that they can execute their programs in different programming environments. 2Each student has a separate login for database access Oracle 9i client version is installed in all systems. On the server, account for each student has been created. This is very useful because students can save their work ( scenarios, pl/sql programs, data related projects ,etc) in their own accounts. Each student

work is safe and secure from other students. 3Latest Technologies like DOT NET and J2EE are installed in some systems. Before submitting their final project, they can start doing mini project from 2nd year onwards. 4MASM ( Macro Assembler ) is installed in all the systems Students can execute their assembly language programs using MASM. MASM is very useful students because when they execute their programs They can see contents of Processor Registers and how each instruction is being executed in the CPU. 1. Rational Rose Software is installed in some systems Using this software, students can depict UML diagrams of their projects. 2Softwares installed: C, C++, JDK1.5, MASM, OFFICE-XP, J2EE and DOT NET, Rational Rose.

3Systems are provided for students in the 1:1 ratio.

4Systems are assigned numbers and same system is allotted for students when they do the lab.

7

LAB CODE:1Equipment in the lab for the use of student community. Students need to maintain a proper decorum in the computer lab. Students must use the equipment with care. Any damage is caused is punishable. 2Students are required to carry their observation / programs book with completed exercises while entering the lab. 3Students are supposed to occupy the machines allotted to them and are not supposed to talk or make noise in the lab. The allocation is put up on the lab notice board. 4Lab can be used in free time / lunch hours by the students who need to use the systems should take prior permission from the lab in-charge. 5Lab records need to be submitted on or before date of submission. 6Students are not supposed to use floppy disks

9

LIST OF LAB EXERCISES: S No1

Name of the programa)To evaluate algebraic exp(ax+b)/(ax-b) b)to Evaluate algebraic exp 2.5logx+cos32+|x*x-y*y|+sqrt(2*x*y) c)to evaluate the algebraic exp aepower-rt d)to evaluate algebraic exp x power5 +10 x power 4+8 x power3+4x+2 To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c) To swap 2 no Greatest of 2 no Greatest of 3 numbers Greatest of 3 onto print the given no in ascending order To perform the arithmetic expression using switch statement Factorial of given no using do while statement To print prime up to n no Sum of n natural no Total no. of even integers Total no. of odd integers Sum of even integers Sum of odd integers A program to print the product of two matrices of any order Write a program to print Fibonacci series Write a program to print o/ps a) 1 b) 1 c) 1 d) 1 22 2 2 22 23 333 3 3 3 333 4 56 Write a program to read n num of students and 5 sub marks Write a program to find factorial of a num using 3 types of funs Write a program to convert all lower case to uppercase characters.

Page No.10 12 14 16 18 20 22 24 27 30 32 35 38 40 43 46 47 50 53 56

2 3 4 5 5 6 7 8 9 10 11 12 13 14 15 16

17 18 19

68 71 76

20 21 22 23 24 25 26 27 28 29 30

Write a program to extract a string Write a program to sort 5 city names in alphabetical order Write a program to find the factorial of a number using recursion A program to print address of variable A program to access a variable using pointers A program to print the element of array using pointers A program to implement call by reference A program to find greatest of n num using funs A program to print the elements of a structure using pointers A program to display student information by initializing structures A program to find total number of marks

79 82 85 88 93 95 97 100 103 106 108

S No31 32 33 34 35 36 37 38 39 40

Name of the programA program to find the tot salary of employee and salary of employee details A program to pass structure as an arguments to fun and cal total marks of 5 subjects A program to display college address using pointers and structures A program to write data file and read data from file A program to write integer data into file and read it from file A program to write product details Use of command line arguments in files Stack operations using arrays Circular queue operations using arrays Infix-postfix operations

Page No.112 116 118 120 123 126 129 132 137 143

11

41 42 43 44 45 46 47 48 49 50 51

Postfix evaluation Prefix-evaluation Single linked list Double linked lists Bubble Sort Selection Sort Insertion Sort Quick Sort Heap Sort Binary Search Linear Search

146 149 155 160 169 171 174 176 179 182 184

Experiment 1:a)To evaluate algebraic exp(ax+b)/(ax-b) b)to Evaluate algebraic exp 2.5logx+cos32+|x*x-y*y|+sqrt(2*x*y) c)to evaluate the algebraic exp aepower-rt d)to evaluate algebraic exp x power5 +10 x power 4+8 x power3+4x+2

(a) 1) AIM: To evaluate algebraic exp(ax+b)/(ax-b) 2) ALGORITHM:Step1: start Step2: input a,b,x,s Step3: s= (a*x+b)/(a*x-b) Step4: Result s Step 5: stop

3) FLOW CHART:To evaluate algebraic exp (ax+b)/ (ax-b)

13

4) PROGRAM:To evaluate algebraic exp(ax+b)/(ax-b) main() { int a,b,x; float s; clrscr(); printf("enter the values of a,b,x..."); scanf("%d%d%d",&a,&b,&x); s=(a*x+b)/(a*x-b); printf("the value of s=%f",s); }

5) Result:Enter the values of a,b,x 1 3 2 The value of s=5

(b) 1) AIM: To Evaluate algebraic exp 2.5logx+cos32+|x*x-y*y|+sqrt (2*x*y)

2) ALGORITHM:Step1: start Step2: input x,y,v Step3: v=2.5*log(x)+cos(32*3.14/180)+mod(x*x-y*y)+sqrt(2*x*y) Step4: Result v Step 5: stop

3) FLOWCHART:To Evaluate algebraic exp 2.5logx+cos32+|x*x-y*y|+sqrt(2*x*y)

15

4) PROGRAM: To Evaluate algebraic exp 2.5logx+cos32+|x*x-y*y|+sqrt(2*x*y) #include main() { float x,y,v; clrscr(); printf("enter x and y values"); scanf("%f,%f",&x,&y); v=2.5*log(x)+(cos(32*3.14/180))+mod(x*x-y*y)+sqrt(2*x*y); printf("the value of v=%f",v); }

5) Result:Enter x and y values 10 20 The value of v=

c) 1) AIM: To evaluate algebraic exp x power5 +10 x power 4+8 x power3+4x+2 2) ALGORITHM:Step1: start Step2: input x,s Step3:s=pow(x,s)+10*pow(x,4)+8*pow(x,3)+4*x+2 Step4: Result s Step 5: stop*/

3) FLOWCHART:To evaluate algebraic exp x power5 +10 x power 4+8 x power3+4x+2

17

4) PROGRAM:To evaluate algebraic exp x power5 +10 x power 4+8 x power3+4x+2 #include #include main () { float x,s; printf("enter the values of x"); scanf("%f",&x); s=pow(x,5)+10*pow(x,4)+8*pow(x,3)+4*x+2; printf("the value of s=%f",s); }

5) Result:Enter the values of x 1 The value of s = 25

d) 1) AIM: To evaluate the algebraic exp ae power-rt 2) ALGORITHM:step1: take a,k and t step2: assign values for them step3: here a*pow(e,-k*t) store this in 'r' Display 'r' step4: stop*/

3) FLOWCHART:To evaluate the algebraic exp a epower-rt

19

4) PROGRAM:To evaluate the algebraic exp aepower-rt #include #include main() { int a,k,t; float r; printf("enterthree values"); scanf("%d%d%d",&a,&k,&t); r=a*pow(e,-k*t); printf("result=%f"); getch(); }

5) Result:Enter values 1 2 3 Result=1.000000

6) Questions:i) What is an Expression? ii) What is the use of main( ) function? iii) What are preprocessors of C? iv) What is a variable?

7) Debugging:1) undefined symbol a in function main( ) 2) r is assigned a value which is never used 3) redeclaration of c in function main( ) First you should declare a and use When you assigned a value to a variable, that must be used in the program You should declare any variable only one

21

time

Experiment 2: evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c) 1) AIM: To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c) 2) ALGORITHM:Step1:start Step2:input a,r,t,s Step3:s=a* pow(-r*t) Step4:Result s Step 5:stop*/

3) FLOWCHART:To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)

23

4) PROGRAM: To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c) #include void main() { int a,b,c; float s,area; clrscr(); printf("enter the values of a,b,c"); scanf("%d%d%d",&a,&b,&c); s=(a+b+c)/2; area=sqrt(s*(s-a)*(s-b)*(s-c)); printf("the area of a trangle is =%f",area); getch(); }

5) Result:enter the values of a,b,c 10 20 30 The area of a trangle is = 0.000000

6) Questions:i) What is the use of sqrt( ) function? ii) Explain data types. iii) Explain I/O Statements. 7) Debugging: 1) Function sqrt( ) should have a prototype You should include math.h first, then you can use sqrt ( ) and other mathematical

functions. 2) Unterminated string or character constant 3) Function call missing ) in function main You should end double quotation or single quotation properly You might be missed any special characters in that line.

Experiment 3: Swapping given two numbers1) AIM: Program to swap two numbers 2) ALGORITHM:Step1:start Step2:input a,b Step3:a=a+b Step4:b=a-b Step 5:a=a-b Step6:Result a,b Step7:stop

3) FLOWCHART:Program to swap two numbers

25

4) PROGRAM: Program to swap two numbers void main() { int a,b; clrscr(); printf("enter the values of a,b"); scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("the values of a,b are: %d %d",a,b); getch(); }

5) Result:Enter the values of a,b 10 2 The values of a,b are: 20 10

6) Questions:i) What is the use of getch( ) function? ii) What is the use of specifications of the data types?

Experiment 4: Find greatest number in given two numbersusing conditional operator 1) AIM: Program to find greatest of 2 numbers using conditional operator 2) ALGORITHM:Step1:start Step2:input a,b,c Step3:c=(a>b)?a:b Step4:Result c Step 5:stop*/

3) FLOWCHART:To Find Greatest of Two numbers.

27

0100090000032a0200000200a20100000000a201000026060f003a03574d464301000000 00000100e7830000000001000000180300000000000018030000010000006c000000000 0000000000000350000006f00000000000000000000009c3100007d21000020454d4600 0001001803000012000000020000000000000000000000000000005613000032190000d 2000000110100000000000000000000000000008e3203009a2a0400160000000c000000 180000000a0000001000000000000000000000000900000010000000b80b0000e907000 0250000000c0000000e000080250000000c0000000e000080120000000c000000010000 00520000007001000001000000a4ffffff000000000000000000000000900100000000000 004400022430061006c0069006200720069000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000001 b007cee1b0010000000e0f11b0060ef1b005251625ce0f11b00d8ee1b001000000048f01b 00c4f11b002451625ce0f11b00d8ee1b002000000049646e60d8ee1b00e0f11b002000000 0ffffffffdc223801d0646e60ffffffffffff0180ffff01802fff0180ffffffff00f500000008000000 0800000c6f3a0001000000000000005802000025000000372e90010000020f0502020204 030204ef0200a07b20004000000000000000009f00000000000000430061006c00690062 007200000000002050ad0170a0625ca216ff2bbc2238010441ff000cef1b009c386660050 000000100000048ef1b0048ef1b00e87864600500000070ef1b00dc22380164760008000 00000250000000c00000001000000250000000c00000001000000250000000c00000001 000000180000000c0000000000000254000000540000000000000000000000350000006 f00000001000000f0c187401e7187400000000057000000010000004c000000040000000 000000000000000b80b0000e90700005000000020000000360000004600000028000000 1c0000004744494302000000ffffffffffffffffb90b0000ea070000000000004600000014000 000080000004744494303000000250000000c0000000e000080250000000c0000000e00 00800e000000140000000000000010000000140000000400000003010800050000000b0 200000000050000000c02f3006801040000002e0118001c000000fb02020001000000000 0bc02000000000102022253797374656d0000000000000000000001003f3f3f3f3f3f3f3f0 0003f3f3f00040000002d010000040000002d01000004000000020101001c000000fb02f5 ff0000000000009001000000000440002243616c69627269000000000000000000000000 00000000000000000000000000040000002d010100040000002d010100040000002d010 100050000000902000000020d000000320a0a00000001000400000000006901f3002000 0600040000002d010000040000002d010000030000000000

4) PROGRAM:Prog:To find greatest of 2 numbers void main()

{ int a,b,c; clrscr(); printf("enter the values of a,b"); scanf("%d%d",&a,&b); c=(a>b)?a:b; printf("the biggest no is %d",c); getch(); }

5) Result:Enter the values of a,b 5 8 The biggest number is : 8

6) Questions:1) What is an operators? 2) How many operators are there in C and List out them? 3) What is the difference between logical and conditional operators?

Experiment 5: numbers

Write a program to find greatest among 3

29

1) AIM: Program to find greatest among 3 numbers

2) ALGORITHM:Step1:start Step2:input a,b,c Step3:if(a>b) &&(a>c) Step4:display a is grater Step 5:else Step6:if(b>c) Step7: display b is grater Step 8:else Step: display c is grater Step10:stop

3) FLOWCHART:

To find greatest among 3 numbers

31

4) PROGRAM:Program to find greatest among 3 numbers void main() { int a,b,c; clrscr(); printf("enter the values of a,b and c"); scanf("%d%d%d",&a,&b,&c); if(a>b && a>c) printf("a is greatest of %d %d %d", a,b,c); else if(b>c) printf("b is greatest of %d %d %d",a,b,c); else printf("c is gratest of %d %d %d",a,b,c); getch(); }

5) Result:Enter the values of a,b and c 10 30 20 30 is greatest of 10 30 20

6) Questions:i) What are the conditional statements? ii) How many conditional statements are there in C? iii) What is the difference between conditional and multi-conditional statements?

Experiment 5: Program to find Greatest of 3 numbers to print the given no in ascending order. 1) AIM: Program to find Greatest of 3 numbers to print the given no inascending order.

2) ALGORITHM:Step1:start Step2:input a,b,c Step3:if(a>b) &&(a>c) Step4:if(b>c) Step5:display a,b,c Step6:else Step7:display a,c,b Step8:else if(brptr=new; break; case3: printf("enter the elements after which u want to insert \n"); scanf("%d",&p); for(;t2!=null && t2->info!=p;t2=t2->rptr) if(t2=null)

{ printf("elements not found \n"); } else { new->rptr=t2->rptr; t2->rptr->lptr=new; t2->prtr=new; new->lptr=t2; } break; } } } void delete() { int flag=0,ele; struct node *t3=head; if(t3==null) { printf("\n noelemnet"); exit(); } else { printf("enter the elemt to be deleted");

189

scanf("%d",&ele); while(t3!=null) { if(t3->info==ele) { flag=1; if(t3==head) { head=t3->rptr; head->rptr=null; free(t3); } else { if(t3->rptr!=null) { t3->lptr=t3->rptr; t3->rptr=t3->lptr; free(t3); } else { t3->lptr->rptr=null; free(t3); } }

} t3=t3->ptr; } if(flag==0) { printf("element not found\n"); } } }

191

3) Result:1) Insert 2) Delete 3) Display Enter choice for circular queue 1 Enter any item 14 1) Insert 2) Delete 3) Display Enter choice for circular queue 1 Enter any item 15 1) Insert 2) Delete 3) Display Enter choice for circular queue 1 Enter any item 20 1) Insert 2) Delete 3) Display Enter choice for circular queue 3 14 15 20

Experiment 45 Program to implement Bubble sort 1) AIM: Program to implement Bubble sort 2) ALGORITHM:step1: take first two elements of a list and compare them step2: if the first elements grater than second then interchange else keep the values as it step3: repeat the step 2 until last comparison takes place step4: reapeat step 1 to 3 until the list is sorted

193

EXPERIMENT 45: Program to implement Bubble sort AIM: Program to implement Bubble sort 3) PROGRAM:Program to implement Bubble sort #include main() { int a[10],i,j,temp,n; clear(); printf("\n enter the max no.of elements u wanna sort \n"); scanf("%d",&n); printf("\n enter the elements u want to sort \n"); for(i=0;i