# Write this algorithm in pseudocode

1. Aug 26, 2010

### Travian

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. Aug 26, 2010

### Staff: Mentor

What is "index", and why does it vary from 1 to 6?

3. Aug 26, 2010

### Travian

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?

4. Aug 26, 2010

### 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.

5. Aug 26, 2010

### Travian

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
6. Aug 26, 2010

### ╔(σ_σ)╝

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.

7. Aug 26, 2010

### Travian

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:/

8. Aug 26, 2010

### iamalexalright

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'.

9. Aug 26, 2010

### iamalexalright

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)

10. Aug 26, 2010

### Travian

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

11. Aug 26, 2010

### iamalexalright

Why wouldn't you add all three?

12. Aug 26, 2010

### ╔(σ_σ)╝

You declare the array at the beginning of your code; the same place you declare variables.

13. Aug 26, 2010

### Travian

Oh i'm sorry, got confused. Yes i can add all of them. I'll try to modify the code.

14. Aug 26, 2010

### 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.

15. Aug 26, 2010

### Travian

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

16. Aug 26, 2010

### Travian

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.

17. Aug 26, 2010

### 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.

18. Aug 26, 2010

### 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.

19. Aug 26, 2010

### ╔(σ_σ)╝

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.

20. Aug 26, 2010

### ╔(σ_σ)╝

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.