Professional Documents
Culture Documents
LAB MANUAL
ACADEMIC YEAR: 2016-2017
EXPT.
Problem statement Revised on
NO
Group A Assignments
1. Using Divide and Conquer Strategies design a function for Binary 15/06/2016
Search using C++/ Java/ Python/Scala.
2. Using Divide and Conquer Strategies design a class for 15/06/2016
Concurrent Quick Sort using C++.
3. Lexical analyzer for sample language using LEX. 15/06/2016
5. Intermediate code generation for sample language using LEX and 15/06/2016
YACC.
6. A6 :Based on Elective-I (Appendix I) 15/06/2016
Group B Assignments
P:F-LTL-UG/03/R1 Page 1
8. Write a program to implement SLR Parsing algorithm using 15/06/2016
Python for the ordered input Set in XML { P E, EE+T,
ET, TT*F, TF, F(E), Fi }
9. B9: Based on Elective-I (Appendix I) 15/06/2016
Group C Assignments
1. Code generation using iburg tool. 15/06/2016
P:F-LTL-UG/03/R1 Page 2
Revised on: 15/06/2016
1. Problem definition
P:F-LTL-UG/03/R1 Page 3
Aim
Using Divide and Conquer strategy design a function to implement binary search .
Prerequisites
Learning Objectives
Learning Outcome
Related Mathematics
Mathematical Model
Consider S as a system
s = start state = {}
x = {L, n, key}
|x| = U l + 1
INPUT: F(X) X
X=X U xi
P:F-LTL-UG/03/R1 Page 4
if key = x(u + l)/2 Empty
Success: Successful output represent success which is either number found or not
found.
Binary search can be effectively performed through divide and conquer strategy,
which reduces searching time. But for binary search it is mandatory that list must
be sorted.
Conquer: - Conquer the sub problems by solving them recursively. If the sub
problem sizes are small enough, then just solve the sub problems in a straight
forward manner.
Combine: - Combine the solutions to the sub problems to form the solution
for the original problem.
Else
P:F-LTL-UG/03/R1 Page 5
}
g(n) is the time to compute the answer directly for small inputs.
f(n) is the time for dividing P and combining the solutions of sub problems.
= aT(n/b)+f(n) n>1
Conclusion:
P:F-LTL-UG/03/R1 Page 6
Revised on: 15/06/2016
PROBLEM Using Divide and Conquer Strategies design a class for Concurrent
STATEMENT Quick Sort using C++.
/DEFINITION
S/W PACKAGES AND 1. Operating Systems (64-Bit) 64-BIT Fedora 17 or latest 64-BIT
Update of Equivalent Open source OS or latest 64-BIT Version and
HARDWARE update of Microsoft Windows 7 Operating System onwards.
APPARATUS USED
2. Programming Tools (64-Bit) Latest Open source update of Eclipse
Programming frame work, GTK+.
P:F-LTL-UG/03/R1 Page 7
Aim
Using Divide and Conquer strategy design a class for Quick sort.
Prerequisites
Learning Objectives
Learning Outcome
Related Mathematics
Mathematical Model
Consider S as a system
s = start state = {}
X = {L, n}
INPUT: f(L) Y
PERMUTE={p | p is a permutation of x}
Div (p)=(p1,p2)
P:F-LTL-UG/03/R1 Page 8
S.T |p|=|p1|+|p2|+1 & p1(l)=p(l) & p2(r)=p(r) & p1(r)<p2(l) & x p1 ,yp2 ,
x<=y
Y = {X n/2, X p, X n/2} S.T {elements of(X n/2) < (X p) < elements of (X n/2) }
Quick sort can be effectively performed through divide and conquer strategy, which
reduces searching time.
P:F-LTL-UG/03/R1 Page 9
Computation time for divide and conquer strategy
T(n) = g(n) when n is small
= T(n1)+ T(n1)+ T(n2)+.+ T(nk)+ f(n) otherwise
T(n) denotes the time for DAndC on any input of size n.
g(n) is the time to compute the answer directly for small inputs.
f(n) is the time for dividing P and combining the solutions of sub problems.
T(n)= T(1) n=1
= aT(n/b)+f(n) n>1
An introduction to threads:
With multithreading we are able to run multiple blocks of independent code
(functions) parallel with main() function.
Consider a thread a function running independent and simultaneous with the rest of
the code, and therefore not interfering with the execution order of 'main' program.
Initially, all C++ programs contain a single, default thread. All other threads must be
explicitly created by the programmer. 'normal' single threaded piece of code :
#include <iostream>
extern "C"
{
#include <unistd.h>
}
using namespace std;
void function1();
void function2();
int main()
{
function1();
function2();
return 0;
}
void function1()
{
cout << "hello..." << endl ;
P:F-LTL-UG/03/R1 Page 10
sleep(2); // fall asLeep here for 2 seconds...
}
void function2()
{
cout << " ....world" << endl ;
Compiling
If you want to compile the above code you need to include the pthread library. In
linux (using the GCC) use:
g++ program.cpp -pthread
Creating a thread
Using pthread_create() we can create multiple threads. We can use this call any
number of times and from anywhere in the code (also inside another thread). When a
thread is created it will be send to OS, which then schedules it for execution.
Parameters: -
thread : The unique identifier for the thread. This identifier has to be of type
pthread_t.
attr: This is an object which you can create for the thread with specific attributes for
the thread. It can be NULL if we want to use the default attributes.
start_routine: The function that the thread has to execute.
arg: The function argument. If we don't want to pass an argument, set it to NULL
returns :0 on success, some error code on failure.
Joining threads:
P:F-LTL-UG/03/R1 Page 11
Parameters :
th: The unique identifier for the thread (as specified by pthread_tunique_identifier).
Conclusion:
Thus, after successfully completing this assignment, you should be able to understand
and implement concurrent Quick sort with divide and conquer strategy.
P:F-LTL-UG/03/R1 Page 12
Revised on: 15/06/2016
TITLE
Lexical analyzer for HLL using LEX
PROBLEM
STATEMENT Lexical analyzer for sample language using LEX.
/DEFINITION
Understand the importance and usage of LEX automated tool
OBJECTIVE Appreciate the role of lexical analysis phase in compilation
Understand the theory behind design of lexical analyzers and
lexical analyzer generator
S/W PACKAGES Linux OS (Fedora 20), 64-bit Fedora or equivalent OS with 64-bit
AND Intel-i5/ i7 or latest higher processor computers, LEX, YACC
HARDWARE
APPARATUS
USED
1. A V Aho, R. Sethi, .J D Ullman, "Compilers:
Principles, Techniques, and Tools", Pearson Education, ISBN 81
REFERENCES
- 7758 - 590
2. J. R. Levine, T. Mason, D. Brown, "Lex & Yacc",
O'Reilly, 2000, ISBN 81-7366 -061-X. 8
3. K. Louden, "Compiler Construction: Principles and
Practice", Thomson Brookes/Cole (ISE), 2003, ISBN 981 - 243 -
694-4:page no.31-90
STEPS Refer theory, mathematical model,algorithm, test input, test
output
INSTRUCTIONS 1. Title
FOR 2. Problem Definition
WRITING 3. Theory and mathematical model
JOURNAL 4. State transition diagram
5. Algorithm
6. Source Code
7. Output
8. Conclusion
P:F-LTL-UG/03/R1 Page 13
Theory:
Definitions
%%
Rules
%%
User Subroutines
The definition section is the place to define macros and to import header files
written in C. It is also possible to write any C code here, which will be copied
verbatim into the generated source file.
The rules section is the most important section; it associates patterns with C
statements. Patterns are simply regular expressions. When the lexer sees some
text in the input matching a given pattern, it executes the associated C code. This
is the basis of how lex operates.
The C code section contains C statements and functions that are copied
verbatim to the generated source file. These statements presumably contain code
called by the rules in the rules section. In large programs it is more convenient to
place this code in a separate file and link it in at compile time.
How the input is matched? :
When the generated scanner is run, it analyzes its input looking for strings which
match any of its patterns. If it finds more than one match, it takes the one matching
the most text (for trailing context rules, this includes the length of the trailing part,
even though it will then be returned to the input). If it finds two or more matches of
the same length, the rule listed first in the flex input file is chosen.
Once the match is determined, the text corresponding to the match (called the token)
is made available in the global character pointer yytext, and its length in the global
integer yyleng. The action corresponding to the matched pattern is then executed (a
more detailed description of actions follows), and then the remaining input is scanned
for another match.
If no match is found, then the default rule is executed: the next character in the input
is considered matched and copied to the standard output. Thus, the simplest legal flex
P:F-LTL-UG/03/R1 Page 14
input is: which generates a scanner that simply copies its input (one character at a
time) to its output.
Actions in lex :
The action to be taken when a RE is matched can be a C program fragment or the
special actions described below; the program fragment can contain one or more C
statements, and can also include special actions.
[] - a character class which matches any character within the brackets. If the
first character is a circumflex '^' it changes the meaning to match any
character except the ones within brackets.
{} - Indicates how many times the previous pattern is allowed to match, when
containing one or two numbers.
P:F-LTL-UG/03/R1 Page 15
$ - matches the end of a line as the last character of a regular expressions
\ - used to escape metacharacters and a part of usual c escape sequences e.g '\n'
is a newline character while '\*' is a literal asterisk.
Examples :
DIGIT [0-9]+
IDENTIFIER [a-zA-Z][a-zA-Z0-9]*
int yylex(void) : Performs lexical analysis on the input; this is the primary function
generated by the lex utility. The function shall return zero when the end of input is
reached; otherwise, it shall return non-zero values (tokens) determined by the actions
that are selected.
int yymore(void):When called, indicates that when the next input string is
recognized, it is to be appended to the current value of yytext rather than
replacing it; the value in yyleng shall be adjusted accordingly.
int main(int argc, char *argv[]): Calls yylex() to perform lexical analysis, then exits.
The user code can contain main() to perform application-specific operations, calling
yylex() as applicable.
Mathematical Model :
S= {St , E , I , O , F, DD , NDD}
P:F-LTL-UG/03/R1 Page 16
Where,
St is an initial state such that St={Fl | Fl is lex file}. At initial state, system consists of
one lex file with three sections.
E is a End state. E={Sc, Fc | Sc = success case which gives the lexemes-token table,
symbol table entries and Fc = failure case which gives the lexical errors}.
I= set of inputs I={Fi | Fi is an input file which consists of High Level Language
code}.
O= set of outputs
O={Op, Ot, Os, | Op is program after preprocessing i.e. after removing comments,
white spaces, Ot is lexeme-token table, Os is symbol table}.
Function f1: I O
P:F-LTL-UG/03/R1 Page 17
Function f2: I O
Function f3: I O
DD=Deterministic Data
P:F-LTL-UG/03/R1 Page 18
State Transition Diagram:
Algorithm:
2. Separate the tokens as identifiers, constants, keywords etc. and fill the
generic symbol table.
Test Input:
#include<stdio.h>
void main()
int a,b;
char bilateral;
a=3;
c=a+d;
Test Output:
Lexeme Token
#include<stdio.h> Directive
void Reserved
main() Reserved
{ Punctuation
int Keyword
a Identifier
P:F-LTL-UG/03/R1 Page 19
b Identifier
; Delimiter
char Reserved
; Delimiter
a Identifier
= Operator
3 Constant
; Delimiter
c Identifier
= Operator
a Identifier
+ Operator
d Identifier
; Delimiter
} Punctuation
Symbol Table:
FAQs
1. What is the data structure used in Lexical phase of compiler
2. What are lexical errors
Practice Problem Statements:
1. Implement lexical analyser for JAVA along-with error handling
P:F-LTL-UG/03/R1 Page 20
2. Write a lex program to find character, word and line count from multiple files.
[Hint: Use yywrap() to handle EOF condition of first file and connect to
second file.]
3. Write a lex program to remove single and multiple line comments using state
concept.
4. Write a lex program that capitalizes all reserved words outside the comments
in a C program.
5. Write a program that takes R.E. and a filename as input and produces as
output all lines of file that contain a substring denoted by R.E.
6. Write a program to add error recovery routine or scheme for lex program to
enable it to continue to look for tokens in presence of errors.
P:F-LTL-UG/03/R1 Page 21
Revised on: 15/06/2016
TOPIC for
PROBLEM Parser for sample language using YACC.
STATEMENT
/DEFINITION
INSTRUCTIONS 1. Title
FOR 2. Problem Definition
WRITING 3. Theory and mathematical model
JOURNAL
4. State transition diagram
5. Algorithm
6. Source Code
7. Output
8. Conclusion
P:F-LTL-UG/03/R1 Page 22
Theory:
Lex recognizes regular expressions, whereas YACC recognizes entire grammar. Lex
divides the input stream into tokens, while YACC uses these tokens and groups them
together logically.
%{
declaration section
%}
rules section
%%
%%
Declaration section:
Here, the definition section is same as that of Lex, where we can define all tokens and
include header files. The declarations section is used to define the symbols used to
define the target language and their relationship with each other. In particular, much
of the additional information required to resolve ambiguities in the context-free
grammar for the target language is provided here.
The rules section defines the context-free grammar to be accepted by the function
Yacc generates, and associates with those rules C-language actions and additional
precedence information. The grammar is described below, and a formal definition
follows.
The rules section is comprised of one or more grammar rules. A grammar rule has the
form:
A : BODY ;
P:F-LTL-UG/03/R1 Page 23
If there are several successive grammar rules with the same left-hand side, the vertical
bar | can be used to avoid rewriting the left-hand side; in this case the semicolon
appears only after the last rule. The BODY part can be empty.
Programs Section
The programs section can include the definition of the lexical analyzer yylex(), and
any other functions; for example, those used in the actions specified in the grammar
rules. It is unspecified whether the programs section precedes or follows the
semantic actions in the output file; therefore, if the application contains any macro
definitions and declarations intended to apply to the code in the semantic actions, it
shall place them within "%{ ... %}" in the declarations section.
representing the kind of token read. If there is a value associated with the
Link the two files together using gcc y.tab.c lex.yy.c lfl
Mathematical Model :
S= { St , E , I , O , F, DD , NDD}
Where, St is an initial state such that St={Fl,Fy | Fl is lex file and Fy is a yacc file}.
At initial state, system consists of two files, each with three sections.
E is a End state.
E={Sc, Fc | Sc = success case which shows that input is syntactically correct and
I= set of inputs
P:F-LTL-UG/03/R1 Page 24
O={Op, Ot, Os, Oc | Op is program after preprocessing i.e. after removing
comments, white spaces, Ot is lexeme-token table, Os is symbol table, Oc is console
output which mention that syntax of statements of input program is correct or not}.
Function f1: I O
Function f2: I O
P:F-LTL-UG/03/R1 Page 25
Function f3: I O
Function f4: I O
DD=Deterministic Data
P:F-LTL-UG/03/R1 Page 26
State Transition Diagram:
Algorithm :
I am a boy
Test Output:
Test Input:
I boy am
Test Output:
FAQs
1. Which is the file generated as output as a result of YACC translation
2. How LEX communicates with YACC
3. Is there any other parser generator/ syntax analysis generator other than
YACC?
4. What is state machine?
5. Which phase is the driver of compiler?
6. How to debug YACC specification file
P:F-LTL-UG/03/R1 Page 27
Practice Problem Statements:
1. Write a lex and yacc program for parsing .asm file ( assembly language).
2. Write a lex and yacc program for parsing SQL statements which are in file
[insert , update, select and delete statements]
3. Write a lex and yacc program for parsing class definition in java [which may
include implementation of interface and inheritance].
4. Write a lex and yacc program for parsing html file along with counting
opening and closing tags
P:F-LTL-UG/03/R1 Page 28
Revised on: 15/06/2016
INSTRUCTIONS 1. Title
FOR 2. Problem Definition
WRITING 3. Theory and mathematical model
JOURNAL
4. State transition diagram
5. Algorithm
6. Source Code
7. Output
8. Conclusion
P:F-LTL-UG/03/R1 Page 29
THEORY:
Introduction
In the analysis-synthesis model of a compiler, the front end analyzes a source program
and creates an intermediate representation, from which the back end generates target
code. Ideally, details of the source language are confined to the front end, and details
of the target machine to the back end. The front end translates a source program into
an intermediate representation from which the back end generates target code. With a
suitably defined intermediate representation, a compiler for language i and machine j
can then be built by combining the front end for language i with the back end for
machine j. This approach to creating suite of compilers can save a considerable
amount of effort: m x n compilers can be built by writing just m front ends and n back
ends.
1. Compiler for a different machine can be created by attaching a back end for the
new machine to an existing front end.
Syntax tree
Postfix notation
The semantic rules for generating three-address code from common programming
language constructs are similar to those for constructing syntax trees or for generating
post-fix notation.
Three-address code:
Each statement generally consists of 3 addresses, 2 for operands and 1 for result.
P:F-LTL-UG/03/R1 Page 30
The use of names for the intermediate values computed by a program allows three
address codes to be easily rearranged unlike post-fix notation.
6. param x and call p, n for procedure calls and return y, where y representing a
returned value is optional. For example,
param x1
param x2
P:F-LTL-UG/03/R1 Page 31
9. Implementation of Three-Address Statements: A three-address statement is an
abstract form of intermediate code. In a compiler, these statements can be
implemented as records with fields for the operator and the operands. Three
such representations are: Quadruples, Triples, Indirect triples.
A. Quadruples:
A quadruple is a record structure with four fields, which are, op, arg1, arg2
and result.
The op field contains an internal code for the operator. The 3 address
statement x = y op z is represented by placing y in arg1, z in arg2 and x in
result.
The contents of fields arg1, arg2 and result are normally pointers to the
symbol-table entries for the names represented by these fields. If so,
temporary names must be entered into the symbol table as they are created.
B. Triples:
To avoid entering temporary names into the symbol table, we might refer to a
temporary value by the position of the statement that computes it.
The fields arg1 and arg2, for the arguments of op, are either pointers to the
symbol table or pointers into the triple structure ( for temporary values ).
Since three fields are used, this intermediate code format is known as triples.
(0) uminus c t1
(1) * b t1 t2
(2) uminus c t3
(3) * b t3 t4
(4) + t2 t4 t5
P:F-LTL-UG/03/R1 Page 32
(5) := t5 a
Fig(a)
Mathematical Model :
S= {St , E , I , O , F, DD , NDD}
Where, St is an initial state such that St={Fl,Fy | Fl is lex file and Fy is a yacc file}.
At initial state, system consists of two files, each with three sections.
E is a End state.
I= set of inputs
F={f1 | f1:I O, function f1 generates the three-address code from input program}
Function f1: I O
DD=Deterministic Data
P:F-LTL-UG/03/R1 Page 33
DD={x | x is HLL's syntax which includes brackets, keywords, variables, functions
definitions etc.}
ALGORITHM:
Write a LEX and YACC program to generate Intermediate Code for arithmetic
expression
LEX program
1. Declaration of header files specially y.tab.h which contains declaration for Letter,
Digit, expr.
4. If match found then convert it into char and store it in yylval.p where p is pointer
declared in YACC
5. Return token
11. End.
YACC program:
2. Declare structure for three address code representation having fields of argument1,
argument2, operator, result.
P:F-LTL-UG/03/R1 Page 34
3. Declare pointer of char type in union.
8. If final expression evaluates then add it to the table of three address code.
12. Declare main function and call yyparse function untill yyin ends
Test Input:
main(){
int x,y,z,i;
x=5;
i=6;
y=8;
z=1;
P:F-LTL-UG/03/R1 Page 35
if(x<=7){
i=i+1+y*z;
if(x==1){
y=3;
if(x<=2){
y=2;
else{
x=5;
else{
i=i-1-y*z;
while(x>0){
x=x-1;
if(x>=7){
i=i+1+y*z;
else{
i=i-1-y*z;
while(x>0){
x=x-1;
}
P:F-LTL-UG/03/R1 Page 36
}
$./a .out
Output:
0 = 5 x
1 = 6 i
2 = 8 y
3 = 1 z
4 <= x 7 t0
5 IF t0 0 20
6 + i 1 t1
7 * y z t2
8 + t1 t2 t3
9 = t3 i
10 == x 1 t4
11 IF t4 0 17
12 = 3 y
13 <= x 2 t5
14 IF t5 0 16
15 = 2 y
16 GOTO 19
17 ELSE
P:F-LTL-UG/03/R1 Page 37
18 = 5 x
19 GOTO 25
20 ELSE
21 - i 1 t6
22 * y z t7
23 - t6 t7 t8
24 = t8 i
25 > x 0 t9
26 WHILE t9 0 30
27 - x 1 t10
28 = t10 x
29 GOTO 26
30 >= x 7 t11
Similarly .
Symbol Table:
i int
z int
y int
x int
FAQs
4. Which representation of 3-address code is better than other and why? Justify.
P:F-LTL-UG/03/R1 Page 38
Practice Problem Statements:
P:F-LTL-UG/03/R1 Page 39
IP Revised on: 15/06/2016
PROBLEM Design a class using C++ to read a gray scale TIFF image
STATEMENT file of a dental digital X-Ray or Medical X-Ray or an
/DEFINITION Areal view Image, design Class to calculate histogram to
return a CList. Design Image Display class to display
histogram of a image.
To learn image file formats such as TIFF
OBJECTIVE To implement program using C++ for reading,
displaying image and histogram
1.Operating Systems (64-Bit)64-BIT Fedora 17 or latest
64-BIT Update of Equivalent Open source OS or latest
S/W PACKAGES AND
64-BIT Version and update of Microsoft Windows 7
HARDWARE Operating System onwards
APPARATUS USED
2. Programming Tools (64-Bit), Latest Open source
update of Eclipse Programming frame work, GTK+
REFERENCES Malay K. Pakhira, Digital Image processing and Pattern
Recognition,PHI, 2014,ISBN-978-81-203-4091-6.
Jayaraman, "Digital Image Processing" Mc Graw Hill
1. Problem definition
INSTRUCTIONS FOR 2. Learning objective
P:F-LTL-UG/03/R1 Page 40
Aim
Design a class using C++ to read a gray scale TIFF image file of a dental digital X-
Ray or Medical X-Ray or an Areal view Image, design Class to calculate histogram
to return a CList, Design Image Display class to display histogram of a image.
Prerequisites
Learning Objectives
Learning Outcome
After successfully completing this assignment, you should be able to understand and
implement different line drawing functions in Java
Related Mathematics
Mathematical Model
Let us consider S as a system for displaying a gray scale image with TIFF format, and
display its histogram.
S=start state
E=end state
S =Start state
E=End state
X= {x |x [0 ---255] N}
Y = {count}
P:F-LTL-UG/03/R1 Page 41
NDD = Non deterministic data; here the image format other than TIFF format is
invalid
1. Operations performed :
{ fread ,fdisplay ,fhisto ,fcount }
For A, B, C, D, E: Array
P:F-LTL-UG/03/R1 Page 42
4 F2(change_pixel(X, Y))= B' = {d| 'd' consists the
B' absolute value of change in
pixel parameters}
I= {L} | t > 0
fe(L) | B'.
(dx1,dy1)
(dx2,dy2)
(dx3,dy3)
.
.
.
fe(L) | C'.
(x1,y1) m1
(x2,y2) m2
(x3,y3) m3
. .
. .
. .
.
6 .
F4(change_interval(X, Y)) D' = {dX, dY} | t > 0 Reusable
= D' component
fe(X, .Y) | D'.
P:F-LTL-UG/03/R1 Page 43
7 F5(get_Newpixel (X, Y)) E' = {X', Y'} | t > 0
= E'
fe(X, Y) | E'.
(x1,y1)
(x2,y2) (x1',y1')
(x3,y3) (x2',y2')
. (x3',y3')
. .
. .
.
.
.
.
Success: Finally, we can display the TIFF image and draw a histogram for the TIFF
grayscale image.
Failure:
Image formats other than TIFF are used, histogram and image can not be displayed.
4. Concepts related Theory
Histogram:
Histogram of an image , like other histograms also shows frequency. But an image
histogram , shows frequency of pixels intensity values. In an image histogram, the x
axis shows the gray level intensities and the y axis shows the frequency of these
intensities.
The x axis of the histogram shows the range of pixel values. Since its an 8 bpp image ,
that means it has 256 levels of gray or shades of gray in it. Thats why the range of x
axis starts from 0 and end at 255 with a gap of 50. Whereas on the y axis , is the count
of these intensities.
P:F-LTL-UG/03/R1 Page 44
Applications of Histograms:
Histograms have many uses in image processing. The first use as it has also been
discussed above is the analysis of the image. We can predict about an image by just
looking at its histogram. Its like looking an x ray of a bone of a body.
The second use of histogram is for brightness purposes. The histograms have wide
application in image brightness. Not only in brightness, but histograms are also used
in adjusting contrast of an image.
And last but not the least; histogram has wide use in thresholding. This is mostly used
in computer vision.
TIFF Library:
LIBTIFF is a C graphic library provides support for the Tag Image File Format
(TIFF), a widely used format for storing image data. This library provides means to
easily access and create TIFF image files. LIBTIFF will be the supported image
manipulating package supported in the class.
P:F-LTL-UG/03/R1 Page 45
1. Setting Up The VC++ 6.0 Project To Use LIBTIFF
To use LIBTIFF in your existing VC++ 6.0 Project/WorkSpace, you need to do the
following setting modifications:
The following steps will make the compiler use correct method to generate the
machine code:
By carrying out the following steps, we tell the linker to link in the correct libtiff
library file.
P:F-LTL-UG/03/R1 Page 46
2. Using LIBTIFF in your code.
2. #include <tiffio.h>
1. You must open the file with the following line to read the tiff file:
TIFF *tif=TIFFOpen("input.tif", "r");
2. Then you must get the size of the image from the opened file:
#define uint32 unsigned long
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
// uint32 width;
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
// uint32 height;
3. You should use the LIBTIFF library memory allocation functions to
get space to store the image:
uint32 npixels=width*h;
raster=(uint32 *) _TIFFmalloc(npixels *sizeof(uint32));
[Note: Though it works under Win32, you should not use the
C-"malloc/free" and the C++-"new/delete" functions/operators
to do the memory management for the reading buffer]
4. Now you are able to read the image from the TIFF file using the
following function:
TIFFReadRGBAImage(tif, width, height, raster, 0);
Note: You should always check the return value of the function to
make sure that there is no error occurred in the reading process.
Returning 1 means read was successful, and if 0 is returned, some type
of error occured.
What you have now is an array (raster) of pixel values, one for each
pixel in the image, and each pixel is a 4-byte value. Each of the bytes
represent a channel of the pixel value (RGBA). Each channel is
represented by an integer value from 0 to 255.
P:F-LTL-UG/03/R1 Page 47
char X=(char )TIFFGetX(raster[i]); // where X can be the channels R,
G, B, and A.
// i is the index of the pixel in the raster.
Important: Remember that the origin of the raster is at the lower left
corner. You should be able to figure out the how the image is stored in
the raster given that the pixel information is stored a row at a time!
5. After you are finished with the raster you should destroy it and free up
the memory:_TIFFfree(raster);
6. Always Close the File when you are done with it. TIFFClose(tif);
7.1 You must open the file with the following line to write the tiff file,
this will create the file if it does not exist, and replace the file if it
does.
7.2 TIFF *out= TIFFOpen("new.tif", "w");
7.3 Your image data should be store in some type of array of char's.
int sampleperpixel = 4; // or 3 if there is no alpha channel,
you should get a understanding of alpha in class soon.
7.4 Now we need to set the tags in the new image file, and the essential
ones are the following:
TIFFSetField (out, TIFFTAG_IMAGEWIDTH, width); // set
the width of the image
TIFFSetField(out, TIFFTAG_IMAGELENGTH, height);
// set the height of the image
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL,
sampleperpixel); // set number of channels per pixel
TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); //
set the size of the channels
TIFFSetField(out, TIFFTAG_ORIENTATION,
ORIENTATION_TOPLEFT); // set the origin of the
image.
// Some other essential fields to set that you do not have to
understand for now.
TIFFSetField(out, TIFFTAG_PLANARCONFIG,
PLANARCONFIG_CONTIG);
P:F-LTL-UG/03/R1 Page 48
TIFFSetField(out, TIFFTAG_PHOTOMETRIC,
PHOTOMETRIC_RGB);
7.5 We will use most basic image data storing method provided by the
library to write the data into the file, this method uses strips, and
we are storing a line (row) of pixel at a time. This following code
writes the data from the char array image into the file:
tsize_t linebytes = sampleperpixel * width; // length in
memory of one row of pixel in the image.
7.6 Finally we close the output file, and destroy the buffer
(void) TIFFClose(out);
if (buf)
_TIFFfree(buf);
7.7 One suggestion for working with the image data is to convert the
raster structure into the char image[] structure for image analysis
and manipulations.
7.8 With the above example you should be able to have the basic way
of read and write TIFF image files.
P:F-LTL-UG/03/R1 Page 49
Trouble Shooting.
1. Error that indicate LIBTIFF library include/header file (.h files) not found
during compilation
2. You did not set the "Directories" "Include Files" path correctly for the
LIBTIFF library, refer to steps 1.3-1.6
3. Error that indicate unresolved function definitions, or error during linking
process
4. You did not correctly add the dlibtiff.lib wsock32.lib to the "Link" option in
the Project-Setting, refer to steps 1.15-1.19
You did not correctly set the path for the library files for LIBTIFF, refer to
steps 1.12-1.15.
5. Other compile errors
1. Most likely caused by some problem in your code.
2. Error/Crash during the execution of your program
3. Most likely caused by some problem in your code.
4. The linker complains:
5. LINK : warning LNK4098: defaultlib "LIBCMTD" conflicts with
use of other libs; use /NODEFAULTLIB:library
This means that you did not compile your program with the correct
code generation settings, described above, refer to steps 1.7-1.11
Class Diagram :
Histogram
+(count, intensities)
+DrawGraph(count)
Conclusion:
Thus, after successfully completing this assignment, you should be able to understand
& Implement different line drawing functions in C/ C++.
P:F-LTL-UG/03/R1 Page 50
CNDM Revised on: 15/06/2016
TITLE Network design and implementation in NS3
PROBLEM A company has three offices at remote locations with
STATEMENT requirement of interoperability with remote services.
/DEFINITION Each office has a server, TCP/IP and different users
including administrator, privileged users and common
clients. Design a network model for the same.
Demonstrate the network model using NS3
OBJECTIVE To understand the installation and working of NS3.
1. Operating Systems
S/W PACKAGES AND (64-Bit)64-BIT Fedora 17 or latest 64-BIT Update of
Equivalent Open source OS or latest 64-BIT Version
HARDWARE
APPARATUS USED 2. Programming Tools (64-Bit)
Latest Open source update of Eclipse Programming
frame work, GTK+
3. Programming languages like C++, Python, etc.
4. ns-allinone-3.15 or latest version and netanim
5. editors like gedit, vi editor, etc.
REFERENCES George F. Riley, Thomas R. Henderson,
Modeling and Tools for Network
Simulation, Springer Berlin Heidelberg,
2010, ISBN 978-3-642-12330-6
William Stallings, Wireless Communications &
Networks, 2/E, Phi Learning Pvt Ltd, 2009,
ISBN-13: 978-8120330191
Kaveh Pahlavan, Prashant Krishnamurthy,
Principles of Wireless Networks, Prentice Hall,
2002, ISBN: 0-13-093003-2
1. Problem definition
INSTRUCTIONS FOR 2. Learning objective
WRITING JOURNAL 3. Learning Outcome
4. Related Mathematics
5. State Transition Diagram
6. Concepts related Theory
7. Program code with proper documentation.
8. Output of program. Conclusion and applications
(the verification and testing of outcomes).
P:F-LTL-UG/03/R1 Page 51
Aim :
Design and implement a network of 3 servers and multiple users using wireless
networks over ns-3 simulator.
Prerequisites :
Learning Objectives :
Related Mathematics
Mathematical Model
Let us consider S as a system of designing a network
I= Set of inputs = {}
DD = Deterministic data;
Operations performed:
P:F-LTL-UG/03/R1 Page 52
SetConstantPosition( ) = sets a position of nodes on the animator.
NetDeviceContainer( ) = has list of networking devices which can be
used.
InternetStackHelper( ) = has list of protocols which can be used.
Ipv4AddressHelper( ) = setting up of Ipv4 connectivity.
UdpEchoClientApplication( ) = applications list for client.
UdpEchoServerApplication( ) = applications list for server.
= Final state
O= {O1, O2, O3, On |O outcome of operations performed by the system
which displays transmission of packets from intended nodes}
Success:
Finally, we can design and implement desired network after performing all
operations defined above.
Failure:
Nodes will not be able to transfer data packets in animator window.
P:F-LTL-UG/03/R1 Page 53
State Transition Diagram
START
Check output
Check / Modify
End
A wireless network is any type of computer network that uses wireless data
connections for connecting network nodes.
P:F-LTL-UG/03/R1 Page 55
Wireless WAN- Wireless wide area networks are wireless networks
that typically cover large areas, such as between neighboring towns
and cities, or city and suburb.
Interference
Multipath fading
Capacity
Conclusion:
P:F-LTL-UG/03/R1 Page 56
DMTA Revised on: 15/06/2016
P:F-LTL-UG/03/R1 Page 57
Aim
Write a program using C/C++ a simple approach for k-means/ k-medoids clustering
Prerequisites
Learning Objectives
Learning Outcome
Related Mathematics
Mathematical Model
Let us consider S as a start state with initial instances of data.
S = Start state
K = {k | k N, no of clusters.}
I = {X, K}
DD = Deterministic data;
Operations performed :
P:F-LTL-UG/03/R1 Page 58
assign_cluster(X, C) = B'; calculate the nearest cluster center to each
Xi.
Cj { mj | center of cluster Cj }
if min{ d(Xi , m1), d(Xi , m2), , d(Xi , mk) } = d(Xi , mj)
then Xi mj
Xj { Xj,1 , Xj,2 , , Xj,p | p attributes of Xj }
mj { mj,1 , mj,2 , , mj,p | p attributes of mj }
d(Xi , mj) = | Xi,1 mj,1 | + | Xi,2 mj,2 | + + | Xi,p mj,p |
update_cluster(C) = C'; calculate the new cluster center.
= {m | no of instances within cluster}
Iteration of cluster assigning procedure occurs till terminating
condition is met.
Termination condition could be maximum number of iteration to meet
or less cluster variance.
Output:
Eucledian distance is used to find distance between instance of data and cluster
center. Cluster center is updated by taking mean between instances of data within
cluster.
Conclusion:
Thus, after successfully completing this assignment, you should be able to understand
& Implement k - mean clustering technique to partition instance data into k clusters in
C++.
P:F-LTL-UG/03/R1 Page 59
ACP Revised on: 15/06/2016
P:F-LTL-UG/03/R1 Page 60
Aim
Write a java program to multiply 64-bit numbers using shared memory, java
collection framework and java utilities.
Prerequisites
Learning Objectives
Learning Outcome
Related Mathematics
Mathematical Model
Let
b1i {0,1}
1i64
b2i {0,1}
1i64
Success
P:F-LTL-UG/03/R1 Page 61
Students are able to multiply 64 bit number in Java using shared memory
Failure
Incorrect result of multiplication
Concepts related Theory
In Java, there is no official API to create a shared memory segment. In this case
you need to resort to a helper library/DDL and JNI to use shared memory to have
two Java processes talk to each other.
In practice, this is rarely an issue since Java supports threads, so you can have two
"programs" run in the same Java VM. Those will share the same heap, so
P:F-LTL-UG/03/R1 Page 62
communication will be instantaneous. Plus you can't get errors because of
problems with the shared memory segment.
Class Diagram
JavaMultiplication
+multiplier1, multiplier2
+acceptInput()
+performMultiplication()
Conclusion
+writeToMemoryMapped
File() shall be able to understand shared memory concept & shall be able to
Students
implement 64-bit numbers multiplication in Java.
P:F-LTL-UG/03/R1 Page 63