C++Programming help!! need help writing/fixing

This is the assignment from class i need to complete:
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) and is going forward).

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.

When ever i run my program i get less then 1% which I'm told is wrong. I should be getting 1.2-3.4% if it starts at zero. and 4.5-5.5% if it starts at 1
please help me
Last edited on
show ur program than i may help you
This code might be a little messy, but when i get it to work i will neaten it up
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
using namespace std;



int main (){  

	int particles = 1000, direction = 0, oldd=0, collision = 0, forward =0;
	double escape=0; 
    const int FORWARD = 1;
	const int BACKWARD = 2;
	srand(time(NULL));
	
	while (particles != 0){

		oldd=0;// set all values back to default before loop
		forward = 0;
		
		for (collision = -1; collision < 10;){

		direction = rand() % 4 + 1; //pick a number 1 through 4

		if (oldd!=direction){//compare old direction to direction
                collision++;
                oldd = direction;
}			
		if (direction==FORWARD){// partical moves forward

			++forward;
		}
		if (direction==BACKWARD){// if partical moves backward
			--forward;}
		
		if (forward>5 && collision <10){
			++escape;
		}

		}// end for loop
				
		particles--;// partical has died
		}
		
		cout << escape/1000 << " % of the particals escaped\n"; 
			
	return 0;

}
closed account (D80DSL3A)
Your code has some logical problems.

1) Easy. On line 17 you assign oldd = 0 as the initial value for each particle, but this is wrong. From the directions:
(You may assume the radioactive particle begins in the shield( position zero) and is going forward).

The bold part means that oldd = FORWARD (1) is correct. I think that this error would push %escaped downward.

2) Why is collision = -1 the initial value in the for loop on line 20? I think that collision = 0 is right. Loop then goes through for collision = 0 to collision =9 (inclusive) which is 10 times.

Logical errors:
1) Line 36: You increment escape, but allow the simulation to continue. You should break from the for loop when a particle escapes. However, I think that this error would result in a number that is too high(particles may be counted as escaping multiple times), not too low.

2) You are not checking if forward < 0 (particle returns to container)
After correcting above error, repeat the code to check if( forward<0 && collision <10 ). Not sure how failing to do this should affect the %escaped figure.

I worked this problem (a bit differently) and got over 100 simulations:
with initial position = 0: average %escaped = 2.06%
with initial position = 1: average %escaped = 5.35%
I put oldd to be 0 because i know it will never equal the direction so it increases collision by one.
after the very first if statement in the for loop, collision will always be 0 when it starts a new particle.
i will go through the problem following your tips

-thanks for your help

i forgot to say that he doesn't want us using break statements as they are signs of poor programming
Last edited on
is there a better way to end the simulation for a particle?
closed account (D80DSL3A)
is there a better way to end the simulation for a particle?


Yes. Replace line 20 with:
while( collision < 10 && (forward >= 0 && forward < 6) )
and rework loop logic accordingly.
Here is my current source code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
using namespace std;



int main (){  

	int particles = 1000, direction = 1, oldd, collision = 0, forward =0;
	double escape=0; 
    const int FORWARD = 1;
	const int BACKWARD = 2;
	srand(time(NULL));
	
	while (particles != 0){
		oldd=1;
		forward=0;
		collision=0;

		while( collision < 10 && (forward >= 0 && forward < 6) ){
			
		direction = rand() % 4 + 1; //pick a number 1 through 4

		if (oldd!=direction){//compare old direction to direction
                collision++;
                oldd = direction;
}			
		if (direction==FORWARD){// partical moves forward

			++forward;
		}
		if (direction==BACKWARD){// if partical moves backward
			--forward;}
		

	
		}
		if (forward>=6 && collision <10){
			++escape;
		}
		particles--;// partical has died
		}
		
		cout << escape/1000 << " % of the particals escaped\n"; 
			
	return 0;

}


I think i found my error ( not fixed above). it was how i calculate percent my answer is in decimal form, but i think i need to multiply it by 100 to get it in the context that I'm outputting it as.

can anyone verify this?

Also please check if it meets all requirements... the while line from fun2code checks if it moves back into reactor hits 10 collisions, and if it moves forward 6 times. the if statement after should check if particle escaped
Last edited on
i forgot to say that he doesn't want us using break statements as they are signs of poor programming


I find that bewildering statement. Are you sure that is exactly what your teacher said? Was he talking about goto?

Programmers use break & continue all the time - is a break in a switch statement invalid?

How else does one leave a loop early?
"How else does one leave a loop early?"

By setting a flag during the body of the loop, and then checking that flag as part of the loop condition. Personally, I prefer that, as it makes the flow of logic through the code more straightforward - there's one point at which you make the decision whether to continue looping or not, and (assuming you've used sensible names for your flags and variables) the logic used to make the decision should be clear.
By setting a flag during the body of the loop, and then checking that flag as part of the loop condition.


But what about the rest of the code in the body of the loop? And it will complicate the loop end condition, especially if there are multiple reasons (multiple places) to exit the loop.

Personally, I prefer that, as it makes the flow of logic through the code more straightforward


Not really, because you need extra logic not to do the code in the rest of the body of the loop - as well as extra in the loop condition. It is very simple and logical to use a break.

Your idea is a recipe for complication.

Of course the other way is to return out of the function, but that might not always be convenient - you just want to exit the loop, do something else, then return.

Consider this psuedo code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
while(end-cond1 || (end-cond2 && end-cond3 ) ) {

     if (cond1) {
         //some code
        continue;
    }
    else if (cond2) {
           //found the answer
           //do some normal code
          return some value
    }
    else if (err-cond1) {
           //some code
           break;
    }
    else if (err-cond2) {
            //some code
           break;
    }
    else  {  //some other error

            //some code
           break;
    }
}


So in that code there are 3 normal end conditions for the loop, and 5 other conditions. Imagine if was even worse with even more else if's. And you can't use a switch because the else if clause don't involve integers. Switches normally have breaks after each case clause.

How would you propose to do it without break statements? With 8 end conditions plus more logic to not do the code in the rest of the body of the loop?

Of course the other aspect of this is to use exceptions to process errors - but what if they aren't errors? It is the same as a list of else if clauses doing the same as a switch for the reasons I mentioned above. The breaks are needed for the same reason they are needed in a switch.

I look forward to your answer.
Last edited on
Topic archived. No new replies allowed.