Simulations

Writing a program and I need help. should I be using for loops, while loops? Am I on the right track?


Here is the problem:

Exercise One: Simulation Program
A circular slab of uniform thickness (5 units) (cross-section of the slab is illustrated below) is to be used to
shield a nuclear reactor. A radioactive particle entering the shield follows a random path by moving forward,
backward, left, or right with equal likelihood, in jumps of one unit (You may assume the radioactive particle
begins in the shield( position zero) ).
A change in the direction of the particle is interpreted as a collision with a Lead atom in the shield. After 10
collisions the particle’s energy has dissipated and will not escape the shield.
If the particle moves forward 6 positions (without exceeding 10 collisions), it has escaped the shielding. If the
particle returns to the reactor, it is has not escaped the shield.
Write a program to simulate 1000 particles entering the shield and determine what percentage of the particles
escape the shielding. Note: The simulation for each particle will end when any of the events described
above has occurred.


Here is what I have so far:

#include <iostream>
#include <fstream>
#include <cctype>
#include <ctime>
#include <cstdlib>
using namespace std;

const int PARTICLES = 1000,
MAX_COLLISIONS = 10; // once it collides 10 times, it wont escape

int main() {

int numCollisions,
direction,
prevDirection = 0, //tracks the previous direction
forward = 0,
backward= 0,
left = 0,
right = 0;
double pctEscaped; // percentage of particles escaped

//srand(time(NULL));

direction = rand() % 4 + 1;

switch (direction) {
case 1: forward++;
break;
case 2: backward++;
break;
case 3: left++;
break;
case 4: right++;
break;
default:;
} // end switch





return 0;

} // end main
Please always use code tags. Your on the right track, keep writing your code and if you run into a specific problem we would be happy to help you :D. Good luck!
The difference in loops depends on what you are doing. All loops do essentially the same thing; they repeat code. A for loop is good if you are using variables that are getting added a constant amount each cycle.

for (int i = 0; i < x; i++)
{
}

A while loop is good if you just want a piece of code to run until some arbitrary condition (or set of conditions) is met.

while (TRUE)
{
}

happy coding!
#include <iostream>
#include <fstream>
#include <cctype>
#include <ctime>
#include <cstdlib>
using namespace std;

const int PARTICLES = 1000,
MAX_COLLISIONS = 10, // once it collides 10 times, it wont escape
MAX_VALUE = 4,
MIN_VALUE = 1;
int main() {

int numCollisions = 0,
direction,
prevDirection = 0, //tracks the previous direction
forward = 0,
backward = 0,
left = 0,
right = 0,
numEscaped;
double pctEscaped; // percentage of particles escaped

//srand(time(NULL));

for (; numCollisions <= MAX_COLLISIONS) {

direction = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
// ^ formula for rand numbers 1-4

switch (direction) {
case 1: forward++;
break;
case 2: backward++;
break;
case 3: left++;
break;
case 4: right++;
break;
default:;
} // end switch

if (prevDirection != direction) {
numCollisions++;
prevDirection = direction;
} // end if


cout << direction << endl;

this is what I have so far. I am wondering how I can modify it so that it will count the number of particles that have more than 10 collisions, so it can count it as dead.
Use a variable like int numOfDead then every time a particle has more than ten collisions add one to int numOfDead.
Well my while loop is only executing until 10 collisions, and once it hits 10 collisions it stops because once a particle hits more than 10 collisions, that is when it dies so the question is how do i modify my loop so that it will count even the particles with more than 10 collisions
Could you post that part of the code.
#include <iostream>
#include <fstream>
#include <cctype>
#include <ctime>
#include <cstdlib>
using namespace std;

const int MAX_COLLISIONS = 10, // once it collides 10 times, it wont escape
MAX_VALUE = 4,
MIN_VALUE = 1;
int main() {

int particles = 1000,
numCollisions = 0,
direction,
prevDirection = 0, //tracks the previous direction
forward = 0,
backward = 0,
left = 0,
right = 0,
numEscaped = 0;
double pctEscaped; // percentage of particles escaped

srand(time(NULL));

while(numCollisions <= MAX_COLLISIONS) {

direction = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
// ^ formula for rand numbers 1-4

switch (direction) {
case 1: forward++;
break;
case 2: backward++;
break;
case 3: left++;
break;
case 4: right++;
break;
default:;
} // end switch

if (prevDirection != direction) {
numCollisions++;
prevDirection = direction;
} // end if

if (forward >= 6) {
numEscaped++;
break;
}

particles--;

} // end

cout << particles << endl;
cout << numEscaped << endl;

cout << "forward " << forward << endl;
cout << "backward " << backward << endl;
cout << "left " << left << endl;
cout << "right " << right << endl;

return 0;

} // end main



the cout at the end are for my own purposes to make sure it works and will be deleted at the end. But my problem is that it only gets like 3 or 4 that escape
Sorry Im a bit confused. Do you need help finding a way to count the amount of dead particles or is there a problem with only a few particles that are escaping.
Topic archived. No new replies allowed.