StudySmarter AI is coming soon!

- :00Days
- :00Hours
- :00Mins
- 00Seconds

A new era for learning is coming soonSign up for free

Suggested languages for you:

Americas

Europe

Q6E

Expert-verifiedFound in: Page 202

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({a}_{1},{a}_{2},.....,{a}_{n}:integerswithn\ge 1)\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$**

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

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

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

The algorithm is as follows:

$procedurecountneg({a}_{1},{a}_{2},.....,{a}_{n}:integerswithn\ge 1)\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.

94% of StudySmarter users get better grades.

Sign up for free