final 1st year c&ds lab manual (a.y.2011-12)
Post on 21-Apr-2015
45 Views
Preview:
TRANSCRIPT
LAB INSTRUCTIONS
1. Students should report to the concerned lab as per the time table.
2. Students who turn up late to the labs will in no case be permitted to do the program schedule for the day. 3. Student should bring a observation book of 200 pages and should draw the flowchart, Algorithm and write program in the observation book along with the sample input and output of the program while performing the execution of the program4. After completion of the program, certification of the concerned staff in-charge in the
observation book is necessary.5. The immediate last lab session program to be written in the lab record book and should be
submitted and corrected by the concerned faculty.6. Viva must be conducted for each student for 5 mints in each lab session about the
understanding of the lab program.7. The student should not be allowed without the observation book and without completion of
record book of the last immediate lab session.8. To improve the understanding of the fundamentals of C&DS additional programs given by
the faculty are to be executed by the students.9. Out of the 25 marks for internal, day-to-day work in the lab shall be evaluated for 15 marks
and internal examination for practical shall be evaluated for 10 marks conducted by the concerned faculty.10. If a student is absent for a lab session the concerned faculty should intimate to the student
parents immediately. .
_____________________________________________________________________________________ 1 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
List of ExperimentsExperiment NAME OF PROGRAM A)Sum of individual digits of given integer 1 B)generate first n terms of Fibonacci series C)generate prime numbers between 1 and n A)calculate sum of series 2 SUM=1-x2/2! +x4/4!-x6/6!+x8/8!-x10/10! B) Roots of a quadratic equation. 3 a) To find the factorial of a b) To find the GCD of two c) To solve Towers of Hanoi problem 4)a)Calculation if s=ut+1/2at2 4 b) program, which takes two integer operands and one operator form the user(+,-,*,/,% use switch) 5)a)find largest and smallest number in a list of integers 5 b) program that uses functions to perform i)Addition of Two Matrices ii)Multiplication of Two Matrices Use functions to perform the following operations: a)i)insert sub-string into main string from given pos. ii)delete n Characters from a given position in given string. b) given string is a palindrome or not a) display the position or index in the string S where the string T begins, or 1 if S doesn't contain T. b) count the lines, words and characters in a given text. 8 9 a) generate Pascal's triangle b) construct a pyramid of numbers a) geometric progression: sum=1+x+x2+x3+.+xn 58 64 34 28 given integer. given integers. 18 12 5 PAGE NO
6
39
7
49
_____________________________________________________________________________________ 2 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
perform error checking 10 a) 2's complement of a number b) convert a Roman numeral to its decimal equivalent A)use functions to perform following ops on complex numbers a)read b)write c)add d)multiply (Use structure to represent complex number) a)program to copy one file to another 12 b)to reverse first n characters in file (file name and n specified on command line) Write a C program to display contents of file. 13 C program to merge two files into a third file (That is contents of first file followed by those of second are put in a third files) 90 83 69
11
78
14 15 16
Use functions to perform following ops On single linked list a)creation b)deletion c)display d)traversal in 2 way Implement stack operations using a)arrays b)pointers Implement queue operations using a)arrays b)pointers Use stack operations
95 106 114
17
a)to convert infix to postfix expression b)evaluate postfix expression
122
18
a)Bubble sort b) Selection sort Searching
127
19
a)linear search b)binary search
134
20 21
Sorting a)quick sort Sorting
140 146
_____________________________________________________________________________________ 3 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
a)merge sort 22 23 24 Implement the Lagrange interpolation and Newton- Gregory forward interpolation Implement the linear regression and polynomial regression algorithms Implement Trapezoidal and Simpson methods 150 157 160
Experiment: 1 1A. Write a Program in C to find the sum of individuals Digits of a positive Integer.AIM: Teaching the students how to write simple logic in all these programs_____________________________________________________________________________________ 4 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Recommended systems/software requirements : Intel based desktop pc ANSI C compiler with supporting editors Theory: a) In mathematics, the digit sum of a given integer is the sum of all its digits, (e.g.: the digit sum of 84001 is calculated as 8+4+0+0+1 = 13). Digit sums are most often computed using the decimal representation of the given number. Algorithm: step 1: start step 2: Read the number n step 3:initialize sum =0 step 4: repeat the steps 4-7 while n not equal to 0 step 5:do module division by 10 to get remainder step6: add the remainder to a sum variable step7:do division by 10 to get the quotientStart
r=n%10
sum=sum+r n=n/10
step 8: print the sum step 7: stopInput n
If n< 0 s=0.m=n
Flowchart
Repeat until n=0 r=n%10 s=s+r n=n/10 Invalid input
print m, s _____________________________________________________________________________________ 5 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL Stop
Yes
No
Yes No
Result: Input: Enter the number 987 Output: The sum of individual digits is 24
1B. Write a program to find 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 precedes two preceding two terms in the sequence, Write a C Program to Generate the first n terms of the sequence. AIM: Teaching the students how to write simple logic in all these programs Recommended systems/software requirements :_____________________________________________________________________________________ 6 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Intel based desktop pc ANSI C compiler with supporting editors Theory: The Fibonacci Summation Series takes 0 and adds 1. Succeeding numbers in the series adds the previous two numbers and thus we have 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 to infinity. Do you see the pattern? 1+1=2, 1+2=3, 2+3=5, 3+5=8, 5+8=13..... Solution: step1: start step2: read number of Fibonacci number to be printed into n step3: initialize I to 2, f1 to 0, f2 to 1 step4: print f1,f2 step5: repeat the steps 5-10 upto while icount[i-1]) total-=count[i-1] else total+=count[i-1] end step6: total += count[ SIZE - 1 ] step7: print total step8: stop Result: Input:
Enter the roman numeral: MDCCVIII
Output:
Its decimal equivalent is : 1708
Questions:
Find the output of the following: 1.main( ) { int x=100; if(!!x) printf(x=%d,!x); else printf(x=%d,x); } a. 0 b. 2 c. 1.5 d. 100
_____________________________________________________________________________________ 72 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
2. main( ) { float a=0.5, b=0.9; if(a&&b>0.9) printf(it is ur style); else printf(it is my style); } a. it is ur style b. it is our style c. it is my style d. no output 3. main( ) { int x=10, y=20; if(!(!x) && x) printf(x=%d,x); else printf(y=%d,y); } a. 10 b. 20 c. 1 d. 0 4. main( ) { char ch=291; printf(%d%d%c,32770,ch,ch); } a. 291 b. -32766 35# c. 32770chch d. 32770 5.main ( ) { int a,b; a = -3- -3; b = -3 - - (-3 ); printf(a=%d b= %d,a,b); } a. a=0 b=-6 b. a=-3 b=+3 c. a=-6 b=+6 d. a=6 b=0 6.main( ) { int x; x= -3 + 4 7 * 8 / 5 % 10;_____________________________________________________________________________________ 73 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
printf(x = %d,x); } a. 23 b. 6 c. 7
d. 0
7.main( ) { int x=3, y=4, z=4; printf(ans = %d, (z>=y>=x?100:200)); } a. 100 b. 300 c. 200 d. No Output 8.main( ) { int a=30, b=40, x; x=(a!=10) && (b=50); printf(x= %d,x); } a. 10 b. 50 c. 1 d. 0 9.main( ) { float x=12.25, y=13.65; if(x=y) printf(x and y are equal); else printf(x and y are not equal); } a. x and y are not equal b. x and y are equal c. No output 10.main ( ) { int i=1, j=1; for(;j;printf(%d%d\t,i,j)) j=i++ filename sfile first word on the command line, which is argv[0]. The command-line arguments are argv[1] dfile through argv[argc - 1]. ALGORITHM: step1: start Copy argv[1] to sfile step2: initialize two file pointers fs=source and fd = destination Copy argv[2] to dfile step3: if (agrc!=3) printf error step4: fs=fopen(argv[1],r) step5: fd=fopen(argv[2],w) Open sfile in read mode If sfpt==NULL Open dfile If argc!=3
step6: repeat step6 until not feof(fs) begin ch=fgetc(fs) fputc(ch,fd) end step7:fclose(fs) step8:fclose(fd) step9:stop
If dfpt==NULL While !feop(sfpt) Read c from sfpt Write c into dfpt
Flowchart:
Close sfile Close dfile Stop
_____________________________________________________________________________________ 81 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Yes No
Yes No
Yes No No Yes
Result:
_____________________________________________________________________________________ 82 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Input: Read file name at command line file1 file2 Output: File is copied to file 2 Questions: 1.what are the integer oriented functions in files---------------2.To read the characters from file which function is used--------3. To write the characters to file which function is used--------4..the difference between append and writing--------
12B. 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.)_____________________________________________________________________________________ 83 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
step1: start step2: initialize file pointer recfile step3: read no of chars from file (n) step4: recfile=fopen(filename,r) step5: if recfile=null print error in opening the file step6: fread(nchars,sizeof(char)*n,1,filename) step7: len=strlen(nchars) step8: repeat step8 until i filename sfile dfile if argc!=3 Yes
No Copy argv[1] to sfname_____________________________________________________________________________________ 84
Print MLR INSTITUTE OF TECHNOLOGY, DUNDIGALfile not found
Open sfname in read mode if sfpt==NULL No Convert argv[2] to integern = fread(text, l, sfpt)
Yes
text[n]=\0 l=length(text) j j=1 rev[j]=\0 j=j+1 for i=0;i= N) then Printf(STACK OVERFLOW) End if Step 2 : [ Increment Top ] Top Top +1 Step 3 : [Insert element] S[Top] x Step 4 : [finished] Return
Pop
Algorithm Pop(S[ ], Top) /* This function removes the top element from stack S and returns this element. Top is a pointer to indicate the top element of the stack */
Step 1 : [ Check for under flow on the stack ] If (Top = 0) then Printf(STACK UNDER FLOW) return End if Step 2 : [ Decrement Top ] Top Top - 1_____________________________________________________________________________________ 106 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Step 3 : [Return the popped element] Return( S[Top+1])
Display Algorithm Display(S[ ]) /* To Display the contents of the Stack S. Stack S is an array consisting of N elements. Top is a pointer variable to indicate the top element of the stack */ Step 1 : [ Check for the stack empty or not ] If (Top = 0) then Printf(STACK EMPTY) End if Step 2 : [ Display the stack elements ] Repeat for I = Top down to 1 Print(S[i]) [End Repeat] Step 3 : [Return the popped element] Stop
_____________________________________________________________________________________ 107 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Questions: 1. A stack is a linear data structure which data is stored and retrieved in a -------------2. A data structure needed to convert infix notation to postfix form ------------3. A stack is a special form of -----------4. Stack can be used to implement to ---------------5. A data structure uses in subroutine calls is -----------------
_____________________________________________________________________________________ 108 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
_____________________________________________________________________________________ 109 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
_____________________________________________________________________________________ 110 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Experiment: 16 Use functions to perform following OPERATIONS on Queues using arrays and pointers
AIM: Teaching the students how to perform operations on queues
Recommended systems/software requirements : Intel based desktop pc ANSI C compiler with supporting editors
Theory:
Queues: A Queue is an ordered collection of items from which items may be deleted at one end (called the front of the queue) and into which items may be inserted at the other end (the rear of the queue). Principle : First-In-First-Out (FIFO) The first element added to the queue will be the first one to be removed Operations : 1. Insertion: Inserts the item at the rear end of the queue. 2. Deletion: Removes the item at the front of a non-empty queue Representation of queue: front=F rear=R
_____________________________________________________________________________________ 111 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
F=-1 R=-1 Inserting elements into queue
10
F=0 R=0
10
20
F=0 10 20
R=1 30
F=0 10 20 30
R=2 40
F=0 Deleting elements from queue:
R=3
10
20
30
40
F=0
R=3
20
30
40
_____________________________________________________________________________________ 112 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
F=1 30
R=3 40
F=2 40
R=3
F=R=3
Queue underflow
. This idea is similar to customer lines at a in any bill payment store(eg. phone bill payment queue). When customer A is ready to check out, he or she enters the tail(end) of the waiting line. When the preceding customers have paid, then customer A pays and exits from the head of the line. The billpayment line is really a queue that enforces a "first come, first serve" policy. Pointer Implementation A second approach to creating a list is to link groups of memory cells together using the pointers. Each group of memory cells is called as a node. With this implementation every node contains the data item and the pointer to the next item in the list. You can picture this structure as a chain of nodes linked together by the pointers. As long as we know where the chain begins, we can follow the links to reach any item in the list. Often this structure is called as a linked list.
_____________________________________________________________________________________ 113 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Queue Insert operaton Algorithm QInsert(Q[ ],F,R,n, x) /* F and R are pointer to the front and rear elements of a queue, this queue Q is an array consisting of n elements and x is an element to insert at the rear end of the queue*/
Step 1 : [ Check for Queue overflow] If (R >= N) then Printf(QUEUE OVERFLOW) return End if Step 2 : [ Increment rear pointer ] R R +1 Step 3 : [Insert element] Q[R] x Step 4 : [Set front pointer, if necessary] If ( F = 0) then
_____________________________________________________________________________________ 114 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
F1 Return
Algorithm Qdelete(Q[ ], F,R) /* F and R the pointers to the front and rear elements of a queue. Last element is deleting from Q. x is a temporary element to hold the deleted element*/
Step 1 : [ Check for under flow ] If (F = 0) then Printf(Q UNDER FLOW) return End if Step 2 : [ Delete element ] x Q[F]_____________________________________________________________________________________ 115 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Step 3 : [Set the front pointer] If(F=R) then FR0 Else FF+1
Step 4 : [Return the deleted element] return(x)
PR
_____________________________________________________________________________________ 116 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Display Algorithm QDisplay(Q[ ]) /* F and R are the pointers to the front and rear elements of a queue Q. It is to display the elements of the Queue array Q */
Step 1 : [ Check for the queue empty or not ] If (R=F = 0) then Printf(QUEUE EMPTY) End if Step 2 : [ Display the queue elements ] Repeat for I = F to R Print(Q[i]) [End Repeat] Stop
Questions: 1. Queues serve a major role in --------2. Queue is served on the principle of ----------3. Queue can be used to implement -----------4. The process of accessing a data on a tape is similar to manipulating data on a ------5. A linear list of elements in which deletion can be done from one end (front) and insertion can be place at the other end is known as -----------
_____________________________________________________________________________________ 117 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Experiment: 17 Use stack operations 17A. to convert infix to postfix expression
AIM: Teaching the students how to evaluate postfix expression and convert infix to postfix expression
Recommended systems/software requirements : Intel based desktop pc ANSI C compiler with supporting editors
_____________________________________________________________________________________ 118 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
_____________________________________________________________________________________ 119 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
_____________________________________________________________________________________ 120 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
17B. evaluate postfix expressionEVALUATION OF A POSTFIX EXPRESSION Algorithm 6.3: This algorithm finds the VALUE of an arithmetic expression P written in postfix Notation. 1. Add a right parenthesis ) at the end of P. [This acts as a sentinel.] 2. Scan P from left to right and repeat Step 3 and 4 for each element of P until The sentinel ) is encountered. 3. If an operand is encountered, put it on STACK. 4. If an operator X is encountered, then: (a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element. (b) Evaluate B X A. (c) Place the result of (b) back on STACK. [End of If structure.] [End of Step 2 loop]. 5. Set VALUE equal to the top element on STACK. 6. Exit. Questions: 1. The postfix form of A+(B*C) is -----------2. The postfix form of A-B(C*D$E) is -------------3. The postfix form of A$B*C-D+E/F/*(G+H) is ------4. Which is the postfix form of the following prefix A-B/(C*D$E) is-----5. Which is the postfix form of the following prefix -A/B*C$DE is -------
_____________________________________________________________________________________ 121 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
_____________________________________________________________________________________ 122 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Experiment: 18 Write C programs that implement the following sorting methods to sort a given list of integers in ascending order: A) Bubble sortAIM: Teaching the students what is sorting ,where we are using the sorting techniques bubble sort and quick sort Recommended systems/software requirements : Intel based desktop pc ANSI C compiler with supporting editors
Theory:
a) Is a straightforward and simplistic method of sorting data that is used in computer science education. The algorithm starts at the beginning of the data set. It compares the first two elements, and if the first is greater than the second, it swaps them. It continues doing this for each pair of adjacent elements to the end of the data set. It then starts again with the first two elements, repeating until no swaps have occurred on the last pass. While simple, this algorithm is highly inefficient and is rarely used except in education. For example, if we have 100 elements then the total number of comparisons will be 10000. A slightly better variant, cocktail sort, works by inverting the ordering criteria and the pass direction on alternating passes. Its average case and worst case are both O(n).
10
9
8
11
4
_____________________________________________________________________________________ 123 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
ALGORI THM:
variable used in algorithm last=position of last unsorted element pass=pass counter exch=count no of exchanges made on any pass
step1: start step2: read n step3: repeat thru step3 until ia[i+1] interchange a[i]and a[i+1]
_____________________________________________________________________________________ 124 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
a[i]>a[i+1]exch=exch+1 end step6: if exch=0 then return else last=last-1 step7:print sorted array a[i] step8:finish
Result: Input:
Enter how many elements: 5 Enter the elements: 56 44 32 12 23
Output:
Sorted array is 12 23 32 44 56
_____________________________________________________________________________________ 125 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
Write C programs that implement the following sorting methods to sort a given list of integers in ascending order:
18B) Selection sortRecommended systems/software requirements : Intel based desktop pc ANSI C compiler with supporting editors
Theory: a) Selection sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:_____________________________________________________________________________________ 126 MLR INSTITUTE OF TECHNOLOGY, DUNDIGAL
simple implementation efficient for (quite) small data sets more efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such as selection sort or bubble sort: the average running time is n2/4, and the running time is linear in the best case stable (i.e., does not change the relative order of elements with equal keys) in-place (i.e., only requires a constant amount O(1) of additional memory space) online (i.e., can sort a list as it receives it)
Example: The following table shows the steps for sorting the sequence 5 7 0 3 4 2 6 1. On the left side the sorted part of the sequence is shown in red. For each iteration, the number of positions the inserted element has moved is shown in brackets. Altogether this amounts to 17 steps. 5 5 0 0 0 0 0 0 7 7 5 3 3 2 2 1 0 0 7 5 4 3 3 2 3 3 3 7 5 4 4 3 4 4 4 4 7 5 5 4 2 2 2 2 2 7 6 5 6 6 6 6 6 6 7 6 1 1 1 1 1 1 1 7 (0) (0) (2) (2) (2) (4) (1) (6)
ALGORITHM:
Step1: start Step2: read n Step3: repeat thru step3 until i
top related