• :00Days
• :00Hours
• :00Mins
• 00Seconds
A new era for learning is coming soon Suggested languages for you:

Europe

Answers without the blur. Sign up and see all textbooks for free! Q6E

Expert-verified Found in: Page 202 ### Discrete Mathematics and its Applications

Book edition 7th
Author(s) Kenneth H. Rosen
Pages 808 pages
ISBN 9780073383095 # Describe an algorithm that takes an input a list of integers and finds the number of negative integers in the list.

The algorithm that takes as input a list of integers for finding number of negative integers is as follows:

$procedurecountneg\left({a}_{1},{a}_{2},.....,{a}_{n}:integerswithn\ge 1\right)\phantom{\rule{0ex}{0ex}}K:=0\phantom{\rule{0ex}{0ex}}fori=1ton\phantom{\rule{0ex}{0ex}}\phantom{\rule{0ex}{0ex}}if{a}_{i}<1thenk:K+1\phantom{\rule{0ex}{0ex}}returnk$

See the step by step solution

## Step 1: Describing the steps needed to write the required algorithm:

We can call the algorithm as ‘countneg’ and input is a list of n integers

$procedurecountneg\left({a}_{1},{a}_{2},.....,{a}_{n}:integerswithn\ge 1\right)$

We initially define the variable k as 0 ( k will count the negative numbers).

$K:=0$

For every integer between 1 and n , ${a}_{i}$ is negative, then we increase the variable by 1.

$fori=2ton\phantom{\rule{0ex}{0ex}}if{a}_{i}<1thenk:K+1$

Finally, we return the k which counts the negative numbers in the set

return k

## Step 2: Combining all the steps to form an algorithm:

The algorithm is as follows:

$procedurecountneg\left({a}_{1},{a}_{2},.....,{a}_{n}:integerswithn\ge 1\right)\phantom{\rule{0ex}{0ex}}K:=0\phantom{\rule{0ex}{0ex}}fori=1ton\phantom{\rule{0ex}{0ex}}\phantom{\rule{0ex}{0ex}}if{a}_{i}<1thenk:K+1\phantom{\rule{0ex}{0ex}}returnk$

Therefore, the required algorithm is designed for the given conditions. ### Want to see more solutions like these? 