FUNDAMENTALS OF COMPUTER SCIENCE LAB 2020-21.pdf

76
1 GOKARAJU RANGARAJU INSTITUTE OF ENGINEERING AND TECHNOLOGY (Autonomous) B. Tech I Year (CSBS Branch) FUNDAMENTALS OF COMPUTER SCIENCE LAB

Transcript of FUNDAMENTALS OF COMPUTER SCIENCE LAB 2020-21.pdf

1

GOKARAJU RANGARAJU INSTITUTE

OF ENGINEERING AND

TECHNOLOGY (Autonomous)

B. Tech I Year (CSBS Branch)

FUNDAMENTALS OF COMPUTER SCIENCE LAB

2

Name: ……………………………………….

Roll No: ……………Branch:....……………..

Section………………Year…………………..

GOKARAJU RANGARAJU

INSTITUTE OF ENGINEERING AND TECHNOLOGY

NIzampet Road, Bachupally , Hyderabad-500090

3

4

Preface The main objective of the course entitled “FUNDAMENTALS OF COMPUTER

SCIENCE LAB” manual is to make first year B.Tech CSBS students familiar

with the Computer Programming concepts and language fundamentals in a

more systematic manner. This material is written according to GRIET GR-

20(Autonomous) syllabus. This book has been prepared to meet the

requirements of Fundamentals of Computer Science Lab.

This work is Contributed and verified by the faculty of CPDS.

1. Dr. B. SrinivasaRao

2. S T G Y.Sandhya

3. K. Ch.Suneetha

4. A.Sravanthi

5. D. SugunaKumari

6. P. Rajesh

7. A.Sowmya

8. R.S.Shalini

9. M.Suresh Babu

GOKARAJU RANGARAJU INSTITUTE OF ENGINEERING AND

TECHNOLOGY

FUNDAMENTALS OF COMPUTER SCIENCE LAB

Course Code : GR20A1027 L/T/P/C:0/0/4/2

I Year I Semester

Course Objectives:

1. To gain a working knowledge of C programming to write modular, efficient and readable C

programs by Identifying the structural elements and layout of C source code.

2. To declare and manipulate single and multi-dimensional arrays of the C data types and

derived data types like structures, unions.

3. To use functions from the portable C library and to describe the techniques for creating

program modules using functions and recursive functions.

4. To manipulate character strings in C programs.

5. Utilize pointers to efficiently solve problems.

Course Outcomes: After the completion of the course, the student will be able to

1. Design algorithms and convert them to programs to solve simple problems.

2. Design, implement, debug a given problem using selection and looping constructs.

3. Implement programs using modular approach using functions and recursion.

4. Solve a given problem using C language arrays, strings and structures and pointers.

5. Implement various operations of files and make use of user defined libraries.

LIST OF EXPERIMENTS

Task-1 (Basic Programs):

a) Write a C program to implement operators in c?

b) Write a C program to find greatest and smallest among three numbers using

conditional operator.

c) Write a C program to implicit and explicit type conversion in c?

Task-2 (Basic Programs):

a) Write a C program to find the roots of a quadratic equation using if-else.

The program should request the user to input two numbers and display one of the

following as per the desire of user:

i. Sum of numbers

ii. Difference of numbers

iii. Product of the numbers

iv. Division of the numbers.

Write a C program using switch statement to accomplish the above task.

b) Write a C program to find the GCD of a given number.

Task-3 (Small but tricky codes):

a) Write a C program to find Maximum and minimum of two numbers without using

any loop or condition.

b) Write a C program to check if two numbers are equal without using arithmetic

operators or comparison operators.

Task-4 (Proper parameter passing):

a) Write a C program to swap two numbers using call by value.

b) Write a C program to add two numbers using call by reference.

Task-5 (Command line Arguments):

a) Write a C program to find sum of n numbers using command line arguments.

b) Write a C program to count the number of times a character occurs in a text file. The

file name and the character are supplied as command line arguments.

Task-6 (Variable parameter ):

a) Write a C program to demonstrate working of variable parameters to find average of

multiple numbers.

b) Write a C program using functions to accept n number of arguments using variable

length arguments. Return maximum of all values.

Task-7(Pointer to functions):

a) Write a c program using functions and pointers that compares two strings to see

whether they are identical. The function returns 1 if they are identical, 0 otherwise.

b) Write a C program that uses functions to perform the following:

i. Addition of Two Matrices

ii. Multiplication of Two Matrices

iii. Transpose of a matrix with memory dynamically allocated for the new matrix

as row and column counts may not be same.

Task-8 (User defined header):

a) Write a c program to implement following pre-processor directives.

(i) define (ii) ifdef (iii) undef (iv) ifndef.

b) Write a c program to create a user defined header file to find product and greatest of

two numbers.

Task-9 (Make file utility):

a) Write a C program to merge two files into a third file.

b) Write a C program to reverse the contents of a file and display it.

Task-10 (Multi file program and user defined libraries):

a) Write a c program to implement a multi file program to set and print the value of a

variable.

b) Write a C program to implement a multi file program to read, write and update a

student record containing the fields name, roll number, marks.

Task-11 (Interesting substring matching / searching programs):

a) Write a C program that uses functions to insert a sub-string in to a given main string

from a given position.

b) Write a C program that uses functions to delete n characters from a given position in

a given string.

Task-12 (Parsing related assignments):

a) Write a C program for implementing type checker.

b) Write a C program to implement predictive parser.

Index

SNo

TaskNo Pag

e

No

Dates Signature

1

a Write a C program to implement operators in c?

b Write a C program to find greatest and smallest among

three numbers using conditional operator.

c Write a C program to implicit and explicit type conversion

in c?

2

a Write a C program to find the roots of a quadratic equation using if-else.

b

The program should request the user to input two numbers

and display one of the following as per the desire of user:

i. Sum of numbers

ii. Difference of numbers

Write a C program using switch statement to accomplish

the above task.

iii. Product of the numbers

iv. Division of the numbers.

c Write a C program to find the GCD of a given number.

3

a Write a C program to find Maximum and minimum of two numbers without using any loop or condition.

b

Write a C program to check if two numbers are equal

without using arithmetic operators or comparison

operators.

4

a) Write a C program to swap two numbers using call by value.

b) Write a C program to add two numbers using call by reference

5

a) Write a C program to find sum of n numbers using command line arguments.

b) Write a C program to count the number of times a character

occurs in a text file. The file name and the character are supplied as

command line arguments.

6

a) Write a C program to demonstrate working of variable parameters

to find average of multiple numbers.

b) Write a C program using functions to accept n number of arguments using variable length arguments. Return maximum of all values.

7

a) Write a c program using functions and pointers that compares two

strings to see whether they are identical. The function returns 1 if

they are identical, 0 otherwise.

b) Write a C program that uses functions to perform the following:

i. Addition of Two Matrices

ii. Multiplication of Two Matrices

iii. Transpose of a matrix with memory dynamically allocated for the new matrix as row and column counts may not be same.

i. Addition of Two Matrices

ii. Multiplication of Two Matrices

iii. Transpose of a matrix with memory dynamically allocated for the new matrix as row and column counts may not be same.

8

a) Write a c program to implement following pre-processor directives.

(i) define (ii) ifdef (iii) undef (iv) ifndef.

b) Write a c program to create a user defined header file to find product and greatest of two numbers.

9

a) Write a C program to merge two files into a third file.

b) Write a C program to reverse the contents of a file and display it.

10

a) Write a c program to implement a multi file program to set and

print the value of a variable.

b) Write a C program to implement a multi file program to read,

write and update a student record containing the fields name, roll

number, marks.

11

a) Write a C program that uses functions to insert a sub-string in to a given main string from a given position.

b) Write a C program that uses functions to delete n characters from

a given position in a given string.

