Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

1.(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 first 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 first element of the list is greater than M then it
should return 0.
Example 1: if M =8 and the list is 3,2,2,12,4,, 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 4,5,3,45,57,11. It should return 12 since 4+5+3=12 and
45>8.
Here is a precise definition. 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 first
element of the list is greater than M then it should return 0. Otherwise suppose ai+1 is the first
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 1,3,2,2,1,3. It should return 10 since 1+3+2+2+
(1)+3=10
Example 4: M =10 and the list is 12,2,1,3. It should return 0 since the first element is
larger than 10.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions

Question

Evaluate employees readiness for training. page 275

Answered: 1 week ago