RSS

My own Coding Problem

29 Jun

ImageI’ve been considering a new classroom exercise to describe genetics and the inheritance of alleles within a population. The idea is that every student draws two coins randomly to start – these represent two alleles for a single gene in that organism. Then each student has the opportunity to ‘breed’ with another student to make two offspring. The genetics of the offspring will be determined randomly from that of the parents (something like each parent flips their coins until they get one heads and one tails – the heads from each parent goes to the offspring).

We’ll do this for several generations and track the frequency of alleles through time.

 

OK – now to coding:

I thought this sounded like a simple project that could be simulated easily, so that’s a coding challenge for myself. To start with, I thought I would build the program up bit by bit doing unit testing along the way. At this point I am trying to just set up the parental generation. To do this, I:

1. get input from the user: how many people are playing?

                                          how many different alleles for the gene exist?

2. loop through making ‘User’ objects for each person playing. Each User object has an id number for tracking (idNum) and two alleles determined randomly(alleles[0,1]). I was thinking of keeping my list of users as an array called players[0…10max]

But, I’m stuck already!

I am getting and using my userData with no problem. I think I am even setting up my objects alright and assigning them two randomly determined elements. But instead of those elements being coins (representing alleles), I’m getting the address of a char within the first denomination….

Any help at all is greatly appreciated, but remember – I’m doing this to learn, so I need to be able to understand the solution, not just get it solved.

 

Here’s the code with some debugging output statements:

//

//  main.cpp

//  CoinsLabSimulation

//

//  Created by Jack on 6/27/13.

//  Copyright (c) 2013 Jack Treml. All rights reserved.

//

//A simulation game to test how my coins lab idea will work

#include <iostream>

#include <cmath>

#include <time.h>

using namespace::std;

class User{

private:

    int idNum;

    string alleles[2];

public:

    User(int A){idNum=A;};//int myArray[] = new int[idNum];

    char getAlleles();

    void setAlleles(string, int[]);

};

void getInput(int userInput[]);

void randomAssignment(int userInput[], string denominations[], int players[]);

int main()

{

    srand(time(NULL));

    int userInput[2];

    char p[10] = {“pennies”};   //I hate character arrays / strings in C++

    char n[10] = {“nickels”};   //I want an array of the coins we will use as alleles in this game

    char d[10] = {“dimes”};

    char q[10] = {“quarters”};

    char s[15] = {“SusanBAnthonys”};

    string denominations[5] = {p,n,d,q,s}; //this needs to be fixed so I can call on this array for coin types

    int players[10];

    getInput(userInput);

    cout<<“We will be playing with “<<userInput[0]<<” players and “<<userInput[1]<<” alleles”<<endl;

    cout<<“Alleles used will be:”<<endl;

    for (int i =0; i<userInput[1]; i++) {

        cout<<denominations[i]<<endl;

    }

    randomAssignment(userInput, denominations, players);

    return 0;

} //end main

//*****************non-member functions

void getInput(int usersChoices[]){  //fx to retreive user settings –unit test passed

cout<<“How many players will be participating(10 max)?”<<endl;

cin>>usersChoices[0];

cout<<“How many denominations would you like to use?”<<endl;

cin>>usersChoices[1];

}// end getInput

void randomAssignment(int userNumbers[], string allelesUsed[], int player[]){ //will create user objects and assign each alleles

    //————-   unit test:

    cout<<endl<<endl<<“Unit Test”<<endl;

     cout<<allelesUsed[0]<<”    “<<allelesUsed[3]<<endl<<endl;

    //————-   pass

    User *a[10];

    for (int i=0; i<userNumbers[0]; i++) {

        a[i] = new User(i);

        a[i]->setAlleles(*allelesUsed,userNumbers);  ///trace this

    }

}// end randomAssignment

//******************User Member Methods

void User::setAlleles(string allelesUsed,int userNum[]){

    int max = userNum[1];

    int randomNumber = floor(1 + (rand() % max));

    cout<<“Random number: “<<randomNumber<<endl;

    string allele1 = &allelesUsed[randomNumber];

    randomNumber = floor(1 + (rand() % max));

    cout<<“Random number: “<<randomNumber<<endl;

    string allele2 = &allelesUsed[randomNumber];

    alleles[0] = allele1;

    alleles[1] = allele2;

    //———–unit test2

    cout<<“UNIT TEST2::::Alleles for this player are: “<<allele1<<” and “<<allele2<<endl;

    //———–fail – it’s using the random number as an address within ‘pennies’

    cout<<“Alleles for this player are: “<<alleles[0]<<” and “<<alleles[1]<<endl;

}

char User::getAlleles(){}

————————————————————-

my output looks like this:

How many players will be participating(10 max)?

2

How many denominations would you like to use?

4

We will be playing with 2 players and 4 alleles

Alleles used will be:

pennies

nickels

dimes

quarters

 

 

Unit Test

pennies    quarters

 

Random number: 4

Random number: 2

UNIT TEST2::::Alleles for this player are: ies and nnies

Alleles for this player are: ies and nnies

Random number: 1

Random number: 2

UNIT TEST2::::Alleles for this player are: ennies and nnies

Alleles for this player are: ennies and nnies

 

 

Advertisements
 
1 Comment

Posted by on June 29, 2013 in Uncategorized

 

Tags: , , , , , , , ,

One response to “My own Coding Problem

  1. downhousesoftware

    June 29, 2013 at 12:08 pm

    Ha! I see the problem!
    I’m calling ‘allelesUsed’ instead of ‘allelesUsed[]’!!!

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: