25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

Borland C++ program for MSDOS builds a graphical normal distribution

This simple program drops a particle from the top center of the screen, makes random left/right decisions on the way down, and as a result builds a normal probability distribution at the screen bottom.

It was inspired by the memory of a fourth grade field trip to a Museum of Science and Industry that had a similar exhibit about probability.

It's written in Borland C++ 4.0 for MSDOS and uses Borland's BGI graphics.

In addition to the code below, the program requires my.h and mylib.cpp.

Unlike the previous versions of this program, you don't see the dots as they fall, so there's not much to watch except the building distribution.

Previous versions. These are slower but there's more to watch:

Screenshot:

Screenshot of the graphical normal distribution built by the normal.cpp program.

Normal.cpp

/*	normal.cpp					3-10-96
	Copyright (C)1995-96 Steven Whitney.
	Initially published by http://25yearsofprogramming.com.
	Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.

	Builds normal distribution at bottom of screen.
	
	This version no longer has show-points or trails, etc., from the previous versions.
	It just calculates how far point should be from center & drops it down on the pile.
	Faster, but there's less to watch.

*/
// #include <stdio.h>
// #include <conio.h>
// #include <ctype.h>
// #include <iostream.h>
#include <graphics.h>
#pragma hdrstop

#include "c:\bcs\my.h"
#include "c:\bcs\mylib.cpp"

//////////////////////////////////////////////////////////////////////////////
void main(void)
{
int ch;					// user input
int i;					// counter
int x, y;				// screen coordinates
int count;              // number of left/right decisions made

cout << "NORMAL builds a normal curve on your screen by simulating dropping BBs.\n";
cout << "\nWhile  program is running:\n";
cout << "P          Pause/Resume\n";
cout << "<ESC>      Exit\n";
cout << "\nAny key to continue...";
getch();
randomize();
initgraf();
setcolor(BLUE);
count = getmaxx() * 8;
line(0,getmaxy(),getmaxx(),getmaxy());
while(1)
{
	if(kbhit())
	{
		ch = tolower(getch());
		if(ch == 27)
			break;
		if(ch == 'p')
			getch();
	}
	x = getmaxx()/2;					// "start" at center top
	for(i = 0 ; i < count ; i++)  		// calc. offset from center
		switch(random(2))
		{
			case 0:  x++ ; break;
			case 1:  x-- ; break;
		}
	if(x > getmaxx()) x = getmaxx();
	if(x < 0) x = 0;
					// now just drop directly down until hits top of pile
	y = 0;
	while(!getpixel(x,y+1))
		y++;
	putpixel(x,y,GREEN);
}			// end while(1)
closegraph();
exit(0);
}

 

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.