Understanding of this pseudo-code

  • Thread starter Niaboc67
  • Start date
In summary: SUM = 0 Initialize the sum to 0WHILE I < N x = READ if the number on the keyboard is divisible by two, then add x to the sumIF N%2 IS 0
  • #1
Niaboc67
249
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
  • #2
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...
 
  • #3
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?
 
  • #4
@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?
 
  • #5
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 berkeman
  • #6
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.
 
  • #7
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 berkeman
  • #8
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 FactChecker
  • #9
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 FactChecker

1. What is pseudo-code?

Pseudo-code is a simplified and informal representation of a computer program or algorithm, using a mixture of English language and coding conventions. It is used as a tool for planning and designing a program before it is written in a specific programming language.

2. Why is pseudo-code important?

Pseudo-code allows programmers to focus on the logic of a program without getting bogged down in the syntax of a particular programming language. It also helps in identifying potential errors and improving the efficiency of a program before it is actually coded.

3. How do I write pseudo-code?

There is no set rule for writing pseudo-code, but it should be clear, concise and easy to understand. It should also follow the general structure of a program, including variables, loops, and conditional statements. It is also important to use standard coding conventions to make it easier to translate into actual code.

4. Can pseudo-code be translated into any programming language?

Yes, pseudo-code can be translated into any programming language. However, it may require some modifications and adjustments to fit the specific syntax and rules of the chosen programming language.

5. Is it necessary to use pseudo-code before writing a program?

Using pseudo-code is not a requirement, but it is highly recommended. It can save time and effort in the long run by helping to catch errors and improve the overall design of a program before writing actual code. It also serves as a useful reference for future modifications and updates to the program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
22
Views
786
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
9
Views
729
Replies
5
Views
891
Replies
2
Views
771
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
759
Back
Top