Example 1 — Pseudocode translated to C
Average of the positive numbers in a list.
#include <stdio.h>
int main(void) {
int numbers[] = {4, -1, 7, 0, -5, 3};
int n = 6, count = 0, sum = 0;
for (int i = 0; i < n; i++) {
if (numbers[i] > 0) {
count++;
sum += numbers[i];
}
}
printf("positives: %d\n", count);
if (count > 0) printf("average: %.2f\n", (double)sum / count);
return 0;
} How It Works
- The loop mirrors "for each x in numbers".
- The
ifmirrors "if x > 0". - The C version adds types, a cast to avoid integer division, and a zero-count guard.
