Image

Imagepixelpoet wrote in Imagecpp




give me feedback....*kabang

the link to the assignment as1

-----------THIS IS THE WALK.CPP -----------

//**The purpose of this program is to simulate a random walk in one dimension**

//*************************************NOTE: sorry the < and > doesnt show~~~~~~~~~~~~~~

#include
[Error: Irreparable invalid markup ('<math.h>') in entry. Owner must fix manually. Raw contents below.]

<font size=1>
<lj-cut text="Here's my code for as1, *enjoy*">

give me feedback....*kabang

the link to the assignment <a href="http://www.cs.ucr.edu/~deganit/12/prog/p1.htm">as1</a>

-----------THIS IS THE WALK.CPP -----------
<font size=1>
//**The purpose of this program is to simulate a random walk in one dimension**

//*************************************NOTE: sorry the < and > doesnt show~~~~~~~~~~~~~~

#include <math.h>
#include "dice.h"
#include <iostream.h>
//function prototype for walk()
int walk(int, int &, int &);

void main()
{
//declare position, farthest, orig, because it is pass by reference
//declare hops for input

int position, farthest, orig, hops;

//prompts user for hops
cout <<"How many hops? " ;
cin >> hops;
//Dummy checks for errors, non positive values and characters
while (cin.fail()|| hops < 0)
{
cin.clear();
cin.ignore(80, '\n');
cout << "Error, please enter correct number of hops: ";
cin >> hops;
}

cout <<endl;

//calls walk function, and saves it to position
position = walk(hops, farthest, orig);

//puts everything in a nice output statement
cout <<"The frog hopped " << hops <<" times, ending at point "
<<position << ".\n";
cout <<"On its way, it landed on the origin " << orig << " times.\n"
<<"The farthest distance from the origin was " << farthest
<<".\n";

}
//function walk passes 3 parameters
int walk(int hops, int &farthest, int &orig)
{
//declare a dice and change NumSides to 2 for the coin toss, set
//distance, orig, farthest to zero for initialization
int left=0;
int right=0;
Dice d;
d.setNumSides(2);
int distance = 0;
orig = 0;
farthest = 0;

//loops hops times
for(int i = 0; i < hops; i++)
{
//tosses coin
int roll = d.roll();

//checks whether it is going left or right
//(1=left) and (2=right)
if( roll == 1)
{
distance--;
}
else
{
distance++;
}

//checks if it is at origin
if(distance == 0)
{
orig++;
}

//If the current distance is greater than the farthest distance on
//the left then hold that number as the farthest, vice versa for right
if(distance<left)
{
left = distance;
}
else if(distance>right)
{
right = distance;
}
cout <<distance <<endl;
}

//compare the greatest abs. left and right distance, return greatest of the two
if(fabs(left) > right)
{
farthest = (int)fabs(left);
}
else
{
farthest = right;
}
return distance;
}


***************************************************************************THEN

//~~~~~~~~~~~the dice.h file



#ifndef _DICE_H_
#define _DICE_H_

const int DEFAULT = 6; // default number of sides

class Dice
{
public:
Dice(); // constructor
~Dice(); // destructor
int roll(); // returns a random roll
int getNumSides(); // number of sides of the die
void setNumSides(int); // sets the number of sides
int getNumRolls(); // number of times die was rolled

private:
int num_sides; // number of sides on the die
int num_rolls; // number of times die was rolled
};

#endif


********************************dice.cpp~~~~~~~~~~~~~~~~~~~~
//NOTE sorrie the < and > doesnt show

#include <iostream.h> // needed for cin & cout
#include <stdlib.h> // needed for rand & srand
#include <time.h> // needed for time

#include "dice.h"

Dice::Dice()
//
// Constructor. Creates a Dice object with DEFAULT sides.
// Also sets the random number generator for the rolls.
//
{
num_sides = DEFAULT;
num_rolls = 0;

static int seed = 0; // generator only first time thru
if (seed == 0)
{
seed = 1;
srand(time(NULL)); // use time to get seed
}
}

Dice::~Dice()
//
// Destructor.
//
{
}

int Dice::roll()
//
// Rolls the die and returns an integer
// between 1 and num_sides, inclusive.
// Increments the number of rolls.
{
num_rolls++;
return ((rand() % num_sides) + 1);
}

int Dice::getNumSides()
//
// Returns the number of sides the die has.
//
{
return num_sides;
}

void Dice::setNumSides(int sides)
//
// Sets the number of sides of the die to sides.
//
{
num_sides = sides;
}

int Dice::getNumRolls()
//
// Returns the number of times the die was rolled.
//
{
return num_rolls;
}