CMSC104

Problem Solving and Computer Programming

Section 0501

 

Project 3 -- Modelling Reality

Due

Project 3 is worth 100 points and due at 11:59pm, Sunday, 6 Nov

Introduction

To study some areas, we have to to simulate them or model them. This project will model the flipping of a coin. When we flip a fair coin, we do not know the result in advance, because it is random. When we flip the coin, there are only to possible outcomes, heads or tails. If we flip the coin enough times, we know that there should be the same number of heads as there is tails. We can use the formula: number of heads = number of flips / 2

Each flip is an occurrance and a number of occurrances is a set of occurrances.

We know that the computer always does things in the same exact way, yet we want to study the result of random occurances. To be able to do this, we fool the computer. There is a built-in function that will return a random number. This function takes a starting number (or seed) and calcuates a new random number each time. The bad news is that this function will always return the same set of random numbers. To fix this, the seed must altered before using the random function for the first time.

Requirements

This project will allow the user to model the flipping of a coin a user-specified number of times. Additionally, the project will allow a user-specified number of set of flips to be modelled. After getting the user's values, you can then starting flipping the "coin". As you flip, you will have to keep track of how many heads and how many tails you get. At the end of the set, you need to see how many of each you got. If you asked for 400 occurrances, you "should" have 200 of each. If you got 197 heads, this deviated from the expected 200 by 7. (Or -7, depending on how you do the math, in which case, you need the absolute value!) Sum the deviations of all the sets. Determine the largest and smallest deviation, and the average deviation.

Prepare a report from the user, giving the number of sets, the number of occurrances in each set, the expected number of occurances for each possibility, the sum of the deviations, the average deviation, the smallest deviation and the largest deviation.

Hints

As discussed in class, you must use the following includes:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
  

One additional function is will be needed is a function that will return the absolute value of a number, the absolute value of -3 is 3, the absolute value of +3 is 3. When a set of occurrances was expected to return 200 heads, and it returned 193, the deviation from the predicted value is 7. When calculating the deviation for this project, we will only sum up the absoluate value of the deviation.

To seed the random number generator, you must change the seed once in your program. You need to use the following statement once as one of the first things you do:

srand( time( 0 ) );

To simulate getting only heads and tails, we can generate a random and scale it. By using the mod operator, we can get only a 0 or a 1, one of which we will assume is heads and the other is tails. To get a random number in the range of 0 - 1, you will use the code:

rand( ) % 2

To get the absolute value of a number, use the following:

deviation = abs ( expected - heads );

Compiling

Compile the program with the following command: If you get an error or warning, and you probably did, simply go back into the editor and fix it. Then recompile, continuing until you get no warnings or errors.

Executing

Execute the program with the following command: ./a.out If everything worked correctly, you should get the messages giving the distances.

One Sample Output

(User input is in red.)
How many sets do you wish?  10
How many attempts per set do you wish?  100

The deviation this time was 3.
The deviation this time was 0.
The deviation this time was 2.
The deviation this time was 1.
The deviation this time was 2.
The deviation this time was 6.
The deviation this time was 2.
The deviation this time was 5.
The deviation this time was 2.
The deviation this time was 7.


The number of requested sets was 10.
The number of occurrances per set was 100.
The expected number of occurances of each odd and even was 50.
The sum of the deviations was = 30
The average dieviation was = 3.000000
The smallest deviation was 0.
The largest deviation was 7.
  

Notice that the UNIX prompt must be on the next line!

Another Sample Output

(User input is in red.)
How many sets do you wish?  1000
How many attempts per set do you wish?  100000



The number of requested sets was 1000.
The number of occurrances per set was 100000.
The expected number of occurances of each odd and even was 50000.
The sum of the deviations was = 91381
The average dieviation was = 91.381000
The smallest deviation was 1.
The largest deviation was 190.

  

Notice that the UNIX prompt must be on the next line!

Turning In Your Project

Once you have everything absolutely correct, you must use Blackboard to submit the project to the TA for grading. Do not email it to the instructor because you will lose points for not following instructions!

When submitting the assignment, I only want the file jdoe2p3.c. Please make the title and the file name the same when you submit it! If you submit a file named jdoe2p3.c, the way we save the material delete everything but the last file with than name. Therefore, your file will be lost and we will give you a zero!!!! Use your login ID!!!

Grading

This project will be grade using the following scale:

Documentation 25 points
Correct Results25 points
Correct Sytle25 points
Other25 points

You will lose 5 points if you do not name the file correctly.
You will lose 5 points if you email the project instead of submitting it via Blackboard.
You will lose 5 points if the prompt is not on the next line.

Comments Required

        /*****************************************************/
        /* Program Header Block                              */
        /* Filename:       jdoe2p3.c                         */
        /* Name:           Ima Student                       */
        /* Email:          jdoe2@umbc.edu                    */
        /* Date:           17 Oct 2005                       */
        /* Course/section: CMSC-104/0501                     */
        /* Description:                                      */
        /*     Analysis:                                     */
        /*         Input:                                    */
        /*         Output:                                   */
        /*         Constraints:                              */
        /*         Formulas:                                 */
        /*         Assumptions:                              */
        /*     Design:                                       */
        /*         (Your psuedocode goes here.)              */
        /*                                                   */
        /* Notes:          (As needed.)                      */
        /*****************************************************/
        


UMBC | CSEE |