/TE/I/CG/Practical/1-7Computer Engineering / TE / I / CG /
Practical / 1-7Applied Science / FE / I / CP / Practical / 1-12Applied
Science / FE / I / CP / Practical / 2-12Computer Engineering / SE / I / DC /
Practical / 8-8
20013
201431
g
Computer ProgrammingGraphics
PracticalExperiment No
.:
. 182
1
Line generation using DDA and Bresenhams
algorithmImplementation of ChecksumProgram
Forfor finding largest from 3 numbers using
Nested-if statementbasic arithmetic operations
Goal:
20013
201431
Control Statements
--
Selection Statements
if
if-else
nested if-else
else-if
switch case
Iterative Statements
for loop
while loop
do-while loop
break
continue
go-to
20013
201431
Standard Functions in Cmain()- It indicates the compiler where program begins, all statements
are written after main in curly bracket.
printf()- It used to print output or data.
scanf()-It is used to get the input
A large number of if else statements are required in the complex
programs so they are nested inside each other which is called nested if
else statement. Syntax is as follows
if(condition)
{
if(condition)
{
statements;
}
else
{
statements
}
}
else
If(condition)
{
{
Statements
If(condition)
{
}
Statements;
}
Else(condition)
{
Statements;
}
Design:
DDA Algorithm
}
Start
Else
Start
{ variables a, b, c
Initialize the
Statements;
Get the input
from user
}
If a is greater than b and c, print
the Initialize
variable aPerform addition print
Declare
Sum
variables a, b, c
Perform subtraction print Difference
If b is greater than a, find b is greater
than
Input
fromc, if so print the variable b
Perform Multiplication print Product user
Perform Division print Division Else print the variable CResult
Stop
True
False
if
a>b
True
True
False
False
if
if a>c
b>c
Input
Input
Input
fromPrint
fromPrint
fromPrint
Bresenhams algorithStudy
of
b user
aLine
usergeneration using DDA and
c user
20013
201431
Types of Error
Single bit error- Only one bit in the data unit has changed.
Burst error- Two or more bits in the data unit have changed.
Error DetectionIn error detection we are looking only to see if any error has occurred. The
answer is simple yes or no. We are not interested in number of errors. The
most common methods used for error detection are simple parity, two
dimensional parity, CRC, and checksum.
Checksum
The checksum is higher level error detecting method used by layers.
Suppose we want to send our data which is a list of 5 numbers to
destination. In addition to sending these numbers we will send sum of the
numbers.
The receiver adds original 5 numbers and checksum if the result is zero
then data is accepted otherwise rejected. Following are the steps to
calculate checksum
Steps involving ChecksumSender Side1. Add all the numbers which you want to send and calculate sum of the
numbers
2. Represent calculated sum into binary format
3. Calculate wrapping sum if the number of bits in calculated sum are
greater than the number of bits in all the numbers which we are
sending.
4. The final sum is complemented and becomes the checksum
F
Flow Chart
20013
201431
Receiver side-
1. Add all the numbers including checksum and calculate new sum of the
numbers
2. Represent calculated sum into binary format
3. Calculate wrapping sum if the number of bits in calculated sum are
greater than the number of bits in all the numbers which are received
4. Complement the final sum
5. If the result is zero, the data are accepted; otherwise rejected
Line generation using DDA and Bresenhams algorithStudy of
Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement
20013
201431
Perform
Addition
20013
201431
20013
201431
Perform Division
Stop
Perform
Multiplication
Perform
Subtraction
Print
Product
Print
Print Division
Result
Difference
20013
201431
Implementation:
Source code of the Program in C/C++
For line drawing, the Bresenhams algorithm is better that that of DDA as it is
faster and more accurate.By using basic C fundamentals Thus we can develop a
program for basic arithmetic operations.detect single bit error using checksum
method
Line generation using DDA and Bresenhams algorithStudy of
Networking CablesmProgram for basic Arithmetic Operationfinding
largest of 3 numbers using nested-if statement
20013
201431
References:
1. B. A. Forouzan, "Data communication and networking", Third Edition TMH
2. ISRD group, Data communication and computer network, TMH
1. Reference:
[1] P K Sinha, "Computer Fundamentals" 4th Edition, BPB Publication, PP:184-189
E Balagurusamy, "Programming in ANSI",5th Edition, Tata Mc Graw Hill PP:23114-12539
David F. Rogers, Procedural Elements for Computer Graphics, Tata McGraw Hill, 2nd Ed.ition
ppPage No: 67,, 76
10
20013
201431
11
20013
201431
Source Code
C /C++ Source code:
//Program for basic Arithmetic Operationsfinding
numbers using nested if-else statement
largest
of
three
#include<stdio.h>
main()
{
int a,b,c;
clrscr();
printf("Enter the numbers\n");
printf("a=");
scanf("%d",&a);
printf("b=");
scanf("%d",&b);
//declare variables
//clear screen
//input
//input
c=a+b;
printf("\nSum=%d",c);
//perform addition
c=a-b;
printf("\nDifference=%d",c);
//perform substraction
c=a*b;
printf("\nProduct=%d",c);
//perform multiplication
c=a/b;
printf("\nDivision=%d",c);
//perform division
getch();
}
/*
** pgm Program for generationtion of line style
** DDA & Bresenhams algorithm
**
*/
#
#
#
#
#
#
#
include
include
include
include
include
include
include
by using
<stdio.h>
<graphics.h>
<math.h>
<conio.h>
<stdlib.h>
<bios.h>
<dos.h>
12
20013
201431
void DDA();
void BRA();
float sign(float k);
int Bitmap();
float sign(float k)
{
float j;
if(k<0) j=-1.0;
if(k==0)j=0.0;
if(k>0) j=1.0;
return j;
}
int bitmap(int no,int bit[100])
{
int i;
//Bitmap method
DDA Algo.");
Bresenhams Algo.");
exit");
your choice");
switch(ch)
{
case 1:
DDADda_Method();
break;
case 2:
BRABrasenham_Method();
break;
case 3:
exit(0);
13
20013
201431
}
//switch
}
//main
void DDA()
//defination of DDA() function
{
float x,y,x1,y1,x2,y2,dx,dy,e,Length;
int i,gd,gm;
int no,bit[100],x100;
clrscr();
/*Read end points of line */
printf("\n\t Enter the first point x1 \n\t");
scanf("%f",&x1);
printf("\n\t Enter the first point y1 \n\t");
scanf("%f",&y1);
printf("\n\t Enter the first point x2 \n\t");
scanf("%f",&x2);
printf("\n\t Enter the first point x2 \n\t");
scanf("%f",&y2);
clrscr();
detectgraph(&gd,&gm);
//IntialiseInitialize the graphics mode
initgraph(&gd,&gm,"c:\\tc\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
{
}
else
{
Length=dx;
Length=dy;
}
dx=(x2-x1)/Length;
dy=(y2-y1)/Length;
x=x1+sign(dx);
y=y1+sign(dy);
14
20013
201431
Bitmap (no,bit[100]);
i=1;
while(i<=Length)
{
x100=(int)x%no;
putpixel(x,y,14*bit[x100]);
x=x+dx;
y=y+dy;
i=i+1;
delay(10);
}//while
getch();
closegraph();
}
void BRA()
//defination of BRA() function
{
int x,y,x1,y1,x2,y2,dx,dy;
int e,m;
int i,gd,gm;
clrscr();
printf("\n\t Enter the first point x1 \n\t");
//Read end points of line
scanf("%d",&x1);
printf("\n\t Enter the first point y1 \n\t");
scanf("%d",&y1);
printf("\n\t Enter the first point x2 \n\t");
scanf("%d",&x2);
printf("\n\t Enter the first point x2 \n\t");
scanf("%d",&y2);
clrscr();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
e=m-0.5;
15
20013
201431
for(i=1;i<=dx;i++)
{
putpixel(x,y,14);
while(e>0)
{
y=y+1;
e=e-1;
}
x=x+1;
e=e+m;
}
getch();
closegraph();
}
float sign(float k)
// defination of sign function
{
float j;
if(k<0)
j=-1.0;
if(k==0)
j=0.0;
if(k>0)
j=1.0;
return j;
}
int Bitmap(int no,int bit[100])
//Bitmap method
{
int i;
printf("\n How many no of pattern");
scanf("%d",&no);
printf("\n\t Enter the pattern(0/1)");
for(i=0;i<no;i++)
{
scanf("%d",&bit[i]);
}
return;
}
16
20013
201431
3.
17