Chris Pollett>Old Classes>PIC 10a, Spring 2000>Hw2>Hw2 Solutions

Spring 2000 PIC 10a HW2 Solutions Page

//
//Program Name: RandomPlotter.cpp
//Purpose: Get the parameters of a linear congruential
//         generator from the user. Then prints out that
//         many rows of the generator.
//Known Bugs: none


#include < iostream.h >

void RandomPlotter( int modulus, int scalar, int offset, int seed, int numberOfRows);
	// Prints out numberofRows many rows of the generator

void PrintRow(int columns);
	// Prints out columns spaces followed by a * and a newline


//
// Function Name: main()
// Purpose: Gets generator parameters from user then call RandomDrawer
//	    to draw out the random number generator
//
// Known Bugs: none
//

void main()
{

	// Get inputs from user
	//
	int modulus, scalar, offset, seed, numberOfRows;

	cout <<"Welcome to the linear congruential\n"
		 <<"random number plotter.\n\n"
		 <<"Please enter the data asked for below to see a plot.\n"
		 <<"All inputs should be positive integers.\n\n";

	cout << "Enter a modulus:\n";
	cin >> modulus;

	cout << "\nEnter scalar:\n";
	cin >> scalar;

	cout << "\nEnter offset:\n";
	cin >> offset;

	cout << "\nEnter start seed:\n";
	cin >> seed;

	cout << "\nEnter number of rows:\n";
	cin >> numberOfRows;

	// Now do drawing...
	//
	RandomPlotter( modulus, scalar, offset, seed, numberOfRows);

}

//
// Function Name: RandomPlotter()
// Purpose:  Prints out numberofRows many rows of the generator
//	     uses the function PrintRows to print out an individual
//	     row.
//
// Known Bugs: none
//
void RandomPlotter( int modulus, int scalar, int offset, int seed, int numberOfRows)
{
	int i=0;

	while (i < numberOfRows)
	{
		PrintRow(seed);
		seed = (scalar*seed+offset)%modulus; //formula
						//to get next random number
		i++;
	}
}

//
// Function Name: PrintRow
// Purpose: Prints out columns spaces followed by a * and a newline
//	    used by RandomPlotter for printing out a row
//
// Known Bugs: none
//

void PrintRow(int columns)
{
	int i=0;

	while(i< columns)
	{
		cout << " ";
		i++;
	}

	cout << "*\n";

}