Write a Pseudocode for the following instructions:

Click For Summary

Discussion Overview

The discussion revolves around writing pseudocode for two tasks: calculating the factorial of a number N and simulating the roll of a die 200 times, including the calculation of the relative frequency of the number 4 appearing on the die. Participants express uncertainty about pseudocode syntax and logic, and they share various approaches and corrections to each other's attempts.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants propose a method for calculating the factorial using a loop, but express confusion over the correct implementation, particularly regarding variable management.
  • Others suggest using a simpler approach for the factorial calculation, such as initializing a variable and multiplying it in a loop.
  • For the die rolling simulation, participants discuss the need to generate a random integer between 1 and 6 and to keep track of how many times a 4 is rolled.
  • There are suggestions to include an "end while" statement in the pseudocode to clarify loop termination.
  • Some participants question the meaning of variable names like "i" in loop constructs and express uncertainty about pseudocode conventions.
  • Corrections are made regarding the terminology of "dice" versus "die" in the context of rolling a single die.

Areas of Agreement / Disagreement

Participants generally agree on the tasks to be accomplished but present multiple competing views on how to implement the pseudocode correctly. The discussion remains unresolved regarding the best practices for writing pseudocode and the specifics of variable management.

Contextual Notes

There are limitations in the participants' understanding of pseudocode syntax and logic, leading to confusion over variable definitions and loop structures. Some assumptions about the programming language context are also not explicitly stated.

Who May Find This Useful

Students learning about pseudocode, those interested in programming logic, and individuals seeking to understand factorial calculations and random number generation in coding contexts may find this discussion useful.

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 ·
Replies
15
Views
6K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 53 ·
2
Replies
53
Views
10K
  • · Replies 1 ·
Replies
1
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
33
Views
4K
  • · Replies 27 ·
Replies
27
Views
5K
Replies
4
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K