Write a Pseudocode for the following instructions:

AI Thread Summary
The discussion focuses on writing pseudocode for two tasks: calculating the factorial of a number N and simulating the roll of a die 200 times while calculating the relative frequency of rolling a 4. For the factorial, the proposed pseudocode needs refinement to ensure all terms are multiplied correctly, with suggestions to simplify using a loop that multiplies from 1 to N. For the die simulation, it's emphasized that a random integer between 1 and 6 should be generated for each roll, and a counter for the number of times 4 is rolled must be included. The relative frequency of rolling a 4 is calculated by dividing the count of 4s by the total rolls. Overall, clarity in pseudocode structure and logic is crucial for both tasks.
angeliKITTYx
Messages
8
Reaction score
0

Homework Statement


- 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)

Homework Equations


None?

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...
 
Physics news on Phys.org
angeliKITTYx said:

Homework Statement


- 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)

Homework Equations


None?

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...
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.
 
angeliKITTYx said:

Homework Statement


- 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)

Homework Equations


None?

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...

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
 
Mark44 said:
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.

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)
Read result
Set roll to roll + 1
 
Last edited:
Quantum Defect said:
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
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...
 
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.
 
What language are you using?
 
BiGyElLoWhAt said:
What language are you using?
Pseudocode... that's all the question says.
 
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
angeliKITTYx said:
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)
Read result
Set roll to roll + 1

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
angeliKITTYx said:
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...

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.
 

Similar threads

Replies
15
Views
5K
Replies
2
Views
2K
Replies
21
Views
3K
Replies
53
Views
9K
Replies
1
Views
7K
Replies
2
Views
2K
Replies
27
Views
5K
Back
Top