12

a) Write a C program for implementing type checker.

b) Write a C program to implement predictive parser.

LIST OF EXPERIMENTS

Task-1 (Basic Programs):

a) Write a C program to implement operators in c?

Algorithm:

Step1:Start

Step2:Read3numbersintoa,b,c.

Step3:DisplayArithmeticoperators andvalueofa+b,a-b,a/b,a%b,a*b.

Step4:DisplayRelationaloperators andvalueofa>b,a<b,a==b,a!=b,a>=b,a<=b.

Step5:Display Logicaloperatorsandvalueofa+b!=5&&a>=b,a<b||a==2 ,!(a==b).

Step6:Displayconditionaloperator andvalueofa>b?a:b.

Step7:DisplayIncandDecoperators andvalueof a++,++c,--b,c--.

Step8:DisplayAssignmentoperators andvalueof a+=5,c-=4.

Step9:Displayspecialoperators andvalueofsizeof(int),&a.

Step10:Display Bitwiseoperators andvalueofa&b,a|b,a^b,~a,a<<2,b>>2.

Step11:Stop

Flowchart:

Program:

Output:

b) Write a C program to find greatest and smallest among three numbers using

conditional operator.

Algorithm:

Step1:Start

Step2:Read three numbers into a,b,c.

Step3:Compute max=a>b?a>c?a:c:b>c?b:c;

Step4:Compute min=a<b?a<c?a:c:b<c?b:c;

Step5:Display max and min.

Step6:Stop

Flowchart:

Program:

OUTPUT:

c) Write a C program to implicit and explicit type conversion in c?

Algorithm:

Step1:Start

Step2:Read a charcter in ch.

Step3:Display ascii value of ch.

Step4:Read one integer,float,double,longint values into i,f,d,j.

Step5:Compute x=1/j+i*f-d.

c1/(float)j+i*f-d.

Step6:Display x,c.

Step7:Stop

Flowchart:

Program:

Output:

TASK 2(Basic Programs):

a) Write a C program to find the roots of a quadratic equation using if-else.

Algorithm:

Step1:start

Step2:read a,b,c values

Step3:computed=b*b-4*a*c

Step4:checkwheatherdvalueis equaltozeroif sogotostep7otherwisegotostep5

Step5:checkwhetherd>0ifsogotostep10otherwisegotostep6

Step6:displayroots areimaginaryandgotostep13

Step7:compute r1=-b/2*aandgotostep8

Step8:compute r2=-b/2*aandgotostep9

Step9:displayroots arerealanddisplayr1andr2values andgotosep13

Step10:compute r1=(-b+sqrt(d))/(2*a) andgotostep11

Step11:compute r2=(-b-sqrt(d))/(2*a)andgotostep12

Step12:displayroots arerealanddisplayr1andr2values andgotostep13

Step13:stop

Flowchart:

Program:

OUTPUT:

b) The program should request the user to input two numbers and display one of the

following as per the desire of user:

i. Sum of numbers

ii. Difference of numbers

iii. Product of the numbers

iv. Division of the numbers.

Write a C program using switch statement to accomplish the above task.

Algorithm

Step1:start

Step2:Readoption+or–or*or/or%

Step3:Reada,bvalue

Step4:ifoptionis + res=a+bopStep5:

ifoptionis- res=a-b Step6: ifoptionis *

res=a*b Step7: ifoptionis / res=a/b Step8:

ifoptionis- res=a%b

Step9:ifoptiondoes notmatchwith +or–or*or/or%

Thenprintinvalidoption

Step10:printa,b,result

Step11:stop

FlowChart:

Program:

OUTPUT:

b) Write a C program to find the GCD of a given number.

Algorithm:

Step1: Start

Step 2: intialize the variable i is 1

Step 3: Repeat loop till n1 and n2 is less than i

Step 4: Checks if i is factor of both n1 and n2.

Step 5: print i value as gcd.

Step 6: Stop.

