Sum of Numbers 2-n: Repeat and While Loops

  • Thread starter Thread starter Matt512
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion focuses on rewriting an algorithm to calculate the sum of numbers using both repeat-until and while loops. The provided pseudocode demonstrates the implementation of these loops, with specific attention to initializing variables and handling user input. Key points include the correct placement of variable initialization and the importance of loop conditions to prevent infinite loops. The discussion also addresses common pitfalls in algorithm design, such as ensuring user input is properly handled.

PREREQUISITES
  • Understanding of pseudocode syntax and structure
  • Familiarity with control flow constructs: repeat-until and while loops
  • Basic knowledge of variable initialization and user input handling
  • Experience with algorithm design for summation problems
NEXT STEPS
  • Study the differences between repeat-until and while loops in algorithm design
  • Learn about input validation techniques in pseudocode
  • Explore advanced summation algorithms, such as using recursion
  • Investigate common pitfalls in loop constructs and how to avoid them
USEFUL FOR

Students learning algorithm design, educators teaching programming concepts, and developers looking to improve their understanding of control flow in pseudocode.

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
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 27 ·
Replies
27
Views
4K
  • · Replies 80 ·
3
Replies
80
Views
10K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
3
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
Replies
20
Views
4K
Replies
2
Views
2K