Thread: help with a source code..

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    1

    help with a source code..

    hello..
    i need help with my source code..

    the question is...
    You are to write a program that simulates trials carried out by rolling some dice. After these trials are
    carried out, the computer first displays the results for each outcome along with the average value of all
    outcomes. The machine then displays a histogram of the outcomes. Following this final display, the
    program is complete. The overall idiom the computer carries out is therefore the generic initialize, process,
    display algorithm. Let us look at how the problem statement breaks this idiom down:
    initialize
    The computer first requests and then extracts an int value from the keyboard that lies between 1000 and
    1000000, inclusive. This number should represent the total number of trials to be carried out. Next, the
    computer will request the number of dice the user intends to “roll” for each trial. The value extracted for
    this result should be a number between 1 and 4. Once these two values have been extracted (such that they
    are both within the specified range), the computer will get an initial seed value for the random number
    generator and then use this seed value (a local variable) to carry out the call srand(seed); Finally, the
    machine should initialize the relevant elements of the results array-counter to 0.
    process
    Once all these values have been gotten and assigned (as side effects of initialize), the computer should
    simulate the trials by calling the function do_trials. This double function will use the values of tot_trials
    and tot_dice to simulate tot_trials trials. Each trial is carried out by simulating a roll of tot_dice dice
    whose values are summed and used to return the double to the variable average. The result of each trial
    (gotten by calling the int function rollem) is used to increment the proper element of the array counter
    results as important side effects of the function.
    After control returns from do_trials, the computer should use the elements of results to find the value
    of max_result. You can get this value by assigning it to the return result of the function get_max, where
    the function itself will return the value of the highest element of results. You need this value to fulfill the
    preconditions required for the two display functions to work.
    display
    The machine needs to call two void functions to carry out the display, show_results and show_histo.
    The first function displays the counts of the possible outcomes of the tot_trials carried out for the tot_dice
    used. The preconditions for show_results to work are that each of the following values need to have been
    determined:
    • tot_dice, the total number of dice rolled for each trial
    • tot_trials, the total number of trials that were carried
    • average, the average value for all the tot_trials
    • max_result, the value representing the largest number of outcomes
    • values have been given to the elements of results, an array that counts the number of times each
    possible outcome occurred for the tot_trials carried out
    The postconditions of this function are that the results for each outcome have been displayed in tabular
    form (the numeric display is right justified) along with the statistics of the trials (tot_dice, tot_trials, and
    average).
    The second function, show_histo, requires the same preconditions as show_results, except that the
    value of average is not needed. The postconditions are clearly that the histogram has been displayed. Each
    star displayed should represent one of the following outcome counts: 5, 10, 20, 50, 100, 200, 500, 1000,
    2000, or 5000. This function needs the value of max_result in order to ensure that no more than 50 stars
    are shown for the outcome that had the most occurrences. If you look at the histogram for the first sample
    run, you will see from the 2 and 12 outcomes and from the histogram display that the number of stars
    shown depends upon a round-off to the nearest representative star value. You will use this information to
    plan the display of each line of stars in a smart way.
    Note from the sample runs that all display is shown using a font that has even spacing for all the
    characters. We do this so that the display looks as it appeared in the dos run, that is, all characters were
    evenly spaced. When you copy and paste the text for each sample run, choose the Courier New font with a
    font size of 10 to produce the same display as those of my sample runs.
    One other thing: you should stop the computer from producing further display after the call to initialize
    and the calls to show_results and show_histo with a call to cin.ignore(80,'\n') You make this call to
    prevent the computer scroll from breaking up the display in a way that will not allow you to get all the
    characters you need on the screen. After each execution of cin.ignore(80,'\n'), copy and paste the screen
    contents into the Word file for your sample run(s).
    Global consts
    To make things simpler, you should define the following global consts (you figure out their values) that
    you plan to reference inside the relevant functions:
    • MIN_TRIALS, representing the minimum number of trials to carry out
    • MAX_TRIALS, representing the maximum number of trials to carry out
    • MIN_DICE, representing the minimum number of dice to use
    • MAX_DICE, representing the maximum number of dice to use
    • MAX_ELS, use an expression dependent on the value of MAX_DICE to get a value representing the
    maximum number of elements you need for your results array.
    • SCALE, a global array of consts, where the elements are initialized to 5, 10, etc. through 5000. You
    will use the elements along with the value of max_result to determine the correct scale to use in
    show_histo.
    Various and sundry hints
    1. Use the auxiliary function get_int, which requires three arguments (a string prompt and a maximum
    and minimum value) and employs a do..while seeker, to get values for tot_trials and tot_dice.
    2. Apply smart semantics on the index expression(s) for the results array. The 0th element of this array for
    a given set of trials should represent the number of times the trials returned a result of tot_dice (all 1’s).
    The last element that is used should represent the number of times the trial returned a value of
    6*tot_dice.
    3. do_trials should call the auxiliary function rollem, a function that returns the result of a single trial
    when tot_dice dice are rolled. The roll of a single die is given by the code 1 + rand( ), where the
    library function rand, as found in the cstdlib library is called. (Note: you will also find the srand
    function in this library.)
    4. You can get the neat layout in show_results with a call to the function get_spaces where the return
    result will tell you how many spaces you need to indent using the manipulator expression
    setw(indent_by). The single argument of this function is max_result, and the return result of the
    function is assigned to the int variable indent_by. The logic of this function should simply count how
    many digits are contained in the value of max_result.
    5. The auxiliary function get_star, whose single argument is max_result is called to return a result that
    represents how many trials each star on the histogram should represent. The requirement is that the
    display for max_result should contain 50 or less stars. You should use a loop that makes references to
    the elements of the global array of consts SCALE to get the return result.
    6. The void function show_stars has two int arguments, total, representing the total number of outcomes
    for a given trial and one_star, representing the number of outcomes a single star represents. Use this
    function to display a single line of stars (rounded off to the nearest star) for a given outcome. Clearly,
    you must call this function from show_histo, passing in the proper arguments for the function to carry
    out the desired


    the source code is...
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <iomanip>
    #include <ctime>
    using namespace std;
    
    const int scale[] = {5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000};
    
    /*const int MIN_TRIALS,		//Global constants
    		MAX_TRIALS,
    		MIN_DICE,
    		MAX_DICE,
    		
    		FACE;
    */
    const int FACE = 6,			//Global constants
    		MAX_ELS = 24;
    
    
    void do_trial(int tot_trial, int tot_dice, float &avera);
    int average(int oneDice[MAX_ELS], int count[MAX_ELS], int tot_trial);
    void display_result( int oneDice[MAX_ELS], int count[MAX_ELS], int tot_trial,	int tot_dice, float avera );
    void show_histo( int oneDice[MAX_ELS], int count[MAX_ELS], int tot_trial,int tot_dice);
    int scale_star(int tot_trial);
    void skip_line();
    
    int main()
    {	
    	int tot_trial;		//initializing total number of trials
    	int tot_dice;		//initializing total number of dices
    	float avera;		//initializing average value
    	srand(time(0));  
    
    
    
    	// Getting the number of trials
    	do
    	{
    		cout << "Enter total number of trials (1000 - 1000000):";
    		cin >> tot_trial;
    		skip_line();
    	}while( (tot_trial<1000) && (tot_trial>1000000) );
    
    	// getting the number of dices
    	do{
    		cout<<"Enter number of dice to use (1-4): ";
    		cin>>tot_dice;
    		skip_line();
    	}while( (tot_dice<=1) && (tot_dice>=4) );
    
    	
    	do_trial(tot_trial, tot_dice, avera);
    	
    	return 0;
    }
    
    
    
    
    //Performing Trial Runs
    void do_trial(int tot_trial, int tot_dice, float &avera)
    {
    	
    	int oneDice[MAX_ELS];
    	int count[MAX_ELS];
    	int rollem = 0;
    	
    	// initializing for count array
    	for( int g=0; g<MAX_ELS; g++ )
    		count[g] = 0;
    	// initializing for oneDice array
    	for( int i=0; i<MAX_ELS; i++ )
    		oneDice[i] = i+1;
    
    	for( int j=1; j<=tot_trial; j++)
    	{	// finding the sum of all dice rolled
    		for(int k=0; k<tot_dice; k++)
    			rollem = (1 + rand() % FACE);
    
    		for(int m=0; m<MAX_ELS; m++)
    			if(oneDice[m]=rollem)
    			{
    				count[m]++;
    				rollem = 0;
    			}
    	}
    
    		for( int n=0; n<MAX_ELS; n++ )
    			cout << oneDice[n] << "       " << count[n] << endl;			
    			
    		avera = average( oneDice[], count[], tot_trial);
    		display_result( oneDice[], count[], tot_trial, tot_dice, avera );
    		show_histo( oneDice[], count[], tot_trial, tot_dice);
    
    }
    
    
    //Averaging trials
    int average(int oneDice[], int count[], int tot_trial)
    {
    	int sum = 0;
    	float av;
    
    	for( int i=0; i<MAX_ELS; i++ )
    		sum += (oneDice[i] * count[i]);
    		
    		av = sum / tot_trial;
    
    		return av;
    }
    
    
    //Display results of the actions performed
    void display_result( int oneDice[], int count[], int tot_trial,	int tot_dice, float avera )
    {
    	int i, j, k, l;
    
    	cout << "RESULTS FOR " << tot_trial << " TRIALS WITH " << tot_dice << "DICE: " << endl;
    	
    	switch(tot_dice){
    	case 1:
    		for (i=0; i<6; i++)
    			cout << " " << oneDice[i] << ": " << setw(4) << count[i] << endl;
    		break;
    	case 2:
    		for (j=1; j<12; j++)
    			cout << " " << oneDice[j] << ": " << setw(4) << count[j] << endl;
    		break;
    	case 3:
    		for (k=2; k<18; k++)
    			cout << " " << oneDice[k] << ": " << setw(4) << count[k] << endl;
    		break;
    	case 4:
    		for (l=3; l<24; l++)
    			cout << " " << oneDice[l] << ": " << setw(4) << count[l] << endl;
    		break;}
    
    	cout << "AVERAGE OF THE " << tot_trial << " TRIALS = " << setw(5) << endl;
    
    }
    
    
    //Displaying results in Histogram format
    void show_histo( int oneDice[MAX_ELS], int count[MAX_ELS], int tot_trial,int tot_dice)
    {
    	int i, j, k, l, sca, ia, ja, ka, la, temp;
    	cout << "HISTOGRAM FOR " << tot_trial << " TRIALS WITH " << tot_dice << "DICE: " << endl;
    	
    	sca = scale_star(tot_trial);
    	
    	switch(tot_dice){
    	case 1:
    		for (i=0; i<6; i++)
    			temp = count[i]
    		{
    			cout << "     " << oneDice[i] << "|";
    			for(ia=count[i]; sca > count[i]; count[i] / sca)
    					cout << "*";
    		}
    		cout << endl;
    		break;
    	case 2:
    		for (j=1; j<12; j++)
    		{
    			cout << "     " << oneDice[j] << "|";
    			for(ja=count[j]; sca > count[j]; count[j]/sca)
    					cout << "*";
    		}
    		cout << endl;
    		break;
    	case 3:
    		for (k=2; k<18; k++)
    			{
    			cout << "     " << oneDice[k] << "|";
    			for(ka=count[k]; sca > count[k]; count[k]/sca)
    					cout << "*";
    		}
    		cout << endl;
    		break;
    	case 4:
    		for (l=3; l<24; l++)
    			{
    			cout << "     " << oneDice[l] << "|";
    			for(la=count[l]; sca > count[l]; count[l]/sca)
    					cout << "*";
    		}
    		cout << endl;
    		break;}
    
    //Scaling the graph
    int scale_star(int tot_trial)
    {
    	if(tot_trial >= 1000 && tot_trial<2500)
    		return 5;
    	else if(tot_trial >= 2500 && tot_trial<7500)
    		return 10;
    	else if(tot_trial >= 7500 && tot_trial<10000)
    		return 20;
    	else if(tot_trial >= 10000 && tot_trial<25000)
    		return 50;
    	else if(tot_trial >= 25000 && tot_trial<75000)
    		return 100;
    	else if(tot_trial >= 75000 && tot_trial<100000)
    		return 200;
    	else if(tot_trial >= 100000 && tot_trial<250000)
    		return 500;
    	else if(tot_trial >= 250000 && tot_trial<500000)
    		return 1000;
    	else if(tot_trial >= 500000 && tot_trial<750000)
    		return 2000;
    	else if(tot_trial >= 750000 && tot_trial<=1000000)
    		return 5000;
    }
    
    //Clear line function
    void skip_line()
    {
    	cin.ignore(80,'\n');
    }
    please help me...
    cant figure anything out...
    Last edited by Salem; 05-21-2004 at 12:49 PM. Reason: tag - you're it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seven Kingdoms I: Ancient Adversaries for Linux
    By MIH1406 in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 01-17-2010, 05:03 PM
  2. How do you call another source code file?
    By nifear4 in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 12:16 PM
  3. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  4. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM
  5. Source Code Beautifier
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-05-2002, 09:21 PM