Flow Chart:

Program:

OUTPUT:

TASK 3(Small but tricky codes):

a) Write a C program to find Maximum and minimum of two numbers without

using any loop or condition.

Algorithm:

Step1:Start.

Step2:Read two values .

Step3: Compute division of two numbers.

Step4: Depending upon the quotient one is maximum remaining is minimum.

Step5 :Stop.

Flow Chart:

PROGRAM:

OUTPUT:

b) Write a C program to check if two numbers are equal without using arithmetic

operators or comparison operators.

Algorithm:

Step1:Start.

Step2:Read two values .

Step3: Compute XOR of two numbers.

Step 4: if XOR is 0 then not equal.

Step 5:Otherwise equal.

Step 6: stop.

FLOWCHART:

PROGRAM:

OUTPUT:

TASK 4(Proper parameter passing):

a) Write a C program to swap two numbers using call by value.

ALGORITHM:

Step 1: Start the program.

Step 2: Set a ← 10 and b ← 20.

Step2a:Print a and b

Step 3: Call the function swap(a,b)

Step 3a: Start function.

Step 3b: Assign temp ← x.

Step 3c: Assign x ← y.

Step 3d: Assign y ← temp.

Step 3e: Print x and y.

FLOW CHART:

PROGRAM:

OUTPUT:

b) Write a C program to add two numbers using call by reference

ALGORITHM:

Step 1: Start the program.

Step 2: Read the values of x and y.

Step 3: Call the function swap(&x,&y)

Step 3a: Start fuction.

Step 3b: Assign sum ← (*a) + (*b)

Step 3c: Return sum

Step 3d: End function.

Step 4:Print sum.

FLOW CHART:

START

READ X AND Y

CALLING SWAP FUNCTION

PRINT SUM

STOP

sum<- (*a)+(*b)

Return sum

PROGRAM:

OUTPUT:

Task-5 (Command line Arguments):

a) Write a C program to find sum of n numbers using command line arguments.

Write a C program to count the number of times a character occurs in a text file. The file

name and the character are supplied as command line arguments.

Program:

OUTPUT:

Task-6 (Variable parameter):

a) Write a C program to demonstrate working of variable parameters to find average of

multiple numbers.

ALGORITHM:

START

Step 1 → Take an array num and define its values

Step 2 → Loop for each value of num

Step 3 → Add each element to 'sum' variable

Step 4 → After loop finishes, divide sum with number of array elements

Step 5 → Store that result to avg variable and display.

STOP

PROGRAM:

OUTPUT:

b) Write a C program using functions to accept n number of arguments using variable length

arguments. Return maximum of all values.

ALGORITHM:

Step 1: Declare variable length arguments function

Step 2: Test var-args with some sample values.

Step 3: Define Variable length arguments function int maximum(int num, ...)

Step 4: num is Total number of arguments passed.

... Variable length arguments

Step 5: Run loop from 1 to number of arguments passed

Step 6: Get next argument in list

Step 7:If current argument is greater than max, then store it in max.

Step 8: Finally return max argument in list

PROGRAM:

OUTPUT:

Task-7(Pointer to functions):

a)Write a c program using functions and pointers that compares two strings to see whether they are

identical. The function returns 1 if they are identical, 0 otherwise.

Program:

OUTPUT:

b) Write a C program that uses functions to perform the following:

i. Addition of Two Matrices

Algorithm:

Function:main()

Step1:Start

Step2:Readorderofmatrixaintor1,c1.

Step3:Readorderofmatrixbintor2,

c2.

Step4:Checkwhetherr1==c1ANDr2==c2if true goto step 5 otherwise goto step

9. Step5: Read(a,r1,c1).

Step6:Read(b,r2,c2).

Step7:Add_Matrix(a,b,c,r1,c1).

Step8:DisplaySUM MatrixC, Display(C,r1,c1).

Andgotostep10. Step9:DisplayMatrixadditionnotpossible.

Step10:Stop.

