Computer Science, asked by mk6075325, 1 month ago

Write an algorithm, using the REPEAT construct, to input a list of numbers terminated by zero and to output the sum of all the numbers input.

Answers

Answered by Anupk3724
1

Answer:

Well, what I’d do is write an algorithm for finding the sum of an arbitrary list of numbers, write another algorithm for finding the first fifty numbers in some list, and then apply the first algorithm to the second.

Of course, because I know Haskell, this is really easy. The Standard Prelude offers both sum, which finds the sum of an arbitrary list of numbers, and take, which finds the first however many items of a list. And so, this function is a one-liner: sum . take 50.

If by “first 50 numbers” you mean the integers 1 to 50, we can just apply this to the (infinite) list of integers, like so: (sum . take 50) [1..]. If you wanted it starting with 0, replace 1 by 0 in that invocation.

Similar questions