Happy Codings - Programming Code Examples

C++ Programming Code Examples

C++ > Computer Graphics Code Examples

Micky Mouse Program

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/* Micky Mouse Program */ #include<graphics.h> #include<dos.h> #include<conio.h> #include<alloc.h> void *buf; void firstleft(); void secondleft(); void main() { int gd=DETECT,gm,i=0,x,y,area; initgraph(&gd,&gm,"tc:\bgi");\put your directory contains egavga.bgi rectangle(0,0,getmaxx(),getmaxy()); arc(240,120,40,140,70); ellipse(165,80,10,280,20,20); ellipse(315,80,-100,170,20,20); arc(235,120,163,215,70); arc(245,120,-35,17,70); ellipse(193,178,85,280,40,20); ellipse(283,178,-100,95,40,20); ellipse(238,199,180,0,39,50); ellipse(213,123,44,240,33,40); ellipse(262,123,-60,135,33,40); ellipse(210,123,0,360,13,20);//left eye ellipse(265,123,0,360,13,20);//right eye ellipse(210,133,0,360,10,10);//left eye ball ellipse(265,133,0,360,10,10);//right eye ball ellipse(210,133,0,360,3,3);//left eye ball ellipse(265,133,0,360,3,3);//right eye ball ellipse(238,160,0,360,10,13);//nose arc(240,125,228,312,68);//mouth arc(240,120,230,310,72);//mouth setfillstyle(1,4); floodfill(238,160,15);//nose setfillstyle(1,15); floodfill(210,113,15); floodfill(265,113,15); setfillstyle(1,9); floodfill(210,100,15); setfillstyle(1,1); floodfill(315,80,15); moveto(203,220); lineto(203,260); lineto(183,260); lineto(183,350); lineto(293,350); lineto(293,260); lineto(273,260); lineto(273,220); moveto(183,350); lineto(173,460); lineto(213,460); lineto(238,400); lineto(263,460); lineto(303,460); lineto(293,350); moveto(173,460); lineto(143,478); lineto(213,478); lineto(213,460); moveto(263,460); lineto(263,478); lineto(333,478); lineto(303,460); line(238,400,238,350); //right hand moveto(183,260); lineto(113,310); lineto(183,375); moveto(183,280); lineto(137,310); lineto(181,353); setfillstyle(2,13); floodfill(190,300,15); setfillstyle(1,5); floodfill(223,400,15); setfillstyle(1,5); floodfill(253,400,15); setfillstyle(1,6); floodfill(173,470,15); floodfill(303,470,15); //fingers secondleft(); ellipse(413.5,228,0,180,3.5,3.5); line(420,240,433,240); line(423,247,440,247); line(413,240,410,228); line(417,228,420,240); ellipse(433,243.5,-90,90,3.5,3.5); line(423,254,440,254); ellipse(440,250.5,-90,90,3.5,3.5); ellipse(430,257,-90,90,3,3); line(413,260,430,260); area=imagesize(409,224,444,261); buf=malloc(area); getimage(409,224,444,261,buf); while(!kbhit()) { if(i==0) { setfillstyle(1,15); setcolor(15); ellipse(210,133,0,360,10,10);//left eye ball ellipse(265,133,0,360,10,10);//right eye ball setcolor(0); ellipse(210,133,0,360,3,3);//left eye ball ellipse(265,133,0,360,3,3);//right eye ball floodfill(210,133,15); floodfill(265,133,15); setcolor(0); putimage(391,209,buf,1); firstleft(); setcolor(15); secondleft(); putimage(409,224,buf,0); i=1; } else { setfillstyle(1,0); setcolor(0); ellipse(210,133,0,360,10,10);//left eye ball ellipse(265,133,0,360,10,10);//right eye ball floodfill(210,133,0); floodfill(265,133,0); setcolor(15); ellipse(210,133,0,360,3,3);//left eye ball ellipse(265,133,0,360,3,3);//right eye ball floodfill(210,133,15); floodfill(265,133,15); setcolor(0); putimage(409,224,buf,1); secondleft(); setcolor(15); firstleft(); putimage(391,209,buf,0); i=0; } delay(300); } getch(); } void firstleft() { moveto(293,260); lineto(353,276); lineto(395,223); moveto(293,280); lineto(355,296); lineto(395,245); } void secondleft() { moveto(293,260); lineto(363,280); lineto(413,240); moveto(293,280); lineto(363,300); lineto(413,260); }
delay() Function in C++
delay() function is used to hold the program's execution for given number of milliseconds, it is declared in dos.h header file. There can be many instances when we need to create a delay in our programs. C++ provides us with an easy way to do so. We can use a delay() function for this purpose in our code. We can run the code after a specific time in C++ using delay() function.
Syntax for delay() Function in C++
void delay(unsigned int milliseconds);
milliseconds
how many milliseconds to delay The function takes one parameter which is unsigned integer. Here, void suggests that this function returns nothing. 'delay' is the function name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* hold the program's execution for given number of milliseconds by delay() function code example. */ #include<iostream.h> #include<dos.h> //for delay() #include<conio.h> //for getch() int main() { clrscr(); int n; cout<<"Enter the delay (in seconds) you want to make after giving input."<<endl; cin>>n; delay(n*1000); cout<<"This has been printed after "<< n <<" seconds delay"; getch(); return 0; }
putimage() Function in C++
putimage puts the bit image previously saved with getimage back onto the screen, with the upper left corner of the image placed at (left,top). bitmap points to the area in memory where the source image is stored. The op parameter to putimage specifies a combination operator that controls how the color for each destination pixel onscreen is computed, based on the pixel already onscreen and the corresponding source pixel in memory.
Syntax for putimage() Function in C++
#include <graphics.h> void putimage(int left, int top, void *bitmap, int op);
left
X coordinate of top left corner of the specified rectangular area
top
Y coordinate of top left corner of the specified rectangular area
bitmap
pointer to the bitmap image in memory
op
operator for putimage. The enumeration putimage_ops, as defined in graphics.h, gives names to these operators. • COPY_PUT 0 Copy • XOR_PUT 1 Exclusive or • OR_PUT 2 Inclusive or • AND_PUT 3 And • NOT_PUT 4 Copy the inverse of the source In other words, COPY_PUT copies the source bitmap image onto the screen, XOR_PUT XORs the source image with the image already onscreen, OR_PUT ORs the source image with that onscreen, and so on. This function does not return any value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/* In C++, the putimage() function is used to put the bit image that is saved with getimage() function back to screen. */ /* put the bit image to screen by putimage() function code example */ #include<graphics.h> #include<conio.h> #include<stdlib.h> main() { int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75; void *p; initgraph(&gd,&gm,"C:\\TC\\BGI"); setcolor(YELLOW); circle(50,100,25); setfillstyle(SOLID_FILL,YELLOW); floodfill(50,100,YELLOW); setcolor(BLACK); setfillstyle(SOLID_FILL,BLACK); fillellipse(44,85,2,6); fillellipse(56,85,2,6); ellipse(50,100,205,335,20,9); ellipse(50,100,205,335,20,10); ellipse(50,100,205,335,20,11); area = imagesize(left, top, left + 50, top + 50); p = malloc(area); setcolor(WHITE); settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2); outtextxy(155,451,"Smiling Face Animation"); setcolor(BLUE); rectangle(0,0,639,449); while(!kbhit()) { temp1 = 1 + random ( 588 ); temp2 = 1 + random ( 380 ); getimage(left, top, left + 50, top + 50, p); putimage(left, top, p, XOR_PUT); putimage(temp1 , temp2, p, XOR_PUT); delay(100); left = temp1; top = temp2; } getch(); closegraph(); return 0; }
imagesize() Function in C++
The header file graphics.h contains imagesize() function which returns the number of bytes required to store a bit-image. This function is used when we are using getimage. imagesize() function returns the required memory area to store an image in bytes. imagesize() function returns the number of bytes needed to store the top-left corner of the screen at left, top and the bottom-right corner at right, bottom. This function is usually used in conjunction with the getimage() function. The imagesize() function only works in graphics mode.
Syntax for imagesize() Function in C++
unsigned int imagesize(int left, int top, int right, int bottom);
left
X coordinate of top left corner
top
Y coordinate of top left corner
right
X coordinate of bottom right corner
bottom
Y coordinate of bottom right corner left, top, right, and bottom define the area of the screen in which image is stored.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/* imagesize() function returns the number of bytes required to store a bitimage. */ /* get the number of bytes required to store a bit-image by imagesize() function code example. */ #include <graphics.h> #include <stdio.h> // driver code int main() { // gm is Graphics mode which is // a computer display mode that // generates image using pixels. // DETECT is a macro defined in // "graphics.h" header file int gd = DETECT, gm, color, bytes; char arr[100]; // initgraph initializes the // graphics system by loading a // graphics driver from disk initgraph(&gd, &gm, ""); // Draws a circle with center at // (200, 200) and radius as 50. circle(200, 200, 50); // draws a line with 2 points line(150, 200, 250, 200); // draws a line with 2 points line(200, 150, 200, 250); // imagesize function bytes = imagesize(150, 150, 250, 250); // sprintf stands for "String print". // Instead of printing on console, // it store output on char buffer // which are specified in sprintf sprintf(arr, "Number of bytes required " "to store required area = %d", bytes); // outtext function displays text // at current position. outtextxy(20, 280, arr); getch(); // closegraph function closes the // graphics mode and deallocates // all memory allocated by // graphics system . closegraph(); return 0; }
getimage() Function in C++
getimage() function copy a specific portion into memory. This specific image would be any bit image like rectangle, circle or anything else. getimage() copies an image from the screen to memory. Left, top, right, and bottom define the screen area to which the rectangle is copied. Bitmap points to the area in memory where the bit image is stored. The first two words of this area are used for the width and height of the rectangle; the remainder holds the image itself.
Syntax for getimage() Function in C++
#include <graphics.h> void getimage(int left, int top, int right, int bottom, void *bitmap);
left
X coordinate of top left corner
top
Y coordinate of top left corner
right
X coordinate of bottom right corner
bottom
Y coordinate of bottom right corner
bitmap
points to the area in memory where the bit image is stored getimage() function saves a bit image of specified region into memory, region can be any rectangle. This function does not return any value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* getimage() function code example copies an image from the screen to memory. */ /* save a bit image of the specified region displayed on the screen into memory by getimage() function code example. */ #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT, gm,size; char *buff; initgraph(&gd,&gm," "); outtextxy(100,80,"Original image:"); rectangle(100,200,200,275); size=http://www.web.com/imagesize(100,200,200,275); buf=malloc(size); getimage(100,200,200,275,buf); outtextxy(100,320,"Captured image:"); putimage(100,340,buf,COPY_PUT); getch(); closegraph(); }
ellipse() Function in C++
Ellipse is used to draw an ellipse (x,y) are coordinates of center of the ellipse, startangle is the starting angle, end angle is the ending angle, and fifth and sixth parameters specifies the X and Y radius of the ellipse. To draw a complete ellipse strangles and end angle should be 0 and 360 respectively.
Syntax for ellipse() Function in C++
#include <graphics.h> void ellipse(int x, int y, int startangle, int endangle, int xradius, int yradius);
x
x-coordinate of center of the ellipse
y
y-coordinate of center of the ellipse
startangle
starting angle
endangle
ending angle
xradius
specifies the X radius of the ellipse
yradius
specifies the Y radius of the ellipse Making a circle and an ellipse in C can be done easily. How to do is, first initialize a graph with two parameters and a path to the "bgi" folder in your system. To make an ellipse on the screen, all we need to do is call the ellipse() function with six numbers as the coordinates of the ellipse. These six co-ordinates decide the location of the ellipse, angles, and radius from X-axis and Y-axis. We would need to include graphics.h file in your program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* graphics.h library is used to include and facilitate graphical operations in program. C graphics using graphics.h functions can be used to draw different shapes, display text in different fonts, change colors and many more. Using functions of graphics.h you can make graphics programs, animations, projects and games. You can draw circles, lines, rectangles, bars and many other geometrical figures. You can change their colors using the available functions and fill them. */ /* draw an ellipse on the screen by ellipse() function code example */ #include <graphics.h> int main() { // gm is Graphics mode which is a computer display // mode that generates image using pixels. // DETECT is a macro defined in "graphics.h" header file int gd = DETECT, gm; // location of ellipse int x = 250, y = 200; // here is the starting angle // and end angle int start_angle = 0; int end_angle = 360; // radius from x axis and y axis int x_rad = 100; int y_rad = 50; // initgraph initializes the graphics system // by loading a graphics driver from disk initgraph(&gd, &gm, ""); // ellipse function ellipse(x, y, start_angle, end_angle, x_rad, y_rad); getch(); // closegraph function closes the graphics // mode and deallocates all memory allocated // by graphics system . closegraph(); return 0; }
kbhit() Function in C++
The kbhit is basically the Keyboard Hit. This function is present at conio.h header file. So for using this, we have to include this header file into our code. The functionality of kbhit() is that, when a key is pressed it returns nonzero value, otherwise returns zero. kbhit() is used to determine if a key has been pressed or not. If a key has been pressed then it returns a non zero value otherwise returns zero.
Syntax for kbhit() Function in C++
#include <conio.h> int kbhit();
Function returns true (non-zero) if there is a character in the input buffer, otherwise false. Note : kbhit() is not a standard library function and should be avoided.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/* kbhit() function is not defined as part of the ANSI C/C++ standard. It is generally used by Borland's family of compilers. It returns a non-zero integer if a key is in the keyboard buffer. It will not wait for a key to be pressed. */ // C++ program code example to fetch key pressed using kbhit() #include <conio.h> #include <iostream> int main() { char ch; while (1) { if (kbhit) { // Stores the pressed key in ch ch = getch(); // Terminates the loop // when escape is pressed if (int(ch) == 27) break; cout << "Key pressed= " << ch; } } return 0; }
moveto() Function in C++
Function moves the current position(cp) to (x,y). The header file graphics.h contains moveto() function which changes the current position to (x, y). Means if you want to move a point from the current position to a new position then you can use this function. outtext is the function used to display the given string on graphics mode. It uses the font style set by settextstyle and the current position. The current position can be changed with moveto function. Alternatively you can use outtextxy function which takes xpos, ypos and string for displaying at a particular location. outtextxy does the work of both moveto and outtext functions.
Syntax for moveto() Function in C++
#include <graphics.h> void moveto(int x, int y);
x
X coordinate of the point
y
Y coordinate of the point moveto() function moves the current position to the x, y position relative to the current viewport.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/* First thing is to initiate a graph. Then set the current position to a location by using the function moveto(). moveto() function takes up the x and y co-ordinate which define the location.*/ /* set the current position to a location by moveto() function code example. */ #include <graphics.h> #include <conio.h> void main() { int d,m; d = DETECT; char msg[80]; initgraph(&d, &m, "d:\\tc\\bgi"); //move the C.P. to location (50, 80) moveto(50, 80); // plot a pixel putpixel(getx(), gety(), getmaxcolor()); //output a message at (50, 80) sprintf(msg, " (%d, %d)", getx(), gety()); outtextxy(50, 80, msg); /* move to (200, 100) */ moveto(200, 100); // plot a pixel putpixel(getx(), gety(), getmaxcolor()); /* create and output a message at C.P. */ sprintf(msg, " (%d, %d)", getx(), gety()); outtext(msg); getch(); closegraph(); }
line() Function in C++
The header file graphics.h contains line() function which is used to draw a line from a point(x1, y1) to point(x2, y2) i.e. (x1, y1) and (x2, y2) are end points of the line. The function line() draws a line on the graphics screen between two specified points. So this function requires four parameters namely x1, y1, x2, and y2 to represent two points. This function draws a line from (x1, y1) coordinates to (x2, y2) coordinates on the graphics screen.
Syntax for line() Function in C++
void line(int x1, int y1, int x2, int y2);
x1
X coordinate of first point
y1
Y coordinate of first point.
x2
X coordinate of second point.
y2
Y coordinate of second point. You can change "linestyle", "pattern", "thickness" of the line by setlinestyle() function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/* draw a line in C++ graphic code example */ #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm,x,y; clrscr(); initgraph(&gd,&gm,"c:\\TC\\bgi"); //INITIALISING GRAPHICS MODE setlinestyle(0,0,3); outtextxy(300,150,"LINE()"); line(350,60,200,200); outtextxy(300,300," CURRENT POSITION"); linerel(320,350); outtextxy(335,315,"LINEREL()"); outtextxy(30,30," CURRENT POSITION"); lineto(30,200); outtextxy(70,45,"LINETO()"); getch(); closegraph(); }
getch() Function in C++
The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program.
Syntax for getch() Function in C++
#include <conio.h> int getch(void);
The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input. We use a getch() function in a C/ C++ program to hold the output screen for some time until the user passes a key from the keyboard to exit the console screen. Using getch() function, we can hide the input character provided by the users in the ATM PIN, password, etc. • getch() method pauses the Output Console until a key is pressed. • It does not use any buffer to store the input character. • The entered character is immediately returned without waiting for the enter key. • The entered character does not show up on the console. • The getch() method can be used to accept hidden inputs like password, ATM pin numbers, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* wait for any character input from keyboard by getch() function code example. The getch() function is very useful if you want to read a character input from the keyboard. */ // C code to illustrate working of // getch() to accept hidden inputs #include<iostream.h> #include<conio.h> void main() { int a=10, b=20; int sum=0; clrscr(); sum=a+b; cout<<"Sum: "<<sum; getch(); // use getch() befor end of main() }
While Loop Statement in C++
In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.
Syntax for While Loop Statement in C++
while (condition) { // body of the loop }
• A while loop evaluates the condition • If the condition evaluates to true, the code inside the while loop is executed. • The condition is evaluated again. • This process continues until the condition is false. • When the condition evaluates to false, the loop terminates. Do not forget to increase the variable used in the condition, otherwise the loop will never end!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/* While Loop Statement in C++ language */ // program to find the sum of positive numbers // if the user enters a negative number, the loop ends // the negative number entered is not added to the sum #include <iostream> using namespace std; int main() { int number; int sum = 0; // take input from the user cout << "Enter a number: "; cin >> number; while (number >= 0) { // add all positive numbers sum += number; // take input again if the number is positive cout << "Enter a number: "; cin >> number; } // display the sum cout << "\nThe sum is " << sum << endl; return 0; }
If Else Statement in C++
In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement,
Syntax for If Statement in C++
if (condition) { // body of if statement }
The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.
Syntax for If...Else Statement
if (condition) { // block of code if condition is true } else { // block of code if condition is false }
The if..else statement evaluates the condition inside the parenthesis. If the condition evaluates true, the code inside the body of if is executed, the code inside the body of else is skipped from execution. If the condition evaluates false, the code inside the body of else is executed, the code inside the body of if is skipped from execution. The if...else statement is used to execute a block of code among two alternatives. However, if we need to make a choice between more than two alternatives, we use the if...else if...else statement.
Syntax for If...Else...Else If Statement in C++
if (condition1) { // code block 1 } else if (condition2){ // code block 2 } else { // code block 3 }
• If condition1 evaluates to true, the code block 1 is executed. • If condition1 evaluates to false, then condition2 is evaluated. • If condition2 is true, the code block 2 is executed. • If condition2 is false, the code block 3 is executed. There can be more than one else if statement but only one if and else statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
Syntax for If Else If Ladder in C++
if (condition) statement 1; else if (condition) statement 2; . . else statement;
Working of the if-else-if ladder: 1. Control falls into the if block. 2. The flow jumps to Condition 1. 3. Condition is tested. If Condition yields true, goto Step 4. If Condition yields false, goto Step 5. 4. The present block is executed. Goto Step 7. 5. The flow jumps to Condition 2. If Condition yields true, goto step 4. If Condition yields false, goto Step 6. 6. The flow jumps to Condition 3. If Condition yields true, goto step 4. If Condition yields false, execute else block. Goto Step 7. 7. Exits the if-else-if ladder. • The if else ladder statement in C++ programming language is used to check set of conditions in sequence. • This is useful when we want to selectively executes one code block(out of many) based on certain conditions. • It allows us to check for multiple condition expressions and execute different code blocks for more than two conditions. • A condition expression is tested only when all previous if conditions in if-else ladder is false. • If any of the conditional expression evaluates to true, then it will execute the corresponding code block and exits whole if-else ladder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* If Else Statement in C++ Language */ #include <iostream> using namespace std; int main () { // local variable declaration: int a = 100; // check the boolean condition if( a < 20 ) { // if condition is true then print the following cout << "a is less than 20;" << endl; } else { // if condition is false then print the following cout << "a is not less than 20;" << endl; } cout << "value of a is : " << a << endl; return 0; }
rectangle() Function in C++
rectangle() is used to draw a rectangle. Coordinates of left top and right bottom corner are required to draw the rectangle. left specifies the X-coordinate of top left corner, top specifies the Y-coordinate of top left corner, right specifies the X-coordinate of right bottom corner, bottom specifies the Y-coordinate of right bottom corner.
Syntax for rectangle() Function in C++
rectangle(int left, int top, int right, int bottom);
left
X coordinate of top left corner.
top
Y coordinate of top left corner.
right
X coordinate of bottom right corner.
bottom
Y coordinate of bottom right corner. To create a rectangle, you have to pass the four parameters in this function. The two parameters represent the left and top upper left corner. Similarly, the right bottom parameter represents the lower right corner of the rectangle. This function does not return any value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* function rectangle() draws a rectangle in graphic mode. */ int main() { // location of left, top, right, bottom int left = 150, top = 150; int right = 450, bottom = 450; // initgraph initializes the graphics system // by loading a graphics driver from disk initgraph(&gd, &gm, ""); // rectangle function rectangle(left, top, right, bottom); left = 200, = 250; right = 150, = 300; rectangle(left, top, right, bottom); left = 100, = 200; right = 450, = 100; rectangle(left, top, right, bottom); getch(); return 0; }
initgraph() Function in C++
To create a program in Graphics Mode, the first step would be to include the header file graphics.h. This file is required for Graphics programming. After this, the graphics have to be initialized. C Language supports 16 Bit's MS-DOS environment. Initializing the Graphics mode is to call various functions, one such is called initgraph. initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. If you tell initgraph to autodetect, it calls detectgraph to select a graphics driver and mode. initgraph also resets all graphics settings to their defaults (current position, palette, color, viewport, and so on) and resets graphresult to 0.
Syntax for initgraph() Function in C++
void initgraph (int *graphdriver, int *graphmode, char *pathtodriver);
graphdriver
This is an integer that indicates that the graphics driver has been used.
graphmode
It is also an integer value that detects the available graphics driver and initializes the graphics mode according to its highest resolution.
pathtodriver
This is the path of the directory that first searches the initgraph function graphics driver. If the graphics driver is not available then the system searches it in the current directory. It is necessary to pass the correct value of the three parameters in the initgraph function or else an unpredictable output is obtained.
intgd = DETECT, gm; initgraph (&gd, &gm, " ");
To initialize Graphics mode, you only have to write two lines. Here, we have taken two integer variables 'd' and 'm'. Here, DETECT is an enumeration type that identifies and identifies the proper graphics driver. The initgraph function has to pass the address of both the variables. You can see in the example that we have given a space at the position of the third variable. This means that if you do not know the driver's path then you can leave it blank. The compiler will auto-detect the path. initgraph always sets the internal error code; on success, it sets the code to 0. If an error occurred, *graphdriver is set to -2, -3, -4, or -5, and graphresult returns the same value as listed below: • grNotDetected -2 Cannot detect a graphics card • grFileNotFound -3 Cannot find driver file • grInvalidDriver -4 Invalid driver • grNoLoadMem -5 Insufficient memory to load driver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* initgraph initializes the graphics system by loading a graphics driver from disk (or validating a registered driver), and putting the system into graphics mode. To start the graphics system, first call the initgraph function. initgraph loads the graphics driver and puts the system into graphics mode. You can tell initgraph to use a particular graphics driver and mode, or to autodetect the attached video adapter at run time and pick the corresponding driver. */ int DGraphics::Init( int gmode ) { int gdriver = VGA, errorcode; gdriver=installuserdriver("SVGA256",NULL); initgraph(&gdriver, &gmode, ""); if ( (errorcode = graphresult()) != grOk ) { cout << "Error: Graphics - %s\n" << grapherrormsg(errorcode); return FALSE; } ActiveMode=gmode; return TRUE; }
Standard Library malloc() Function in C++
Allocate memory block. Allocates a block of size bytes of memory, returning a pointer to the beginning of the block. The content of the newly allocated block of memory is not initialized, remaining with indeterminate values. If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced. Malloc function in C++ is used to allocate a specified size of the block of memory dynamically uninitialized. It allocates the memory to the variable on the heap and returns the void pointer pointing to the beginning address of the memory block. The values in the memory block allocated remain uninitialized and indeterminate. In case the size specified in the function is zero then pointer returned must not be dereferenced as it can be a null pointer, and in this case, behavior depends on particular library implementation. When a memory block is allocated dynamically memory is allocated on the heap but the pointer is allocated to the stack.
Syntax for malloc() Function in C++
#include <cstdlib> void* malloc (size_t size);
size
Size of the memory block, in bytes. size_t is an unsigned integral type. On success, a pointer to the memory block allocated by the function. The type of this pointer is always void*, which can be cast to the desired type of data pointer in order to be dereferenceable. If the function failed to allocate the requested block of memory, a null pointer is returned.
Advantages of malloc() in C++
There are a lot of advantages to using the malloc method in one's application: • Dynamic Memory allocation: Usually we create arrays at compile time in C++, the size of such arrays is fixed. In the case at run time we do not use all the space or extra space is required for more elements to be inserted in the array, then this leads to improper memory management or segmentation fault error. • Heap memory: Local arrays that are defined at compile time are allocated on the stack, which has lagged in memory management in case the number of data increases. Thus one needs to allocate memory out of the stack, thus malloc comes into the picture as it allocates the memory location on the heap and returns a pointer on the stack pointing to the starting address of the array type memory being allocated. • Variable-length array: This function helps to allocate memory for an array whose size can be defined at the runtime. Thus one can create the number of blocks as much as required at run time. • Better lifetime: Variable created using malloc method is proved to have a better life than the local arrays as a lifetime of local arrays depends on the scope they are being defined and cannot access out of their scope. But variables or arrays created using malloc exist till they are freed. This is of great importance for various data structures such as linked list, binary heap, etc.
Differences between the malloc() and new
• The new operator constructs an object, i.e., it calls the constructor to initialize an object while malloc() function does not call the constructor. The new operator invokes the constructor, and the delete operator invokes the destructor to destroy the object. This is the biggest difference between the malloc() and new. • The new is an operator, while malloc() is a predefined function in the stdlib header file. • The operator new can be overloaded while the malloc() function cannot be overloaded. • If the sufficient memory is not available in a heap, then the new operator will throw an exception while the malloc() function returns a NULL pointer. • In the new operator, we need to specify the number of objects to be allocated while in malloc() function, we need to specify the number of bytes to be allocated. • In the case of a new operator, we have to use the delete operator to deallocate the memory. But in the case of malloc() function, we have to use the free() function to deallocate the memory.
Data races
Only the storage referenced by the returned pointer is modified. No other storage locations are accessed by the call. If the function reuses the same unit of storage released by a deallocation function (such as free or realloc), the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.
Exceptions
No-throw guarantee: this function never throws exceptions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/* allocate the requested size of bytes and it returns a pointer to the first byte of allocated memory by malloc() function code example. */ #include <iostream> #include <cstdlib> using namespace std; int main() { // allocate 5 int memory blocks int* ptr = (int*) malloc(5 * sizeof(int)); // check if memory has been allocated successfully if (!ptr) { cout << "Memory Allocation Failed"; exit(1); } cout << "Initializing values..." << endl << endl; for (int i = 0; i < 5; i++) { ptr[i] = i * 2 + 1; } cout << "Initialized values" << endl; // print the values in allocated memories for (int i = 0; i < 5; i++) { // ptr[i] and *(ptr+i) can be used interchangeably cout << *(ptr + i) << endl; } // deallocate memory free(ptr); return 0; }
setfillstyle() Function in C++
The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. Current fill pattern and fill color is used to fill the area. setfillstyle sets the current fill pattern and fill color. To set a user-defined fill pattern, do not give a pattern of 12 (USER_FILL) to setfillstyle; instead, call setfillpattern.
Syntax for setfillstyle() Function in C++
#include<graphics.h> void setfillstyle(int pattern, int color);
color
Specify the color • BLACK – 0 • BLUE – 1 • GREEN – 2 • CYAN – 3 • RED – 4 • MAGENTA – 5 • BROWN – 6 • LIGHTGRAY – 7 • DARKGRAY – 8 • LIGHTBLUE – 9 • LIGHTGREEN – 10 • LIGHTCYAN – 11 • LIGHTRED – 12 • LIGHTMAGENTA – 13 • YELLOW – 14 • WHITE – 15
pattern
Specify the pattern • EMPTY_FILL – 0 • SOLID_FILL – 1 • LINE_FILL – 2 • LTSLASH_FILL – 3 • SLASH_FILL – 4 • BKSLASH_FILL – 5 • LTBKSLASH_FILL – 6 • HATCH_FILL – 7 • XHATCH_FILL – 8 • INTERLEAVE_FILL – 9 • WIDE_DOT_FILL – 10 • CLOSE_DOT_FILL – 11 • USER_FILL – 12 If invalid input is passed to setfillstyle, graphresult returns -1(grError), and the current fill pattern and fill color remain unchanged. The EMPTY_FILL style is like a solid fill using the current background color (which is set by setbkcolor). This function does not return any value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* The header file graphics.h contains setfillstyle() function which sets the current fill pattern and fill color. floodfill() function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area. */ #include <graphics.h> // driver code int main() { // gm is Graphics mode which is // a computer display mode that // generates image using pixels. // DETECT is a macro defined in // "graphics.h" header file int gd = DETECT, gm; // initgraph initializes the // graphics system by loading // a graphics driver from disk initgraph(&gd, &gm, " "); // center and radius of circle int x_circle = 250; int y_circle = 250; int radius=100; // setting border color int border_color = WHITE; // set color and pattern setfillstyle(HATCH_FILL,RED); // x and y is a position and // radius is for radius of circle circle(x_circle,y_circle,radius); // fill the color at location // (x, y) with in border color floodfill(x_circle,y_circle,border_color); getch(); // closegraph function closes the // graphics mode and deallocates // all memory allocated by // graphics system closegraph(); return 0; }
arc() Function in C++
In the C programming language, there is an option to create an arc of a circle of a given radius with a given center coordinates and degree of the arc. The arc() function is used to create an arc. This arc function is included in graphics.h library in C which contains methods that can draw figures on the output screen. The function to make an arc(), accepts five parameters for x, y co-ordinate, starting angle, end angle and radius. This will make the arc will all the values are fine. The Example below takes care of all these things as it have four arcs implemented.
Syntax for arc() Function in C++
#include <graphics.h> void arc(int x, int y, int start_angle, int end_angle, int radius);
x
x coordinate of the center of the arc
y
y coordinate of the center of the arc
start_angle
starting angle of arc
end_angle
ending angle of arc
radius
radius of the arc The header file graphics.h contains arc() function which draws an arc with center at (x, y) and given radius. start_angle is the starting point of angle and end_angle is the ending point of the angle. The value of the angle can vary from 0 to 360 degree.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/* arc() function is used to draw an arc with center (x, y) and stangle specifies starting angle, endangle specifies the end angle and last parameter specifies the radius of the arc. arc function can also be used to draw a circle but for that starting angle and end angle should be 0 and 360 respectively. */ /* create an arc by arc() function code example. */ #include <graphics.h> // driver code int main() { // gm is Graphics mode which is // a computer display mode that // generates image using pixels. // DETECT is a macro defined in // "graphics.h" header file int gd = DETECT, gm; // location of the arc int x = 250; int y = 250; // starting angle and ending angle // of the arc int start_angle = 155; int end_angle = 300; // radius of the arc int radius = 100; // initgraph initializes the graphics system // by loading a graphics driver from disk initgraph(&gd, &gm, ""); // arc function arc(x, y, start_angle, end_angle, radius); getch(); // closegraph function closes the graphics // mode and deallocates all memory allocated // by graphics system closegraph(); return 0; }
getmaxy() Function in C++
The header file graphics.h contains getmaxy() function which returns the maximum Y coordinate for current graphics mode and driver. getmaxy returns the maximum (screen-relative) y value for the current graphics driver and mode. For example, on a CGA in 320*200 mode, getmaxy returns 199. getmaxy is invaluable for centering, determining the boundaries of a region onscreen, and so on.
Syntax for getmaxy() Function in C++
#include <graphics.h> int getmaxy(void);
getmaxy() returns the maximum y screen coordinate. getmaxy() function is used to fetch the maximum Y coordinate for the current graphics mode or driver.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* Function getmaxy() returns the maximum Y coordinate for current graphics mode and driver. */ int main() { int x,y,i; int g=DETECT,d; initgraph(&g,&d,"\tc\bgi"); cleardevice(); x=getmaxx()/2; y=getmaxy()/2; settextstyle(TRIPLEX_FONT, HORIZ_DIR, 3); setbkcolor(rand()); setcolor(4); outtextxy(30,100,"Press"); outtextxy(30,130,"any"); outtextxy(30,160,"key"); outtextxy(30,190, "to"); outtextxy(30,220,"Quit"); while (!kbhit()) { setcolor(rand()); for (int i=0;i<50;i++) circle(x,y,i ); setcolor(rand()); for (int j=70;j<120;j++) circle(x,y,j); setcolor(rand()); for (int k=140;k<190;k++) circle(x,y,k); setcolor(rand()); for (int l=210;l<230;l++) circle(x,y,l); delay(200); } getch(); closegraph(); }
lineto() Function in C++
Function lineto() draws a line from the current position (CP) to the point (x, y), you can get current position using getx and gety function. Where, (x, y) are the coordinates upto which the line will be drawn from previous point.
Syntax for lineto() Function in C++
#include<graphics.h> lineto(int x, int y);
x
X coordinate of the point
y
Y coordinate of the point Use getx() and gety() to get the current position.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* lineto() function draws a line from current position to the point(x, y). */ /* draws a line by lineto() function code example */ #include<iostream.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm,x,y; clrscr(); initgraph(&gd,&gm,"c:\\TC\\bgi"); //INITIALISING GRAPHICS MODE setlinestyle(0,0,3); outtextxy(300,150,"LINE()"); line(350,60,200,200); outtextxy(300,300," CURRENT POSITION"); linerel(320,350); outtextxy(335,315,"LINEREL()"); outtextxy(30,30," CURRENT POSITION"); lineto(30,200); outtextxy(70,45,"LINETO()"); getch(); closegraph(); }
getmaxx() Function in C++
The header file graphics.h contains getmaxx() function which returns the maximum X coordinate for current graphics mode and driver. getmaxx() returns the maximum (screen-relative) x value for the current graphics driver and mode. For example, on a CGA in 320*200 mode, getmaxx returns 319. getmaxx is invaluable for centering, determining the boundaries of a region onscreen, and so on.
Syntax for getmaxx() Function in C++
#include <graphics.h> int getmaxx(void);
getmaxx returns the maximum x screen coordinate. getmaxx() function is used to fetch the maximum X coordinate for the current graphics mode or driver.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Function getmaxx() returns the maximum X coordinate for current graphics mode and driver. */ void loading() { int i,j,x,y; setbkcolor(6); x=getmaxx()/2; y=getmaxy()/2; for(j=30;j<200;j++) { delay(10); setcolor(j/20); arc(x,y,0,180,j-10); } settextstyle(3,0,6); outtextxy(150,250,"!..DOGDE IT..!"); settextstyle(4,0,4); outtextxy(250,340,"LOADING"); for(i=100; i<600; i++) { bar(i,380,i,400); delay(10); } }
floodfill() Function in C++
floodfill function is used to fill an enclosed area. Current fill pattern and fill color is used to fill the area.(x, y) is any point on the screen if (x,y) lies inside the area then inside will be filled otherwise outside will be filled, border specifies the color of boundary of area. To change fill pattern and fill color use setfillstyle.
Syntax for floodfill() Function in C++
#include <graphics.h> void floodfill(int x, int y, int border_color).
x
X coordinate of the point within the enclosed area to be filled
y
Y coordinate of the point within the enclosed area to be filled
border_color
specify the color int values corresponding to colors: • BLACK 0 • BLUE 1 • GREEN 2 • CYAN 3 • RED 4 • MAGENTA 5 • BROWN 6 • LIGHTGRAY 7 • DARKGRAY 8 • LIGHTBLUE 9 • LIGHTGREEN 10 • LIGHTCYAN 11 • LIGHTRED 12 • LIGHTMAGENTA 13 • YELLOW 14 • WHITE 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/* floodfill() fills an enclosed area on bitmap devices. (x,y) is a "seed point" within the enclosed area to be filled. The area bounded by the color border is flooded with the current fill pattern and fill color. If the seed point is within an enclosed area, the inside will be filled. If the seed is outside the enclosed area, the exterior will be filled. Use fillpoly instead of floodfill whenever possible so that you can maintain code compatibility with future versions. */ /* fill an enclosed area on bitmap devices by floodfill() function code example. */ #include <stdio.h> #include <conio.h> void main() { int d,m; int midx,midy; d=DETECT; initgraph(&d,&m,"c:\\tc\\bgi"); midx=getmaxx()/2; midy=getmaxy()/2; circle(midx,midy,50); floodfill(midx,midy,15); circle(midx+50,midy+100,80); floodfill(midx,midy,15); getch(); closegraph(); }
main() Function in C++
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
Syntax for main() Function in C++
void main() { ............ ............ }
void
void is a keyword in C++ language, void means nothing, whenever we use void as a function return type then that function nothing return. here main() function no return any value.
main
main is a name of function which is predefined function in C++ library. In place of void we can also use int return type of main() function, at that time main() return integer type value. 1) It cannot be used anywhere in the program a) in particular, it cannot be called recursively b) its address cannot be taken 2) It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace). 3) It cannot be defined as deleted or (since C++11) declared with C language linkage, constexpr (since C++11), consteval (since C++20), inline, or static. 4) The body of the main function does not need to contain the return statement: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;. 5) Execution of the return (or the implicit return upon reaching the end of main) is equivalent to first leaving the function normally (which destroys the objects with automatic storage duration) and then calling std::exit with the same argument as the argument of the return. (std::exit then destroys static objects and terminates the program). 6) (since C++14) The return type of the main function cannot be deduced (auto main() {... is not allowed). 7) (since C++20) The main function cannot be a coroutine.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* simple code example by main() function in C++ */ #include <iostream> using namespace std; int main() { int day = 4; switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; case 3: cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; case 7: cout << "Sunday"; break; } return 0; }
#include Directive in C++
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
Syntax for #include Directive in C++
#include "user-defined_file"
Including using " ": When using the double quotes(" "), the preprocessor access the current directory in which the source "header_file" is located. This type is mainly used to access any header files of the user's program or user-defined files.
#include <header_file>
Including using <>: While importing file using angular brackets(<>), the the preprocessor uses a predetermined directory path to access the file. It is mainly used to access system header files located in the standard system directories. Header File or Standard files: This is a file which contains C/C++ function declarations and macro definitions to be shared between several source files. Functions like the printf(), scanf(), cout, cin and various other input-output or other standard functions are contained within different header files. So to utilise those functions, the users need to import a few header files which define the required functions. User-defined files: These files resembles the header files, except for the fact that they are written and defined by the user itself. This saves the user from writing a particular function multiple times. Once a user-defined file is written, it can be imported anywhere in the program using the #include preprocessor. • In #include directive, comments are not recognized. So in case of #include <a//b>, a//b is treated as filename. • In #include directive, backslash is considered as normal text not escape sequence. So in case of #include <a\nb>, a\nb is treated as filename. • You can use only comment after filename otherwise it will give error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* using #include directive in C language */ #include <stdio.h> int main() { /* * C standard library printf function * defined in the stdio.h header file */ printf("I love you Clementine"); printf("I love you so much"); printf("HappyCodings"); return 0; }
setcolor() Function in C++
setcolor() function is used to set the foreground color in graphics mode. After resetting the foreground color you will get the text or any other shape which you want to draw in that color. setcolor sets the current drawing color to color, which can range from 0 to getmaxcolor. The current drawing color is the value to which pixels are set when lines, and so on are drawn. The drawing colors shown below are available for the CGA and EGA, respectively.
Syntax for setcolor() Function in C++
void setcolor(int color);
color
specify the color setcolor() functions contains only one argument that is color. It may be the color name enumerated in graphics.h header file or number assigned with that color. This function does not return any value. INT VALUES corresponding to Colors: • BLACK 0 • BLUE 1 • GREEN 2 • CYAN 3 • RED 4 • MAGENTA 5 • BROWN 6 • LIGHTGRAY 7 • DARKGRAY 8 • LIGHTBLUE 9 • LIGHTGREEN 10 • LIGHTCYAN 11 • LIGHTRED 12 • LIGHTMAGENTA 13 • YELLOW 14 • WHITE 15
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/* setcolor() function change the current drawing color in graphic mode. */ #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { int gd=DETECT,gm; initgraph(&gd,&gm," "); setbkcolor(5);//set background color setcolor(11);//color of time settextstyle(4, HORIZ_DIR, 8);//font of time setcolor(GREEN); circle(320,240,100); setcolor(RED); outtextxy(320,80."It is circle"); getch(); closegraph(); }


Algorithm takes the input of the number of vertex & edges. Take the input of connected vertex pairs. Print the graph using 2D arrays. A function to "print the matrix". Print 1 if the
Implement to binary search on a sorted array. For the mid index value of any of a 'Subarray', instead of calculating refer Lookup Table. It is an improvement in 'Binary Search' since table
numElements is the initial number of disjoint sets. Union "Two Disjoint" sets. For simplicity, we assume "root1 and root2" are distinct and represent set names. "root1" is the root of set