Question: ( 1 0 points ) Write an algorithm in pseudocode that takes as input a number M and a nonempty list a 1 a 2

(10 points) Write an algorithm in pseudocode that takes as input a number M and a nonempty list a1 a2 an of numbers, and adds up all the numbers in the list up to but not including the rst number in the list larger than M. If every element of the list is less than M it should return the sum of the entire list. If the rst element of the list is greater than M then it should return 0. Example 1: if M =8 and the list is 322124, then it should return 7, since 3+2+2=7. We stop adding up the list when we get to the 12 since 12>8. Remember we do not add the 12. Just all the numbers before the 12. Example 2: M =20 and list is 453455711. It should return 12 since 4+5+3=12 and 45>8. Here is a precise de nition. The input is a number M and a nonempty list a1 a2 an. If every element of the list is less than M it should return the sum of the entire list. If the rst element of the list is greater than M then it should return 0. Otherwise suppose ai+1 is the rst element of the the list such that ai+1> M. In this case it should return a1+a2+a3++ai. Example 3: M =4 and the list is 132213. It should return 10 since 1+3+2+2+(1)+3=10 Example 4: M =10 and the list is 12213. It should return 0 since the rst element is larger than 10.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!