Function:Read(int*x,intr,intc

):

Step1:

Start

Step2:Allocatememorydynamicallyformatrix

a. x=(int*)calloc(r*c,sizeof(int)).

Step3:Se

ti=0.

Step4:Repeatsteps 4to9untilli<rotherwisegotostep10.

Step5:setj=0.

Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9.

Step7:Readelementinto*(x+i*c+j).

Step8:Incrementjb

y1.

Step9:Incrementib

y1..Step10:Stop.

Function:Add_Matrix(int*a, int*b, int*Cintr,intc):

tart

Step2:Allocatememorydynamicallyformatrixa.

C=(int*)calloc(r*c,sizeof(int)).

Step3:Seti=0.

Step4:Repeatsteps 4to9untilli<rotherwisegotostep10.

Step5:Setj=0.

Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9.

Step7:Addelementsinto*(C+i*c+j)=*(a+i*c+j)+*(b+i*c+j).

Step8:Incrementjby1.

Step9:Incrementiby1..Step10

:Stop.

Function:Display(int*x,intr,intc):

Step1:Start

Step2:Seti=0.

Step3:Repeatsteps 3to8untilli<rotherwisegotostep10.

Step4:setj=0.

Step5:Repeatsteps 5to7untillj<cotherwisegotostep 9.

Step6:Display elementin*(x+i*c+j).

Step7:Incrementjby1.

Step8:Incrementiby1..

Step9:Stop

PROGRAM:

OUTPUT:

ii. Multiplication of Two Matrices

Algorithm:

Function:main():

Step1:Start

Step2:Readorderofmatrixaintor1,c1.

Step3:Readorderofmatrixbintor2, c2.

Step4:Checkwhetherc1==r2iftruegotostep5otherwisegotostep9.

Step5:Read(a,r1,c1).

Step6:Read(b,r2,c2).

Step7:Multiply_Matrix(a,b,C,r1,c1,c2).

Step8:DisplayProductMatrixC,Display(C,r1,c2).Andgotostep10.

Step9:DisplayMatrixmultiplicationnotpossible.

Step10:Stop.

Function:Read(int*x,intr,intc):

Step1:Start

Step2:Allocatememorydynamicallyformatrixa.

x=(int*)calloc(r*c,sizeof(int)).

Step3:Seti=0.

Step4:Repeatsteps 4to9untilli<rotherwisegotostep10.

Step5:setj=0.

Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9.

Step7:Readelementinto*(x+i*c+j).

Step8:Incrementjby1.

Step9:Incrementiby1..Ste

p10:Stop.

Function:Multiply_Matrix(int*a,int*b, int*C,intr1,intc1,int c2):

Step1:Start

Step2:AllocatememorydynamicallyformatrixC.

C=(int*)calloc(r1*c2,sizeof(int)).

Step3:Seti=0.

Step4:Repeatsteps 4to12untilli<r1otherwisegotostep10.

Step5:setj=0.

Step6:Repeatsteps 6to11untillj<c2otherwisegotostep9.

Step7:Set*(C+i*c2+j)=0,k=0.

Step8:Repeatsteps 8to10untillk<c1otherwisegotostep11.

Step9:Multiplyelementsinto*(C+i*c2+j)+=*(a+i*c2+j)**(b+i*c2+j).

Step10:Incrementkby1.

Step11:Incrementjby1.

Step12:Incrementiby1.

Step13:Stop.

Function:Display(int*x,intr,intc):

Step1:Start

Step2:Seti=0.

Step3:Repeatsteps 3to8untilli<rotherwisegotostep10.

Step4:setj=0.

Step5:Repeatsteps 5to7untillj<cotherwisegotostep 9.

Step6:Displayelementsin*(x+i*c+j).

Step7:Incrementjby1.

Step8:Incrementiby1.

Step9:Stop

PROGRAM:

OUTPUT:

iii. Transpose of a matrix with memory dynamically allocated for the new matrix as row and

