Learn How to Write Pseudocode: Questions Answered!

  • Comp Sci
  • Thread starter Sunwoo Bae
  • Start date
  • Tags
    Writing
In summary, pseudocode is a simplified programming language used to plan out the logic and structure of a program without worrying about specific syntax or language rules. It helps programmers break down complex tasks into smaller, more manageable steps and is often used as a first step in the coding process. Pseudocode is not an actual programming language, but rather a tool for planning and organizing code. It can be written in plain English or using programming terminology, and can also be used to communicate ideas and concepts to non-technical team members. Overall, learning how to write pseudocode can greatly improve a programmer's efficiency and effectiveness in developing programs.
  • #1
Sunwoo Bae
60
4
Homework Statement
Take as input a list of integers A and find the sum of three maximum numbers
Relevant Equations
.
I just learned about pseudocode, and I am confused about how I should use/write it.
For the question,

1: sum ← 0
2: For i in range (3) do:
3: sum ← sum +max(A)
4: remove max(A) from list A
5: return sum

The following is the kind of pseudocode I want to write.

I have several questions:
Is using functions like max() okay when writing pseudocode?
Would it be right to say that the above code can be described by T(n) = an + b and will thus have time complexity of Θ(n), as there is no nested loop?
Is using informal language like the fourth line (remove max(A) from list A) okay?

Thank you ahead!
 
Physics news on Phys.org
  • #2
Don't overthink this. Pseudo-code is an informal way of describing an algorithm.

Some people use a python-like pseudo-code style with minimal syntactic elements. Python style is nice because the indentation helps organize the pseudo-code.

This wiki article on pseudo-code shows several styles.

https://en.wikipedia.org/wiki/Pseudocode

I favor the python style (not listed) for the reasons I mentioned earlier.

There is also Structured English as a style of pseudo-code that works well until you have many decisions to make.

https://en.wikipedia.org/wiki/Structured_English

Lastly, there is also an actual programming language called Andover Plain English used in Building Mgmt. Systems to monitor and control AC heat, lighting... and while not pseudo-code emulates it to some degree.

An example of Andover Plain English is shown below:

https://github.com/mitchpaulus/andover-plain-english-udl/blob/master/example.png
 
Last edited:
  • Like
Likes PeroK
  • #3
Sunwoo Bae said:
Homework Statement:: Take as input a list of integers A and find the sum of three maximum numbers
Relevant Equations:: .

I just learned about pseudocode, and I am confused about how I should use/write it.
For the question,

1: sum ← 0
2: For i in range (3) do:
3: sum ← sum +max(A)
4: remove max(A) from list A
5: return sum

The following is the kind of pseudocode I want to write.

I have several questions:
Is using functions like max() okay when writing pseudocode?
Would it be right to say that the above code can be described by T(n) = an + b and will thus have time complexity of Θ(n), as there is no nested loop?
Is using informal language like the fourth line (remove max(A) from list A) okay?

Thank you ahead!
The most important thing about pseudocode is that it should be understandable by someone who doesn't know the target programming language. And, the focus is on the logic flow of what your program is doing; and, not the precise syntax of the coding languague.
 
  • Like
Likes FactChecker
  • #4
PeroK said:
The most important thing about pseudocode is that it should be understandable by someone who doesn't know the target programming language. And, the focus is on the logic flow of what your program is doing; and, not the precise syntax of the coding languague.
what he said (small).jpg
 
  • Like
Likes PeroK
  • #5
Your pseudocode is a good example of the benefit of pseudocode. It allows you to discuss an algorithm before getting into the details of making compilable code. For instance, I think you want to keep an updated list, Max3, of the three largest to date. For each new number, see if it should replace one of the Max3 entries and update the sum appropriately. Can you rewrite your pseudocode for that or, if I have misunderstood, make your current version clear?
 
  • #6
A couple of observations:

