# Understanding of this pseudo-code

Tags:
1. Jul 13, 2015

### Niaboc67

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")

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

Code (Text):

START
SET I = 0
SET SUM = 0
WHILE I < N
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?

2. Jul 13, 2015

### Staff: Mentor

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?

3. Jul 13, 2015

### Niaboc67

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. Jul 13, 2015

### Niaboc67

@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. Jul 13, 2015

### FactChecker

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.

6. Jul 13, 2015

### Niaboc67

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. Jul 13, 2015

### FactChecker

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.

8. Jul 13, 2015

### Staff: Mentor

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 (Text):

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"

9. Jul 27, 2015

### Jarvis323

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 (Text):

n = get user input
sum = 0
repeat n times
x = get user input
if x is even