Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Understanding of this pseudo-code

Tags:
  1. Jul 13, 2015 #1
    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 (Text):

    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?
     
  2. jcsd
  3. Jul 13, 2015 #2

    berkeman

    User Avatar

    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? :smile:

    BTW, I'll add code tags to your post to improve the readability...
     
  4. Jul 13, 2015 #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?
     
  5. Jul 13, 2015 #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?
     
  6. Jul 13, 2015 #5

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

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

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

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

    jim mcnamara

    User Avatar

    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"
    EXIT                 End of program, return to the operating system
     
     
  10. Jul 27, 2015 #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 (Text):

    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: Jul 27, 2015
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Understanding of this pseudo-code
  1. Writing pseudo-code (Replies: 2)

Loading...