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   Ads   Donate   Humor

Normal.cpp - normal distribution

Drops a particle from the top center of the screen, and then makes random left/right decisions on the way down, and as a result builds a normal distribution at the screen bottom.

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

Borland C++ 4.0 for MSDOS. BGI graphics.

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 2, 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
View content labeling at ICRA.
Copyright ©2008 Steven Whitney. Last modified 02/27/2008.