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.
  • #36
I will use a boolean expression.

if largestNumber < Number then
largestNumber = Number + largestNumber
else
hmmm:DDD
 
Physics news on Phys.org
  • #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 reccomend this forum to other people.
 

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • 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
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • 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