Algorithms
Count Matches
A counting loop increments a result when an element satisfies a condition.
Count Matches
count_matches.c
#include <stdio.h>
int main(void) {
int threshold = ;
int values[5] = {2, 5, 7, 9, 4};
int count = 0;
for (int i = 0; i < 5; i++) {
if (values[i] >= threshold) {
count++;
}
}
printf("count=%d\n", count);
return 0;
}
#include <stdio.h>
int main(void) {
int threshold = ;
int values[5] = {2, 5, 7, 9, 4};
int count = 0;
for (int i = 0; i < 5; i++) {
if (values[i] >= threshold) {
count++;
}
}
printf("count=%d\n", count);
return 0;
}
#include <stdio.h>
int main(void) {
int threshold = ;
int values[5] = {2, 5, 7, 9, 4};
int count = 0;
for (int i = 0; i < 5; i++) {
if (values[i] >= threshold) {
count++;
}
}
printf("count=%d\n", count);
return 0;
}
predicate
The condition decides whether one element contributes to the count.
counter
The counter changes only when the condition is true.