Sum of Numbers 2-n: Repeat and While Loops

  • Thread starter Thread starter Matt512
  • Start date Start date
AI Thread Summary
The discussion focuses on rewriting an algorithm for summing numbers using repeat until and while loops. Participants express doubts about the correctness of their pseudocode and seek clarification on how control flow works in these loops. Key points include the need for proper initialization of variables and ensuring that user input is correctly handled. Concerns are raised about the conditions used in the loops, particularly regarding safety and potential errors if the conditions are not properly defined. Overall, the conversation emphasizes the importance of structured logic in algorithm design.
Matt512
Messages
17
Reaction score
0

Homework Statement


1.Rewrite this algorithm using a repeat until loop and then with a while loop.[/B]
Start
sum=0
Display "Input value n"
Input n
FOR( i=1 , i <=n , i+1 )
Input a value
sum=sum+value
ENDFOR
Output sum
Stop

I have some doubts if i have done the pseudo codes correctly.
2.Design an algorithm for finding the sum of the numbers 2,4,6,8,...,n
Can someone give me a leading clue on how to do it.
3.Write an algorithm to read 100 numbers then display the largest.
I am stuck midway.

Homework Equations


FOR( starting state , stopping condition, increment)

The Attempt at a Solution



Repeat until loop [/B]
Start
Repeat
sum=0
i=1
Display "Input value n"
Input n
Display "Input a value"
Input a value
sum=sum+value
i=i+1
UNTIL n>=i
Output sum
Stop

while loop.
Start
WHILE(n <>i)
sum=0
i=1
Display "Input value n"
Input n
Display "Input a value"
Input a value
sum=sum+value
i=i+1
ENDWHILE
Output sum
Stop

3.
START
count=0
sum=0
Input number
For( c=1 , c>100 , c+1)
sum=sum+value
ENDFOR
Output sum
Stop
 
Physics news on Phys.org
Please put code (even pseudocode) inside of code tags makes it easier to read :)

Let me ask some questions:
1 (Repeat until): How does your code know where to "jump" back up to??

1 (while): When the code gets to the ENDWHILE and returns to the While <> i what gets reexecuted?
also the <> i is "NOT EQUAL TO" using it here isn't strictly wrong. But while n > i is safer. What would happen if the code accidentally jumped over n=i? 2: There are strong similarities to previous problems. This time you know already what values your going to sum up...

3: You're missing a way for the user to input a number. Also are you summing the values? or maybe you should just track something?
 
1. ( Repeat until ) Since the until condition will still be false , it will go back up and repeats itself.
1. I think it will re execute the sum =0 , i = 1 , so i should put those two at the top and the while <>i after
 
Back
Top