Write this algorithm in pseudocode

  • Thread starter Travian
  • Start date
  • Tags
    Algorithm
In summary, the user enters a number and the algorithm finds and displays the largest of the positive numbers entered by the user and the sum of the positive numbers.
  • #1
Travian
64
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
  • #2
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?
 
  • #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?
 
  • #4
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.
 
  • #5
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:
  • #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.
 
  • #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 I am here:/
 
  • #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'.
 
  • #9
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?
 
  • #31
hmmmm not sure at the moment. trying to figure out.
 
  • #32
no idea actually.
 
  • #33
Alright, so we know one of them will be the largest (multiples perhaps but we don't care about those).

We'll call this largest. Can we compare each number they enter to largest(which is the current largest) in the loop?
 
  • #34
iamalexalright said:
Alright, so we know one of them will be the largest (multiples perhaps but we don't care about those).

We'll call this largest. Can we compare each number they enter to largest(which is the current largest) in the loop?

ok, so i declare largestNumber as integer. Then i put in into a loop and compare as you say. But the problem is - i have to assign something to the largestNumber.
 
  • #35
well, first why don't we initialize it to zero.

Now how will you tell if a number entered is bigger than this? If it is, what do you do? If it isn't, what do you do?
 

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
999
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
26
Views
5K
  • Quantum Physics
Replies
13
Views
878
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
33
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top