Understanding of this pseudo-code

  • Thread starter Thread starter Niaboc67
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around understanding a piece of pseudo-code designed to compute the sum of even numbers from user-provided values. Participants explore the structure and semantics of the pseudo-code, discussing its components and the logic behind it.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • Some participants seek clarification on the meaning of each line in the provided pseudo-code, questioning the purpose of constructs like "START" and "READ".
  • Others argue that pseudo-code is primarily for human understanding rather than machine execution, emphasizing its role in planning before translating to formal programming languages.
  • A participant suggests that there are no universally standard rules for writing pseudo-code, though common control flow structures like "WHILE" and "IF" should be recognized.
  • Another participant points out that the original pseudo-code incorrectly checks if N is even instead of checking if X is even.
  • Some participants express uncertainty about how to formulate pseudo-code from problem statements, indicating a lack of familiarity with programming concepts.

Areas of Agreement / Disagreement

There is no consensus on the correctness of the original pseudo-code, as some participants identify errors while others defend its structure. The understanding of pseudo-code itself is also debated, with varying opinions on its purpose and format.

Contextual Notes

Participants mention that pseudo-code can vary significantly in style and may not adhere to strict rules, reflecting individual or educational preferences. The discussion highlights the importance of understanding control flow and logical constructs in programming.

Who May Find This Useful

This discussion may be useful for beginners in programming who are trying to grasp the concept of pseudo-code and its application in developing algorithms before coding in a specific programming language.

Niaboc67
Messages
249
Reaction score
3
I am trying to understand pseudo-code and an assignment that was given and which I have the answer to.

Question:
Write pseudo code for a program that computes the sum of the even numbers from use-provided values. The program will first read the number of values (N) that the user will input. It will then process the N values from user input, one by one. Finally, it will print the sum.

To keep things simple, you can read the next integer and store it in some variable x as follows: x=read

you can print a value x or message as follows: print(x) or print("hello")

-------------------------------------------------------------------------------------------------------------------------------------------------

The answer:
Code:
START
N = READ
SET I = 0
SET SUM = 0
WHILE I < N
    X = READ
    IF N%2 IS 0 : SUM = SUM + X
    I = I+1
ENDWHILE
PRINT(SUM)
EXIT
-------------------------------------------------------------------------------------------------------------------------------------------------

I do not understand this. If someone could please explain step-by-step the meaning here it would be greatly appreciated. What does each line represent? why is it there?
 
Technology news on Phys.org
Niaboc67 said:
I am trying to understand pseudo-code and an assignment that was given and which I have the answer to.

Question:
Write pseudo code for a program that computes the sum of the even numbers from use-provided values. The program will first read the number of values (N) that the user will input. It will then process the N values from user input, one by one. Finally, it will print the sum.

To keep things simple, you can read the next integer and store it in some variable x as follows: x=read

you can print a value x or message as follows: print(x) or print("hello")

-------------------------------------------------------------------------------------------------------------------------------------------------

The answer:

START
N = READ
SET I = 0
SET SUM = 0
WHILE I < N
X = READ
IF N%2 IS 0 : SUM = SUM + X
I = I+1
ENDWHILE
PRINT(SUM)
EXIT

-------------------------------------------------------------------------------------------------------------------------------------------------

I do not understand this. If someone could please explain step-by-step the meaning here it would be greatly appreciated. What does each line represent? why is it there?

It would be easier if you asked a more specific question. Which part do you have trouble seeing? "READ" seems to imply reading the variable from the keyboard and storing it in the variable shown. The % symbol is used for modulo math... What else? :smile:

BTW, I'll add code tags to your post to improve the readability...
 
Thanks for the reply. I guess i'll start with how you define what pseudo-code actually is? and then with "START" why must it begin with start and what is this communicating to the computer?
 
@FactChecker I did some simple HTML/CSS before doesn't the computer have to prompt the user first? Or maybe I am missing the idea of what pseudo-code is. Is it just straight forward logic?
 
Oh, I see the problem. You are expecting more from pseudo-code than you should. It is not so much for the computer to understand. It is something for the human to understand what the process will be. Then a programmer will translate the pseudo-code to a formal language that the computer can accept.

PS. Sorry about the harsh tone of my earlier post. I have deleted it. I see that your original question is a natural one.
 
  • Like
Likes   Reactions: berkeman
I see are there certain rules for this? given the question how do I go about formulating the general structure from it. I am pretty new to programming so reading the question and then implementing it to code does not come naturally, yet.
 
Your class may have specified rules for your pseudo-code, but I don't think there are any universally standard rules. Your example shows some of the basics. It contains a "WHILE, ENDWHILE" loop and an "IF" statement. You should get familiar with those types of control flow that are common in programming and indicate them in the pseudo-code. Other types are "DO, UNTIL", "IF, ELSE IF, ELSE, ENDIF", "CALL", "RETURN".

Some good guidance can be found in http://www.unf.edu/~broggio/cop3530/3530pseu.htm but you should be ready to adapt those guidelines to the standards of your class.
 
  • Like
Likes   Reactions: berkeman
Pseudo-code is meant to help you write language-specific code - like FORTRAN or C or BASIC
What you do is mentally translate each statement into the code you want to use. Since it is generic there is no one-to-one translation some languages may not have an exact match for START, for example. Sometimes a couple of pseud-code lines can be written as one or two keywords in a language.

SO,

Code:
START          begin the program
N = READ     read a value from the keyboard - "remember it" in  N - a variable name for a place in memory to hold an integer
SET I = 0         Place zero in the memory place named "I"
SET SUM = 0   Place zero in the memory place named  "SUM"
WHILE I < N     Compare the two variables I and N and if N is larger than I execute everything between WHILE and ENDWHILE - this is called a loop
    X = READ    read an integer value and store it in "X"
    IF N%2 IS 0 : SUM = SUM + X    Divide N by two, and if the remainder is zero, then add the value of "X" to "SUM" and store it in "SUM"
    I = I+1          Add one to "I" - take "I" and ad one, then store it back in "I".  This is called an increment.
ENDWHILE     This marks then end of the while loop construct
PRINT(SUM)    Display the value stored in the variable "SUM"
EXIT                 End of program, return to the operating system
 
  • Like
Likes   Reactions: FactChecker
Pseudo-code is informal fake code. It can actually look much more like English than a programming language if desired and that's fine. The particular pseudo-code you posted is not typically how pseudo-code looks, and the style it's written in, and the choices of keywords are just arbitrary.

The following would be perfectly valid pseudo code as well,

Code:
n = get user input
sum = 0
repeat n times
    x = get user input
    if x is even
       add x to sum
print sum

Also, note that the pseudo-code you posted is incorrect, as it should be checking if X is even, not N, in the loop.
 
Last edited:
  • Like
Likes   Reactions: FactChecker

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 29 ·
Replies
29
Views
4K
  • · Replies 28 ·
Replies
28
Views
5K
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
2
Views
1K