dev c++

Lately I have been searching  this post on the net about graphics.h in dev c++ and found few resources about it .  So I decided to write how to use graphics.h in dev-c++.  I am expecting that you have already installed bloodshed dev-cpp on your machine.  Graphics.h is a header file found in borlan c but because of its late development devcpp is now the mostly used IDE and compiler in programming c++.

Below are the steps on how to get graphics.h running on your machine,

  1. download graphics.h and save it to the include folder where you installed dev-cpp (C:\Dev-Cpp\include)
  2. Download libbgi.a and save it in the lib folder where you installed dev-cpp (C:\Dev-Cpp\lib)
  3. So when you have to create a program that needs the use of graphics.h  you must instruct the linker to link in certain libraries by going to projects > project options > parameters tab > linker column copy the lines below and paste it on that column.
1
2
3
4
5
6
-lbgi
-lgdi32
-lcomdlg32
-luuid
-loleaut32
-lole32

By then you should be able to use graphics.h. As a sample I created this bubble sort algorithm as an example on how to use graphics.h in c++.

#include <iostream>
#include <graphics.h>

#define PIXEL_COUNT 1000
#define DELAY_TIME 1 /* in milliseconds */
#define CLIP_ON 1

//using namespace std;

int main () {

int temp, x, toSort[300], loop, isSwapped=1;
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, “”);

/* read result of initialization */
errorcode = graphresult();

if (errorcode != grOk) { /* an error occurred */

printf(“Graphics error: %s\n”, grapherrormsg(errorcode));
printf(“Press any key to halt:”);
getch();
exit(1); /* terminate with an error code */

}

/* Generate the numbers */
for(x=0; x<300; x++) {
toSort[x] = rand() % 300 + 1;
}

for(loop=0; (loop <= 300)&& isSwapped == 1; loop++) {
isSwapped = 0;
for(x=0; x<300; x++) { if(toSort[x] > toSort[x+1]) {

//swapping of data
temp = toSort[x];
toSort[x] = toSort[x+1];
toSort[x+1] = temp;
isSwapped = 1;

}
std::cout << loop << “=> “<< x << “,” << toSort[x] << ” “;

putpixel(x, toSort[x], 16776960);
delay(DELAY_TIME);

}
std::cout << “\n\n”;
if(isSwapped == 1) {
cleardevice();
}
//loop++;
}

//cout << endl << “\n\n\n”;
std::cout << “Loop Count: ” << loop << “\n\n\n”;
system(“pause”);
return 0;
}

The program will have scattered points on the screen and will form a line as the loop iterates and at the end it will display how many loops are needed to sort the randomly generated numbers.

If you have problems using the functions you can refer to this link.  I hope this helps in solving your problem about dev c++ graphics.h with the bubble sort algorithm.