Write a Pseudocode for the following instructions:

Tags:
1. Feb 26, 2015

angeliKITTYx

1. The problem statement, all variables and given/known data
- Calculating the factorial of a number N
- For simulating the roll of a die 200 times (include the calculation for the relative frequency of the number 4 in the upface of the die within the 200 rolls)

2. Relevant equations
None?

3. The attempt at a solution
I am taking this class online and I am far from understanding pseudocode...
For example, if N was 7...

Set N to 7
Set M to 0

While (N>=M)
X = N - M
M = M+1
X = X(N - M)
M = (M+1)

This almost works, but the loop doesn't multiply ALL of the terms together... it just moves down the line of terms.

Second one:

Set roll to 1
While roll =< 200
Roll the dice
Read the upface of the dice
Set roll to roll + 1

Not sure about the probability of the 4...

2. Feb 26, 2015

Staff: Mentor

I'll start with your second problem first. "Roll the dice" -- actually it's "roll the die" since there is only one die. More importantly, you need to elaborate on what "roll the die" means, which would be to generate a random integer in the range 1, 2, 3, ..., 6.

You also need logic to keep track of how many times a 4 occurs. If a 4 occurs, increment a variable to indicate this event. The number of times a 4 is rolled divided by 200 is the relative frequency.

Also, you should have an "end while" pseudocode statement or something similar to mark the end of your while loop.

3. Feb 26, 2015

Quantum Defect

For the factorial question, a simple way is simply:

Fact = 1
for i = 1 to N
Fact = Fact * i

Computer languages let you use expressions like Fact = Fact*i, where "Fact" on the right side is the current value of "Fact", and "Fact" on the left is the new value of "Fact" after the operation. This way, you do not need to do the bookkeeping that you are trying to do with M.

For the dice roll:

Four = 0 {the number of rolled fours -- set to zero before the rolls are done}
For i = 1 to 200
{Start of For loop}
Roll the die
If die = 4 then Four = Four + 1 {If four shows, increase the count by one}
{End of For Loop}
Freq_of_Four = Four/200

4. Feb 26, 2015

angeliKITTYx

Okay, well I can fix the "dice" to "die," but I just don't understand the language to use... would I just say this: ?

Set roll to 1
While roll =< 200
Generate integer (1,6)
Set roll to roll + 1

Last edited: Feb 26, 2015
5. Feb 26, 2015

angeliKITTYx

Fact = 1
for i = 1 to N
Fact = Fact * i

Okay so for this last line, is it repeating itself because i is a range?

And the second one...
For i = 1 to 200

Does lowercase i mean something? Like For and While (etc) do?
I don't see i anywhere else in the second one other than that line...

6. Feb 26, 2015

BiGyElLoWhAt

Hey, welcome to PF!
So a good way to approach this, and actually see what's happening, physically write down the variables for the first few iterations of your loop. This way you can see what line it's breaking on.
N=7
M=0
(loop iteration #1)
X=7-0=7
M=0+1=1
X=7(7-1)=7(6)=42
M=1+1=2
(loop iteration #2)
X=7-2=5 (uh oh, now the variable that was holding the 42 has been set equal to 5)
M=2+1=3 (did we handle the M=2 case? or did that just get skipped?)
X=7(7-3)=7(4)=28
M=3+1
...
So there are some problems with your X definition (or rather RE-definition), you lose the previous value each time the loop starts over. There's also a bug in your M counter.

7. Feb 26, 2015

BiGyElLoWhAt

What language are you using?

8. Feb 26, 2015

angeliKITTYx

Pseudocode... that's all the question says.

9. Feb 26, 2015

BiGyElLoWhAt

Ok, that's fine. Normally pseudocode gets written for a class, which has a goal to teach you a specific language. It doesn't really matter, but different languages tend to treat loops differently. So how can you fix part A?

10. Feb 26, 2015

Quantum Defect

For pseudocode, a lot depends upon what kind of code the thing will be written into and how much detail you need to provide.

For example, I had put "Roll die" in the example that I wrote above, as shorthand for a number of steps that I would need if I coded this. Some languages that I have used have a random number generator that will return a real number between zero and one, while others have something that will return a random number within a certain range. If I used the former, I could divide the inerval 0..1 into 6 equally-sized bins, and assign an integer value to the roll, depending upon which bin the random value fell into. If I only cared if the roll generated a "4" I would only need to check to see if the random number fell within the fourth bin, and if it did to increment the counter.

A lot of times, you will write a program to be as efficient as possible, other times, you might make it flexible if you thought that you might like to tweak the program to do other things at a later date.

In your example above, the skeleton is ok, but you need to keep track of the number of fours you roll -- so you need a counter of "fours" as well as a counter of rolls.

11. Feb 26, 2015

Quantum Defect

Most languages have a "For" loop You could also do the same thing with a "while" like you had:

Fact = 1
N = 7
M = 1
While M<=N
{Start of loop}
Fact = Fact * M
M = M+1
{End of loop}

The thing you always want to do when you write these kinds of loops is to check that they are doing what you want to do. Do they end at the right point? Do they get stuck?
For example, if I forgot to add the last step in the loop, M would always be 1, and the loop would never exit. M would always be less than N.

The lower case "i" is nothing special, no special meaning.

On the For loop, there needs to be a counter of the number of cycles that the loop executes. This counter never actually has to be used in any of the operations. I could have used it if I wanted to watch the probability of rolling a four converge to 1/6:

E.g.

Four = 0 {the number of rolled fours -- set to zero before the rolls are done}
For i = 1 to 200
{Start of For loop}
Roll the die
If die = 4 then Four = Four + 1 {If four shows, increase the count by one}
Freq_of_Four = Four/i
Output (Freq_of_Four)
{End of For Loop}

In this case, the program will be significantly slower than the first one because at every step, I am doing an additional division operation. In the first example, I did the computation of the frequency of rolling a four only one time, at the very end.