Thread: help with a source code..

  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

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    1. Use code tags.
    2. Long posts are very difficult to read and thus reduce your chances of resolving the problem.
    3. We won't do your homework for you. We can, however, try to help you understand certain programming principles and techniques.
    3. What IS the problem? What errors do you get?
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    like bennyandthejets said, your post is too long/hard to read/no specific question... if you're copying/pasting something, put it in [quote][/quote] tags, and put your code in [code][/code] tags... or since the source you posted is so long, just attatch it as a .zip file or something...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Exclamation Some Very General Programming Suggestions...

    That's a pretty big assignment. I hope you have at least a week to get it done.

    Do NOT try to write the whole program before test-compiling and test-running!!! All "real" programs are written and tested a little at a time.... It's WAY easier t find an error in 3 lines of code, than it is to find 10 errors in 100 lines of code.

    So, work on one section of the program requirements at a time. For example, you could write and debug the code for these requirements first:
    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.
    Put in some extra cout statements along the way, so that you can verify things are working so far. You can remove these extra "debugging" statements when you're done.

    Also, you can also hard-code the input values - Instead of using a cin statement, just stick-in a constant like int NumberOfTrials = 1000; That way, you won't have to type-in all the variables every time you test-run. If you've already written and debugged the user-input code, you can temporarily comment-it out and replace it with constants to speed-up testing.

    When you write a function, start-out with an empty function first. If the function is supposed to return a value, just make it return a constant (i.e return 10; ). When that compiles and runs, start adding the logic to the function... Test-compile and test-run every few lines!!! ALWAYS TEST your functions before moving-on to other code or other functions! Sometimes you'll have to write some special test-code just to test your function. In fact, this is a very common programming practice.

    You don't have to write the code in the same order as the problem statement. It's not always easy to know what to work on first, or next... You'll just have to look at the requirements and figure-out what to do "next".

    Take it slow... a few lines of code at a time, and you will get this poject done!
    Last edited by DougDbug; 05-20-2004 at 11:53 AM.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by DougDbug
    Do NOT try to write the whole program before test-compiling and test-running!!!
    that's the way i've always done it... it takes too long to test-compile every little function in your program... if you just jump in and code things, as long as you keep it clean, you'll hit a groove that's hard to get out of, and the ideas will start flowing like mad...
    Quote Originally Posted by DougDbug
    It's WAY easier t find an error in 3 lines of code, than it is to find 10 errors in 100 lines of code.
    not if you're using a good IDE... and those 10 errors are really probably only about 3 errors...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User ventolin's Avatar
    Join Date
    Jan 2004
    Posts
    92
    Quote Originally Posted by major_small
    that's the way i've always done it... it takes too long to test-compile every little function in your program... if you just jump in and code things, as long as you keep it clean, you'll hit a groove that's hard to get out of, and the ideas will start flowing like mad...
    not if you're using a good IDE... and those 10 errors are really probably only about 3 errors...
    Are you mad?

    What if youve written 250 lines of code, surely this will expand the testing and debugging phase? whereas small builds every 5/10 lines or so will take longer initially but you will pick up on simple mistakes you commonly do, and thus make you a better programmer in the long run.

    Surely writing everything you do like this, its more difficult for you to spot run-time bugs as you have to worry about 'too' much code. Divide and conquer the problem into many smaller problems usually makes it easier to code overall than worrying about doing it as a whole. Did u not take any classes in program design? Unless you are a super genius, most, if not all, programming friends i know have never done it that way...

    I worked for a company which practiced eXtreme programming where you write the tests before ANY code, this helps to write better more efficient code most of the time, plus it helps you to work out the problem more than just start coding straight away and delve head first into the problem.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    If you use functions alot and not put everything in just a few big functions and if you isolate what function does to one thing there is really no problem in writing a lot of code and then testrun it. Generally I can solve problems better if I code a couple of functions, say 4 or 5, before I testrun it and not just testrun all the time.

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    and those 10 errors are really probably only about 3 errors...
    Well... right! And, that can be a BIG PROBLEM too. It's easy to confuse a compiler, and the reported error messages are not always accurate. The compiler doesn't know what you're trying to do! (The first reported error is usually near the first actual error.) Link errors and run-time bugs are often more difficult to find than compile-time errors.

    I once had a "little" misplaced bracket (after a big cut-and-paste) and the compiler reported over one hundred errors!!! Of course, NONE of the error messages said "misplaced bracket". It took a loooong time to find the actual error.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Of course there's a balance between too much compiling/testing and not enough.

    The point is that if you are a beginner, you should always error on the side of too much.

    The newer you are to programming, the less likely you will be able to figure out the cause of one compile error, let alone ten.

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