Pascal's Bagatelle Beginner Coder Challenge

This is a discussion on Pascal's Bagatelle Beginner Coder Challenge within the Contests Board forums, part of the Community Boards category; Hi all, Well i quite liked this one, i saw a maths demo on TV the other day, and thought ...

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425

    Pascal's Bagatelle Beginner Coder Challenge

    Hi all,

    Well i quite liked this one, i saw a maths demo on TV the other day, and thought it would be neat to write, then saw it actually makes for a good little programming problem for beginners methinks.

    As I say, I am aiming it at beginners to have a go at. it is probably trivial for more advanced coders, but might provide some interest, I dont really consider myself advanced, but i know this has thrown up some wider project ideas for me at least.

    Am keeping open for two weeks, winner is first correct working code sent to me by PM. That satisfies the requirements.
    Output validation will be based on the expected distribution for the given parameters.

    Sorry i can only accept win console.
    Limit of 100 lines actual code, which should be plenty.
    C or C++

    It is worked around Pascal's triangle but as if it were set in a Bagatelle board where you ping a ball up like pinball and then it drops down into a field of pins and you win money depending where it lands.

    This problem requires you to model the outcome of such a game in terms of the landing position of the ball.
    As each new ball is dropped into the field then it will bounce off the pins and enventually end up in one of the columns at the bottom, see image:

    http://img201.imageshack.us/img201/3...lbagatelle.jpg

    In the image shown there are 4 columns for this size 'pin field', the arrow shows where a ball is dropped in, then it hits the first pin, and so on, until it finally lands in one of the four possible columns.

    Your challenge is to write a program that can simulate this game and show the expected distribution of balls across the columns.

    Your program must accept input for the base triangle size, (in the example this would be 3, ie three pins) and successfully show the outcome for the resulting number of columns required for the given base size.

    That is to say your program must be able to scale its operation to any size field..

    I am not asking for input validation as such, but it should be obvious that the minimum input has to be two.

    set number of balls as twenty times your base input, this is just to keep the distribution values up.

    Your output must be stored in an array, vector, or other container,number of elements equal to the columns required. - no bigger.

    Display output will be:

    A simple win console

    request input triangle base size
    display the value.
    display the ball count

    //start working part of program..
    then:

    The final display should just be a loop outputting each value in the container.with a pipe seperator

    follwed by a sum of the column values which should be equal to the amount of balls used.

    EG:

    Code:
    for(int i = 0; i < columns; i++)
    {
        //print columns[i] and seperator
        //add columns[i] to check_total
        
    }
    //print check_total =
    so it might look like:
    input base: 5
    ball count 100

    ..working..

    output:
    | 2 | 16 | 27 | 36 | 14 | 5 |

    check total balls distributed: 100
    Last edited by rogster001; 06-03-2012 at 07:48 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    I have received a candidate winner but I think it may be beaten on a technicality.. so am keeping open for now to allow amendments or more entries to challenge.
    Based on the feedback received and some of my thinking since re-reading my post i will clarify a couple of points:

    one input for the base number of pins (ie triangle size) in the equilateral triangle that forms the obstacles field - everything else is determined programatically, the layout of the pins follows the pattern shown in the image.
    Line limit i feel is generous and should encourage beginners to use concise code and suitable constructs - bear in mind it is actual code lines limit, not text layout length
    Just in case anyone is digging out the physics engine library, It is simple probability for the paths, no physical effects per se need to be modelled. you just have to imagine a ball dropped in hitting the first pin, then it falls down to the next level hitting one of the two pins on the second level, depending on which direction it fell from the first pin, and so on down until it exits and falls into one of the containers
    Last edited by rogster001; 06-06-2012 at 02:42 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,425
    Congratualtions to jdragyn! The winner of this challenge!
    jdragyn submitted the most complete response to the question that i received, a nice bit of code with a neat method to calculate the ball position as it moves through the pin field, well within the line limit boundary and with the output distribution correct.

    Congrats !

    winning code nicely commented, posted here with permission:

    Code:
    /* jdragyn.c
    
    implementation of Pascal's bagatelle for rogster001's contest at cboard
    
    http://cboard.cprogramming.com/contests-board/148958-pascals-bagatelle-beginner-coder-challenge.html
    
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int drop_ball(int count);
    
    int main()
    {
        int base_size = 0;
        int columns = 0;
        int *column = 0;
        int balls = 0;
        int i=0;
    
        srand ( time(NULL) );    
        
        /* get triangle size and figure everything else out */
        printf("\nbase size: ");
        scanf("%d", &base_size);
        
        /* If the triangle doesn't exist, just stop. */
        if(base_size < 1)
        {
            printf("\nReally?\n\n");
            exit(1);
        }
        
        balls = 20 * base_size;
    
        printf("ball count %d\n\n..working..",balls);
    
        columns = base_size +1;    
        column = malloc(columns * sizeof(int));
        if(!column)
        {
            printf("\n\nFailed malloc()...!");
            exit(1);
        }
        for(i=0; i< columns; ++i) 
            column[i]=0;
    
        /* drop the balls! */        
        for (i=0; i<balls; ++i)
        {
            int col = drop_ball(columns);
            ++column[col];
        }
        
        /* show the results */
        printf("\n\noutput:\n|");
        balls = 0;
        for (i=0; i<columns; ++i)
        {
            printf("%d|",column[i]);
            balls += column[i];
        }
        printf("\n\ncheck total balls distributed: %d\n", balls);
    
        free(column);
        return 0;
    }
    
    /* drop_ball()
     *
     * Given:
     *   the number of columns
     * 
     * Simulates dropping the ball on the center pin
     * at the top with a 50/50 chance of going left
     * or right on each pin.
     *
     * Returns the column that the ball will fall into
     */
    int drop_ball(int count)
    {
        int spots = count * 2;
        int cur_spot = count - 1;
        int i = 0;
    
        for (i = 0; i < count - 1; ++i)
            if ((rand() % 100) < 50)
                --cur_spot;
            else
                ++cur_spot;
        
        return cur_spot / 2;
    }
    A mention should go to whiteflags who was first through with a very good answer, that was acheived in a very compact program of around fifty actual lines, great work, I only failed it as it requested input for the number of balls in the game, which i had stated should be done programatically, a minor technicality i know..but a very worthy second place to whiteflags.
    Last edited by rogster001; 06-16-2012 at 04:29 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    92
    Hmmm, was trying to do the drop_ball() without a division and here's the first thing that comes to mind, though it is a cheap cop-out. heh

    Still thinking about a truly division-less version. Also noticed that I have a spots variable that I don't actually use (or need).

    Code:
    int drop_ball(int count)
    {
        float cur_spot = count * 0.5f; /* cheap cop-out to technically avoid division */
        int i = 0;
    
        for (i = 0; i < count - 1; ++i)
            if ((rand() % 100) < 50)
                cur_spot -= 0.5f;
            else
                cur_spot += 0.5f;
        
        count = cur_spot;
        return count;
    }
    Edit...
    Ah hah! Here it is, no division and no cop-outs.

    Code:
    int drop_ball(int count)
    {
        int cur_spot = 0;
        int i = 0;
    
        for (i = 0; i < count - 1; ++i)
            if ((rand() % 100) > 49)
                ++cur_spot;
        
        return cur_spot;
    }
    Last edited by jdragyn; 06-16-2012 at 05:58 PM. Reason: Figured out the division-less version.
    C+/- programmer extraordinaire

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Quote Originally Posted by rogster001
    A mention should go to whiteflags who was first through with a very good answer, that was acheived in a very compact program of around fifty actual lines, great work, I only failed it as it requested input for the number of balls in the game, which i had stated should be done programatically, a minor technicality i know..but a very worthy second place to whiteflags.
    Yeah well I don't want to be a noodle incident. For those curious, this was my code.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    double uniform_deviate (int seed)
    {
    	return seed * ( 1.0 / ( RAND_MAX + 1.0 ) );
    }
    void plinko (int buckets[], int nBuckets, int balls)
    {
    	int i, ball, rightBounce;
    	srand((unsigned long)time(NULL));
    	for(ball = 0; ball < balls; ball++) {
    		rightBounce = 0;
    		for(i = 0; i < (nBuckets - 1); i++) {
    			if(uniform_deviate(rand()) > 0.5) {
    				++rightBounce;
    			}
    		}
    		++buckets[rightBounce];
    	}
    }
    int main ()
    {
    	int nBuckets = 0;
    	int balls = 0;
    	int sum = 0;
    	int *buckets = NULL;
    	printf("enter the number of bottom pins: ");
    	scanf("%i", &nBuckets);
    	printf("enter the number of balls: ");
    	scanf("%i", &balls);
    	++nBuckets;
    	if((buckets = calloc(nBuckets, sizeof buckets[0])) != NULL) {
    		int i;
    		plinko(buckets, nBuckets, balls);
    		printf("\n\n|");
    		for(i = 0; i < nBuckets; i++) {
    			sum += buckets[i];
    			printf("%d|", buckets[i]);
    		}
    		printf("\n\ncheck sum = %d\n", sum);
    		free(buckets);
    	} else {
    		printf("Memory allocation failure\n");
    	}
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# for a C++ coder
    By KIBO in forum C# Programming
    Replies: 5
    Last Post: 04-07-2012, 01:10 AM
  2. Replies: 6
    Last Post: 07-26-2011, 02:57 PM
  3. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  4. new coder using Dev C++
    By newcoder3333 in forum C++ Programming
    Replies: 3
    Last Post: 07-29-2006, 05:35 PM
  5. HL2 mod needs coder
    By livewire in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 07-30-2005, 10:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21