It is not obvious (to me) what "i in range (3)" is meant to do. (I am quite familiar with For and Do loops - that's not the problem.) What is range(3)? I mean, presumably you're wanting to iterate through the (ostensibly) three inputs, but your use of "range" and "3" doesn't make that clear.

But, more-to-the-point: I think you have misunderstood the algorithm. It does not specify only three inputs. It could be any number. But the idea is to sum only the largest three.
 
Last edited:
  • Like
Likes FactChecker
  • #7
DaveC426913 said:
It is not obvious (to me) what "i in range (3)" is meant to do. (I am quite familiar with For and Do loops - that's not the problem.)
This is a standard Python idiom. "for i in range(3)" means to iterate something for i = 0, 1, and 2. IOW, for values starting at 0 up to, but not including 3.

for i in range(0, 3) is exactly the same, again starting at 0 and going up to, but not including, 3.

for i in range(1, 6, 2) starts i off at 1, but with steps of 2. Here i takes on values of 1, 3, and 5.
 
Last edited:
  • #8
Mark44 said:
This is a standard Python idiom.
Yeah, which is why it doesn't really make good pseudocode. I'm being a bit nitpicky here. Every language has a for loop.

The bigger question is why the hard-coded 3?
The answer is: because of the misinterpretation of the stated problem.
 
  • #9
DaveC426913 said:
Yeah, which is why it doesn't really make good pseudocode.
Sure it does. There's not just one style of pseudocode. As @jedishrfu mentioned, a Python-ish style is just one possibility.
DaveC426913 said:
Every language has a for loop.
But Python's for loop uses range() exclusively, and is unlike for loops in most other high-level languages.
DaveC426913 said:
The bigger question is why the hard-coded 3?
Most likely because the code is supposed to find the 3 largest numbers in the list.
 
  • #10
Mark44 said:
Sure it does. There's not just one style of pseudocode. As @jedishrfu mentioned, a Python-ish style is just one possibility.

But Python's for loop uses range() exclusively, and is unlike for loops in most other high-level languages.
One point of pseudocode is that it should be readable by someone whodoesnt know the language.
Mark44 said:
Most likely because the code is supposed to find the 3 largest numbers in the list.
Tjats not what it's doing
 
  • #11
Mark44 said:
This is a standard Python idiom. "for i in range(3)" means to iterate something for i = 0, 1, and 2. IOW, for values starting at 0 up to, but not including 3.

for i in range(0, 3) is exactly the same, again starting at 0 and going up to, but not including, 3.

for i in range(1, 6, 2) starts i off at 1, but with steps of 2. Here i takes on values of 1, 3, and 5.
The fact that this is only an exact description in the Python language is a handycap for pseudocode. Pseudocode is better if it is understandable by non-Python people.
 
  • Like
Likes phinds and DaveC426913
  • #12
When i write pseudocode for loops i revert to a BASIC style

For i= 1,10 step 2

Or

For i=1 to 10 step 2
 
  • Like
Likes FactChecker
  • #13
DaveC426913 said:
Tjats not what it's doing
Right, but I offered a possible explanation of why 3 was hardcoded. Whether the algorithm is correct is a different matter.
 
  • #14
Pseudo code is pseudo code it’s not a language and doesn’t need to follow any particular style. We can agree to disagree as to how something should be written with the onus on the writer to make it understandable to his/ her audience.

I think we’ve pretty much exhausted this topic and so without further ado I hereby close this thread and thank all who contributed here.
 
  • Like
Likes Mark44, berkeman and FactChecker
  • #15
@Sunwoo Bae your code is not wrong, I think @Mark44 and @DaveC426913 have misread it due to formatting - you should post code (even pseudocode) inside "code" tags so the spaces display properly:
Code:
sum ← 0
For i in range (3) do:
    sum ← sum +max(A)
    remove max(A) from list A
return sum
If anyone thinks this doesn't work, look closer at line 4. However this is still not a good solution for two main reasons:
  1. As well as calculating the sum, the code modifies the input list (by removing the greatest 3 values). This is known as a side effect and is almost always undesireable.
  2. Removing an item from a list can be an expensive process depending on the implementation, and if the list has many items then you don't want to traverse it 3 times to find the maximum. Athough it may still run in time ~kn, it will have a much bigger k than a single list traversal keeping track of 3 maximum values as you go as @FactChecker suggested.
 
  • Informative
  • Like
Likes DaveC426913 and FactChecker
  • #16
Thanks @pbuk and now we will close this thread once more.
 

What is pseudocode?

Pseudocode is a simplified and informal way of writing code that is used to plan out and design a program or algorithm before actually writing it in a specific programming language. It uses a mix of plain English and programming concepts to outline the logic and steps of a program.

Why is it important to learn how to write pseudocode?

Learning how to write pseudocode is important because it helps programmers to think critically and logically about a problem before diving into coding. It also allows for easier collaboration and communication among team members, as pseudocode is easier to understand for non-programmers.

What are the benefits of using pseudocode?

Using pseudocode has several benefits, such as helping to identify potential errors and bugs in the logic of a program before writing actual code, saving time and effort by planning and organizing the program beforehand, and making it easier to translate the pseudocode into actual code in a specific programming language.

How do I write pseudocode?

To write pseudocode, start by identifying the problem or task that needs to be solved. Then, break down the problem into smaller steps and use plain English to describe the logic and actions needed for each step. Use programming concepts such as variables, loops, and conditional statements to outline the program's structure. It's also helpful to use indentation and comments to make the pseudocode more organized and readable.

Can I use pseudocode for any programming language?

Yes, pseudocode is a universal language that can be easily translated into any programming language. However, it's important to keep in mind the syntax and conventions of the specific language you will be using, as they may differ slightly from the pseudocode. It's always a good idea to consult the documentation for the programming language to ensure proper translation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
564
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
45
Views
16K
Back
Top