Write this algorithm in pseudocode

  • Thread starter Thread starter Travian
  • Start date Start date
  • Tags Tags
    Algorithm
Click For Summary
SUMMARY

The forum discussion revolves around writing a pseudocode algorithm to find the largest positive number and the sum of positive numbers entered by the user, terminating input with a zero. Key points include the need for a single loop to process inputs efficiently, without requiring an array for storage. The final pseudocode should include a mechanism to check for positive numbers and update the largest number and sum accordingly, while avoiding unnecessary complexity.

PREREQUISITES
  • Understanding of basic programming constructs such as loops and conditionals.
  • Familiarity with pseudocode syntax and conventions.
  • Knowledge of variable declaration and data types (e.g., float, integer).
  • Ability to implement algorithms that process user input dynamically.
NEXT STEPS
  • Research how to implement a single loop for input processing in pseudocode.
  • Learn about conditional statements and their usage in pseudocode.
  • Explore examples of algorithms that aggregate data from user input.
  • Study best practices for writing clear and efficient pseudocode.
USEFUL FOR

This discussion is beneficial for students learning algorithm design, educators teaching programming concepts, and anyone interested in improving their pseudocode writing skills.

  • #31
hmmmm not sure at the moment. trying to figure out.
 
Physics news on Phys.org
  • #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?
 
  • #36
I will use a boolean expression.

if largestNumber < Number then
largestNumber = Number + largestNumber
else
hmmm:DDD
 
  • #37
╔(σ_σ)╝ said:
Again I would emphasize that given the setup that OP gave called for arrays.
If by "setup" you mean the problem statement, you are confusing the requirements of the problem statements with an implementation. This problem can and should be done without arrays. That's the point that I and several other responders have been making.
╔(σ_σ)╝ said:
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.
Then you and I disagree. In my view, Travian was heading down the wrong track. When that happens, the best thing is not to help him go in the wrong direction, but to steer him in the right direction.
╔(σ_σ)╝ said:
Mark44 it was not a flawed interpretation . I belief he had the right idea but the execution was not satisfactory.
 
  • #38
Travian said:
I will use a boolean expression.

if largestNumber < Number then
largestNumber = Number + largestNumber
else
hmmm:DDD

Try it with actual numbers.
1) Let largestNumber = 25 and Number = 28
What should happen?

2) Let largestNumber = 25 and Number = 23
What should happen?
 
  • #39
Mark44 said:
Try it with actual numbers.
1) Let largestNumber = 25 and Number = 28
What should happen?

2) Let largestNumber = 25 and Number = 23
What should happen?


1) largestNumber becomes 28
2) largestNumber cannot be 25, because it became 28. i should have used while loop i think, not if statement.
 
  • #40
Code:
Declare Counter as integer
Declare Number as float
Delare largestNumber as integer
Set Counter = 0
Set Number = -1
Set largestNumber = 0
Display "Enter a number. Enter a zero to finish"
WHILE Number != 0
    Get Number
    IF Number > 0 THEN
        Counter = Counter + Number
         IF largestNumber < Number THEN
             largestNumber = largestNumber + Number    
         ENDIF
    ENDIF
ENDWHILE
Display "The sum of positive numbers is: " + Counter  
Display “The largest number is “ + largestNumber
I can't think of anything else..
 
  • #41
I see one problem...

If largetsNumber < Number why do you then add number to largetsNumber and not just set largestNumber to number?
 
  • #42
Then largestNumber == Number...
instead of largestNumber + Number
?
 
  • #43
Mark44 said:
Try it with actual numbers.
1) Let largestNumber = 25 and Number = 28
What should happen?

2) Let largestNumber = 25 and Number = 23
What should happen?

Travian said:
1) largestNumber becomes 28
2) largestNumber cannot be 25, because it became 28. i should have used while loop i think, not if statement.
No, IF statement is what you want. The examples above weren't necessarily in sequence. The idea was that in the first example, largestNumber should be reset. In the second example, largestNumber shouldn't be reset.

Code:
IF Number > largestNumber THEN
  largestNumber = Number
ENDIF

What's implied in code above is that if Number <= largestNumber, then don't do anything.
 
  • #44
Code:
Declare Counter as integer
Declare Number as float
Delare largestNumber as integer
Set Counter = 0
Set Number = -1
Set largestNumber = 0
Display "Enter a number. Enter a zero to finish"
WHILE Number != 0
    Get Number
    IF Number > 0 THEN
        Counter = Counter + Number
        [b]IF (Number > largestNumber) THEN
            largestNumber = Number
        ENDIF[/b]    
    ENDIF
ENDWHILE
Display "The sum of positive numbers is: " + Counter  
Display “The largest number is “ + largestNumber
Ok i did all that. One last question. Is it correct to put if statement in another if statement, like i did (text emphasized in bold)
 
  • #46
Thank you guys, i appreciate your help.
I will recommend this forum to other people.
 

Similar threads

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