column counts may not be same.

Algorithm:

Function:main():

Step1:Start

Step2:Readorderofmatrixaintor, c.

Step3:Read(a,r,c).

Step4:Trnspose_Matrix(a,b,r,c).

Step5:DisplayTransposeMatrixb,Display(b,c,r).

Step6:Stop.

Function:Read(int*x,intr,intc):

Step1:Start

Step2:Allocatememorydynamicallyformatrixa. x=(int*)calloc(r*c,sizeof(int)).

Step3:Seti=0.

Step4:Repeatsteps 4to9untilli<rotherwisegotostep10.

Step5:setj=0.

Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9.

Step7:Readelementinto*(x+i*c+j).

Step8:Incrementjby1.

Step9:Incrementiby1..

Step10:Stop.

Function:Transpose_Matrix(int*a,int*b, intr,intc):

Step1:Start

Step2:Allocatememorydynamicallyformatrixb. b=(int*)calloc(r*c,sizeof(int)).

Step3:Seti=0.

Step4:Repeatsteps 4to9untilli<rotherwisegotostep10.

Step5:setj=0.

Step6:Repeatsteps 6to8untillj<cotherwisegotostep 9

. Step7: Transposeelementsinto*(b+j*r+i)=*(a+i*c+j).

Step8:Incrementjby1.

Step9:Incrementiby1.

Step10:Stop.

Function:Display(int*x,intr,intc):

Step1:Start

Step2:Seti=0.

Step3:Repeatsteps 3to8untilli<rotherwisegotostep10.

Step4:setj=0.

Step5:Repeatsteps 5to7untillj<cotherwisegotostep 9.

Step6:Displayelementsin*(x+i*c+j).

Step7:Incrementjby1.

Step8:Incrementiby1..Step9:

Stop.

PROGRAM:

OUTPUT:

Task-8 (User defined header):

a) Write a c program to implement following pre-processor directives.

(i) define

Algorithm:

Step 1: Start

Step 2: Define PI 3.142

Step 3: Read radius R

Step 4: Compute ar=PI*R*R;

Step 5: Display area of Circle ar.

Step 6: Stop.

Program:

OUTPUT:

(ii) ifdef

Algorithm:

Step 1: Start

Step 2: check ifdef ABC then

Step 2.1: undef ABC

Step 2.2: Define ABC 500

Step 3: Otherwise

Step 3.1: Define ABC 100

Step 4: Display ABC.

Step 5: Stop.

Program:

OUTPUT:

(iii) undef

Algorithm:

Step 1: Start

Step 2: Define MAX 10

Step 3: Undef MAX

Step 4: Define MAX 20

Step 5: Display MAX.

Step 6: Stop.

Program:

OUTPUT:

(iv) ifndef.

Algorithm:

Step 1: Start

Step 2: check ifndef ABC then

Step 2.1: Define ABC 500

Step 3: Otherwise undef ABC

Step 3.1: Define ABC 100

Step 4: Display ABC.

Step 5: Stop.

Program:

OUTPUT:

b) Write a c program to create a user defined header file to find product and greatest of two

numbers.

Program:

OUTPUT:

Task-9 (Make file utility):

a) Write a C program to merge two files into a third file.

Algorithm:

Step1:Open file1.txtandfile2.txtinreadmode.

Step2:Open file3.txtinwritemode.

Step3:Runalooptoonebyonecopycharacters offile1.txttofile3.txt.

Step4:Runalooptoonebyonecopycharacters offile2.txttofile3.txt.

Step5:Closeallfiles.

PROGRAM:

OUTPUT:

b) Write a C program to reverse the contents of a file and display it.

Task-10(Multi file program and user defined libraries):

a)Write a c program to implement a multi file program to set and print the value of a

variable.

Algorithm:

Step 1: Define the functions in two separate .c files(A.c and B.c) then put their prototypes in

the corresponding headers(A.h and B.h).

Step 2:In a .c file,when we need to use the functions defined in another .c, you

