1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Write this algorithm in pseudocode

  1. Aug 26, 2010 #1
    1. The problem statement, all variables and given/known data
    I have to write this algorithm in pseudocode that finds and displays the largest of a list of positive numbers entered by the user and the sum of the positive numbers. The user should indicate that he has finished entering numbers by entering a zero.

    For example the user might enter

    3 4 -9 8 -2 0

    the algorithm should report that the largest of the positive numbers is 8 and that the sum of positive numbers is 15.


    2. Relevant equations

    3. The attempt at a solution
    Im on it right now and thats what i have done so far:

    Declare Sum as float
    Declare Counter as integer
    Declare Number as float
    Declare Average as float
    Declare highestNumber as integer
    highestNumber = score(0)
    FOR index 1 to 6
    IF number(index) > highestNumber THEN
    highestNumber=number(index)
    ENDIF
    ENDFOR
    Set Sum = 0
    Set Count = 0
    Display "Enter a number. Enter 0 when done"
    Get Number
    WHILE Number > 0 THEN
    Set Count = Count 1
    Set Sum = Sum Number
    ENDWHILE
    IF Count != 0
    Set Average = Sum/Counter
    ENDIF

    Can someone help me to finish it, because im really stuck here:/
     
  2. jcsd
  3. Aug 26, 2010 #2

    berkeman

    User Avatar

    Staff: Mentor

    What is "index", and why does it vary from 1 to 6?
     
  4. Aug 26, 2010 #3
    Im not sure myself, maybe it should be Number instead of index?
    It varies from 1 to 6 because i don't know how to make infinite number input.

    Any ideas how to do that?
     
  5. Aug 26, 2010 #4

    berkeman

    User Avatar

    Staff: Mentor

    You will generally only go through a list once, not in multiple passes to do different things. So you will go through that list once, stopping when you see a zero. While you are going through the list, you do both the sum of the positive numbers, and remembering the largest number you've seen so far. Organize it something like:

    Code (Text):

    get first number

    while number != 0{

         <do stuff>

         get next number }
    PS -- use the "code" tags to get indentation in forum posts to make the code more readable. Click the Quote button on this post to see how I used code tags.
     
  6. Aug 26, 2010 #5



    Code (Text):

    Declare Sum as float
    Declare Counter as integer
    Declare Number as float
    Declare Average as float
    Declare highestNumber as integer
    highestNumber = score(0)
    Display "Enter a number. Enter a zero to finish"
    Get Number
    [b]WHILE Number != 0
        Get Number
    ENDWHILE[/b]
    IF number > highestNumber THEN
    highestNumber=number
    ENDIF
    Set Sum = 0
    Set Count = 0
    WHILE Number > 0 THEN
    Set Count = Count + 1
    Set Sum = Sum Number
    ENDWHILE
    IF Count != 0
    Set Average = Sum/Counter
    ENDIF
    Ok i modified that, but i suppose i have to store them somehow, so i could add them.
     
    Last edited: Aug 26, 2010
  7. Aug 26, 2010 #6
    Index should not be hard coded. You should keep scanning for integers until you see a end of character sysmbol like '0/' or something.

    Alternatively, you can ask the user how many number he or she will enter
    and then use that variable in your loop.

    As for the adding all the positive numbers your program will most like not do that. Where does get Get Number get the numbers from ? You realize that if you didn't save the numbers in some sort of array your data is no where to be found.

    This is also the problem wih your code to find the largest integer. You actually have no storage of the users input. You need some sort of array to store the numbers and then you can acess them.
     
  8. Aug 26, 2010 #7
    Yeah i figured that i need arrays, the problem is - i don't know how to correctly (and where to) put them. That's why im here:/
     
  9. Aug 26, 2010 #8
    Well here's my input...

    If you only need to find the largest and the sum of positive numbers you can do that all in the while loop (with only three variables). You can add them by checking if they are positive and them just adding them to some 'master' variable 'sum'.
     
  10. Aug 26, 2010 #9
    Why do you need arrays? Does the problem description explicitly state that you need them to recall the numbers entered? If you use arrays this problem gets tougher(would have to dynamically change the size of the array as needed)
     
  11. Aug 26, 2010 #10
    That would be ok if the user would input only 2 positive numbers. But what if he inputs 3 positive numbers? I need to add only two of them
     
  12. Aug 26, 2010 #11
    Why wouldn't you add all three?
     
  13. Aug 26, 2010 #12
    You declare the array at the beginning of your code; the same place you declare variables.
     
  14. Aug 26, 2010 #13
    Oh i'm sorry, got confused. Yes i can add all of them. I'll try to modify the code.
     
  15. Aug 26, 2010 #14

    Mark44

    Staff: Mentor

    No, that's completely out of the question. The algorithm has to work with the numbers that will be coming in as described in the OP, which is a sequence of numbers terminated by 0.
    No array is needed. All the OP needs is a single, scalar (non-array) variable to store the current input in.
    Again, no. To use an array you need to know upfront how many cells to allocate, and that information is not given in this problem.

    The problem statement seems to say that the input stream consists of positive numbers, yet the example has negative numbers in it. Have you given us the problem statement exactly as given to you?

    Also, the problem asks for pseudocode, but you are showing what looks like a complete Basic program. You don't need to declare variables in pseudocode. What berkeman suggested is more like what is asked for in this problem.
     
  16. Aug 26, 2010 #15
    Code (Text):

    Declare Sum as float
    Declare Counter as integer
    Declare Number as float
    Declare Average as float
    Declare highestNumber as integer
    Set Counter = 0
    highestNumber = score(0)
    Display "Enter a number. Enter a zero to finish"
    Get Number
    WHILE Number != 0
        Get Number
        [b]IF Number > 0 THEN
            Counter = Counter + Number
        ENDIF[/b]
    ENDWHILE
    IF number > highestNumber THEN
    highestNumber=number
    ENDIF
    WHILE Number > 0 THEN
    Set Count = Count + 1
    Set Sum = Sum Number
    ENDWHILE
    IF Count != 0
    Set Average = Sum/Counter
    ENDIF
     
     
  17. Aug 26, 2010 #16
    Yes, this is the condition. I have to sum up ONLY the positive numbers.
    About the declaring... my tutor does that, so i think i should follow him. Thats all.
     
  18. Aug 26, 2010 #17

    Mark44

    Staff: Mentor

    And how big would this array be? The number of input values is not known until the program is finished processing the inputs.
     
  19. Aug 26, 2010 #18

    Mark44

    Staff: Mentor

    This isn't what you want. For one thing, there should be only a single loop. Also, your 2nd IF structure needs to be inside the loop.

    You have two variables with similar names that seem to represent the same thing: Count and Counter. I see at least one other syntax error.
     
  20. Aug 26, 2010 #19
    I missed the termination by 0 part, oopps sorry!

    I suggested arrays based on the code that was provided. He needs arrays for his current setup. To avoid using them he has to find the largest number and the sum of he positive intergers at the same time. If he wanted to do them separately he either needs to input the numbers twice or use an array.


    Op was cyling through an imaginary array initially. This is why i suggested arrays.
     
  21. Aug 26, 2010 #20
    Dynamic allocation. This is why i had originally suggested to ask about the amount of numbers been put in. You guys should stop hammering me for suggesting arrays. Please refer to my post above.
     
  22. Aug 26, 2010 #21
    Code (Text):

    Declare Counter as integer
    Declare Number as float
    Set Counter = 0
    Display "Enter a number. Enter a zero to finish"
    Get Number
    WHILE Number != 0
        Get Number
        IF Number > 0 THEN
            Counter = Counter + Number
        ENDIF
    ENDWHILE
    Display "The sum of positive numbers is: " + Counter
     
    I deleted all that crap in the bottom, looks better now.
     
  23. Aug 26, 2010 #22
    Yes, you can use dynamic allocation but this is 'somewhat advanced' and for something like this completely over the top and unnecessary.

    Travian:

    If you notice before your while loop you 'get number'. Then immediatly entering the while loop you 'get number' again ! Doing this means you have 'lost' the first number entered.
     
  24. Aug 26, 2010 #23
    Then i should change the second get number? Because the first one is necessary and the second allows the user to input as many numbers as he wants
     
  25. Aug 26, 2010 #24

    Mark44

    Staff: Mentor

    You should instead focus on the problem that was stated, rather than on the flawed implementation that the OP gave.
    Inputting the numbers twice is not an option. Any pseudocode algorithm has to work with the numbers as they come in.
     
  26. Aug 26, 2010 #25
    Why is the first one necessary?

    You can fix this two ways:

    1. set number to -1 at the start of the program (-1 != 0 so you can enter the loop). Then the user will enter whatever integer they feel like and your program can run.

    2. use a do...while loop
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook