Write this algorithm in pseudocode

  • Thread starter Thread starter Travian
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
The discussion revolves around writing pseudocode for an algorithm that finds the largest positive number and the sum of all positive numbers entered by the user, terminating input with a zero. Participants emphasize that the algorithm should use a single loop to process inputs efficiently, avoiding unnecessary complexity with arrays. The importance of correctly handling both the sum and the largest number within the same loop is highlighted, ensuring clarity in variable usage. There is confusion regarding the input examples, particularly the inclusion of negative numbers, which the original problem statement does not clarify. Ultimately, the consensus is to streamline the code to focus on the required functionality without overcomplicating it.
Travian
Messages
64
Reaction score
0

Homework Statement


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.


Homework Equations



The Attempt at a Solution


Im on it right now and that's 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 I am really stuck here:/
 
Physics news on Phys.org
Travian said:

Homework Statement


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.


Homework Equations



The Attempt at a Solution


Im on it right now and that's 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 I am really stuck here:/

What is "index", and why does it vary from 1 to 6?
 
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?
 
Travian said:
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?

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:
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.
 
berkeman said:
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:
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.




Code:
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:
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.
 
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 I am here:/
 
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'.
 
Travian said:
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 I am here:/

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
iamalexalright said:
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'.

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
Why wouldn't you add all three?
 
  • #12
You declare the array at the beginning of your code; the same place you declare variables.
 
  • #13
Oh I'm sorry, got confused. Yes i can add all of them. I'll try to modify the code.
 
  • #14
╔(σ_σ)╝ said:
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.
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.
╔(σ_σ)╝ said:
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.
No array is needed. All the OP needs is a single, scalar (non-array) variable to store the current input in.
╔(σ_σ)╝ said:
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.
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.

Travian said:
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 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
Code:
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
Mark44 said:
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.

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
╔(σ_σ)╝ said:
You declare the array at the beginning of your code; the same place you declare variables.
And how big would this array be? The number of input values is not known until the program is finished processing the inputs.
 
  • #18
Travian said:
Code:
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

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
Mark44 said:
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.
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
Mark44 said:
And how big would this array be? The number of input values is not known until the program is finished processing the inputs.
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.
 
  • #21
Mark44 said:
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.

Code:
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.
 
  • #22
╔(σ_σ)╝ said:
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.

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.
 
  • #23
iamalexalright said:
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.

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
 
  • #24
╔(σ_σ)╝ said:
I missed the termination by 0 part, oopps sorry!

I suggested arrays based on the code that was provided.
You should instead focus on the problem that was stated, rather than on the flawed implementation that the OP gave.
╔(σ_σ)╝ said:
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.
Inputting the numbers twice is not an option. Any pseudocode algorithm has to work with the numbers as they come in.
╔(σ_σ)╝ said:
Op was cyling through an imaginary array initially. This is why i suggested arrays.
 
  • #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
 
  • #26
Again I would emphasize that given the setup that OP gave called for arrays. I even thought that was the direction OP was going to. In this original post he was using a loop to cycle through an array he didn't create. Besides, i thought it would be better if i went along with OP thought process instead of giving him a totally different approach.

Mark44 it was not a flawed interpretation . I belief he had the right idea but the execution was not satisfactory.
 
  • #27
Code:
Declare Counter as integer
Declare Number as float
Set Counter = 0
Set Number = -1
Display "Enter a number. Enter a zero to finish"
WHILE Number != 0
    Get Number
    IF Number > 0 THEN
        Counter = Counter + Number
    ENDIF
ENDWHILE
Display "The sum of positive numbers is: " + Counter

Good idea! But then again, i think i should store the numbers. Right?
 
Last edited:
  • #28
Well, tell me why you think you should store the numbers.
 
  • #29
Well, i will have to display the largest number.
 
  • #30
Well, it can be done in the while loop! Knowing this, can you devise a way to do it?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
8K
  • · Replies 26 ·
Replies
26
Views
6K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
7
Views
2K
Replies
7
Views
3K
Replies
6
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
33
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K