Write a Pseudocode for the following instructions:

In summary: X is the temporary variable)i=1 X=7-0 M=0+1i=2 X=7-1 M=1+1i=3 X=7-2 M=2+1This keeps going until the end condition is reached. So your code is actually working as intended, but you're overwriting the results. You need to have a sum variable or an accumulator to keep track of the multiplication.In summary, to calculate the factorial of a number N, a simple way is to use a for loop that multiplies all numbers from 1 to N, using an accumulator variable to keep track of the result. For simulating the roll of a die 200 times, you can
  • #1
angeliKITTYx
8
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
  • #2
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.
 
  • #3
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
 
  • #4
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:
  • #5
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...
 
  • #6
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
What language are you using?
 
  • #8
BiGyElLoWhAt said:
What language are you using?
Pseudocode... that's all the question says.
 
  • #9
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.
 

1. What is pseudocode?

Pseudocode is a simplified, informal way of writing out the steps or instructions for a computer program. It uses a combination of natural language and programming code to outline the logic and structure of a program.

2. Why is pseudocode used?

Pseudocode is used as a planning tool for writing code because it allows the programmer to focus on the logic and algorithms of the program without worrying about specific syntax or programming language rules.

3. How do you write pseudocode?

Pseudocode is typically written in plain English or a language that is familiar to the programmer. It should be easy to understand and follow, with clear and concise instructions that outline the main logic and steps of the program.

4. Is pseudocode the same as a programming language?

No, pseudocode is not a programming language. It is simply a way of expressing the logic and structure of a program in a more informal and human-readable way. It cannot be compiled or directly executed by a computer.

5. Can pseudocode be used to write any type of program?

Yes, pseudocode can be used to plan and write any type of program. It is a flexible and versatile tool that can be adapted to different programming languages and problem-solving scenarios.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Precalculus Mathematics Homework Help
2
Replies
53
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
933
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Precalculus Mathematics Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
27
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
Back
Top