will #include the corresponding header; then we will be able to use the functions normally.

Step 3: All the .c and .h files must be added to the program.

PROGRAM:

OUTPUT:

c)Write a C program to implement a multi file program to read, write and update a student

record containing the fields name, roll number, marks.

Algorithm:

Step 1: Define the functionmain.c and then put its prototype in the corresponding header

struct.h

Step 2:Inmain.c file,enter student name,rollnumber and three subject marks.

Step 3: All the .c and .h files must be included to the program.

Step 4: Display student name,rollnumber and three subject marks.

PROGRAM:

OUTPUT:

Task-11(Interesting substring matching / searching programs):

a) Write a C program that uses functions to insert a sub-string in to a given main string from a

given position.

Algorithm:

Step1:start

Step2:Declarethetwocharacter arrays str,str1

Step3:DeclarethevariableposSte

p4:ReadtheinputstringstrStep5:

Readthesubstring str1

Step6:ReadthepositionposStep

7:Callthefunctioninsert

Step8:End.

AlgorithmInsert(arguments)

Step1: start

Step2: Declarethevariablesi,j,k

Step3: Declareacharacterarraystr2

Step4: Initializei=0

Step5: Repeatthestep5.1tilli<pos

5.1str2[i]=str[i]

Step6: Initializek=i,j=0

Step7: Repeatthestep7.1tillj<strlen(str1)

7.1str2[k]=str1[j]

Step8: Initializei=pos

Step9: Repeatsteps 9.1to9.2tilli<strlen(str)

9.1str2[k]=str[i];

9.2k++;

Step10: Assignnulltostr2[k];

Step11: Displaythe resultantstringstr2

Step12: stop

PROGRAM:

OUTPUT:

b) Write a C program that uses functions to delete n characters from a given position in a given

string.

Algorithm:

Step1:start

Step2:DeclareacharacterarraysstrSte

p3:Declarethevariables pos,n

Step4:Readtheinputstringstr

Step5:Readthesubstring str1

Step6:Readthepositionposandnumberofcharacterstobedeletedn

Step7:Callthefunctiondel

Step8:End.

Algorithmdel(arguments)

Step1:Declarethevariablesi,j,k

Step2:Declareacharacterarraystr1

Step3:Initializei=0

Step4:Repeatthestep4.1tilli<pos

4.1str1[i]=str[i]

Step5:Initializej=pos+n

Step6:Repeatthestep6.1to6.2tillj<strlen(str)

6.1str1[i]=str[j]

6.2i++

Step7:Assignnulltostr1[i];

Step8:Displaythe resultantstringstr1

Step9:end

PROGRAM:

OUTPUT:

Task-12(Parsing related assignments):

a)Write a C program for implementing type checker.

ALGORITHM:

Step1: Track the global scope type information.

Step2: Determine the type of expressions recursively, i.e. bottom-up, passing the

resulting types upwards.

Step3: If type found correct, do the operation

Step4: Type mismatches, semantic error will be notified

PROGRAM:

OUTPUT:

b) write a C program to implement predictive parser.

Algorithm:

Step1. Input Buffer

It consists toindicateendofinput. Step 2.Stack

Itconsist to indicate end of stack.

Step 3. Driver Routine

It is a function that drives the Parser.

Step 4. Parser Table

• It determines the actions to be carried out by the parser

• Predictive parsing uses a stack and a parsing table to parse the input and generate

a parse tree.

• Both the stack and the input contains an end symbol $to denote that the stack is

empty and the input is consumed.

• The parser refers to the parsing table to take any decision on the input and stack

element combination.

Step 5:Rulesfor designing Predictive Parser:

• Make the grammar suitable for top-down parser. By performing the elimination of left

recursion. And by performing left factoring.

• Find the FIRST and FOLLOW of the variables

• Design predictive parser table

Step 6:Construction of a predictive syntax-directed translator.

PROGRAM:

OUTPUT: