1 of 12

Computational Thinking - Counting

By

Dept. of CSE

PVPSIT, Kanuru.

PRASAD V. POTLURI SIDDHARTHA INSTITUTE OF TECHNOLOGY

2 of 12

  • Problem
  • Given a set of n students’ examination marks (in the range 0 to 100) make a count of the number of students that passed the examination. A pass is awarded for all marks of 50 and above.
  • Algorithm development
  • Counting mechanisms are very frequently used in computer algorithms.
  • Generally a count must be made of the number of items in a set which possess some particular property or which satisfy some particular condition or conditions.

Dept. of CSE

2026

Algorithm 2.2

COUNTING

PVPSIT (Autonomous)

Problem Solving Techniques

3 of 12

  • Suppose that we are given the set of marks

55, 42, 77, 63, 29, 57, 89

  • To make a count of the passes for this set we can start at the left, examine the first mark (i.e. 55), see if it is >=50, and remember that one student has passed so far.
  • The second mark is then examined and no adjustment is made to the count.
  • The process continues in a similar manner until all marks have been tested.

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques

4 of 12

  • In more detail we have:

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques

5 of 12

  • The two steps in this process can be represented by

  • Obtain the similar expression

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques

6 of 12

  • Viewing the mechanism in this way makes it clear that the existence of the variable previous_count in its own right is unnecessary.
  • The essential steps in our pass-counting algorithm can therefore be summarized as:

While less than n marks have been examined do

(a) read next mark,

(b) if current mark satisfies pass requirement then add one to count.

  • Before any marks have been examined the count must have the value zero.

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques

7 of 12

  • Algorithm description

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques

8 of 12

  • Pseudo-code / logic
  • begin

read n

set passmark as 50

count := 0

i := 0

while i < n do

begin

i:= i+1

read m

if m >= passmark then count := count+1

end

writeout “no. of passes=”, count

  • end

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques

9 of 12

  • Notes on design:
  • Initially, and each time through the loop, the variable count represents the number of passes so far encountered.
  • On termination (i=n) count represents the total number of passes in the set.
  • Applications
  • All forms of counting.

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques

10 of 12

  • Supplementary Problems
  • 2.2.2 Design an algorithm that reads a list of numbers and makes a count of the number of negatives and the number of non-negative members in the set.

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques

11 of 12

2.2.2 Design an algorithm that reads a list of numbers and makes a count of the number of negatives and the number of non-negative members in the set.

  • Algorithm description
  • Prompt and read the number of numbers to be processed.
  • Initialize negative and non-negative counts to zero.
  • While there are still numbers to be processed repeatedly do

(a) read next number.

(b) if it is < 0 then add one to negative count

else add one to non-negative count.

4. Write out negative and non-negative counts.

Dept. of CSE

2026

Solution

PVPSIT (Autonomous)

Problem Solving Techniques

12 of 12

  • Pseudo-code:
  • begin

read (n)

negative_count := 0

non-negative_count := 0

total_count := 0

while total_count < n do

begin

read (number)

total_count := total_count+1

if number < 0 then

negative_count := negative_count+1

else

non-negative_count := non-negative_count+1

end

write out negative_count and non-negative_count

    • end

Dept. of CSE

2026

PVPSIT (Autonomous)

Problem Solving Techniques