Practical C Programs
CSV Totals
A practical data program can reduce small table columns into a deterministic total.
row count
The selected row count controls how much of the fixed table participates in the summary.
total
Each row contributes `quantity * price` to the final total.
CSV Totals
csv_totals.c
Replay: real traced execution (multi-file project)
#include <stdio.h>
int main(void) {
int rowCount = 3;
int quantities[3] = {2, 4, 6};
int prices[3] = {5, 3, 2};
int total = 0;
for (int i = 0; i < rowCount; i++) {
total += quantities[i] * prices[i];
}
printf("rows=%d total=%d\n", rowCount, total);
return 0;
}
#include <stdio.h>
int main(void) {
int rowCount = 1;
int quantities[3] = {2, 4, 6};
int prices[3] = {5, 3, 2};
int total = 0;
for (int i = 0; i < rowCount; i++) {
total += quantities[i] * prices[i];
}
printf("rows=%d total=%d\n", rowCount, total);
return 0;
}
#include <stdio.h>
int main(void) {
int rowCount = 2;
int quantities[3] = {2, 4, 6};
int prices[3] = {5, 3, 2};
int total = 0;
for (int i = 0; i < rowCount; i++) {
total += quantities[i] * prices[i];
}
printf("rows=%d total=%d\n", rowCount, total);
return 0;
}
rowCount ← 3, quantities ← ⟨addr A⟩, prices ← ⟨addr B⟩, total ← 0
3int main(void) {4 int rowCount→ 3 = 3; //@rowCount=1, 25 int quantities→ ⟨addr A⟩[3] = {2, 4, 6};6 int prices→ ⟨addr B⟩[3] = {5, 3, 2};7 int total→ 0 = 0;total ← 10
pass 1 of 39for (int i0 = 0; i < rowCount3; i++) {10 total→ 10 += quantities[i]2 * prices[i]5;11}All 3 passes — pass 1 is the card above pass iquantities[i]prices[i]total1 0 2 5 0 → 10 2 1 4 3 10 → 22 3 2 6 2 22 → 34 printf("rows=%d total=%d ", rowCount, total);
13 printf("rows=%d total=%d\n", rowCount3, total34);14 return 0;15}outputrows=3 total=34
rowCount ← 1, quantities ← ⟨addr A⟩, prices ← ⟨addr B⟩, total ← 0
3int main(void) {4 int rowCount→ 1 = 1;5 int quantities→ ⟨addr A⟩[3] = {2, 4, 6};6 int prices→ ⟨addr B⟩[3] = {5, 3, 2};7 int total→ 0 = 0;total ← 10
9for (int i0 = 0; i < rowCount1; i++) {10 total→ 10 += quantities[i]2 * prices[i]5;11}printf("rows=%d total=%d ", rowCount, total);
13 printf("rows=%d total=%d\n", rowCount1, total10);14 return 0;15}outputrows=1 total=10
rowCount ← 2, quantities ← ⟨addr A⟩, prices ← ⟨addr B⟩, total ← 0
3int main(void) {4 int rowCount→ 2 = 2;5 int quantities→ ⟨addr A⟩[3] = {2, 4, 6};6 int prices→ ⟨addr B⟩[3] = {5, 3, 2};7 int total→ 0 = 0;total ← 10
pass 1 of 29for (int i0 = 0; i < rowCount2; i++) {10 total→ 10 += quantities[i]2 * prices[i]5;11}total ← 22
pass 2 of 29for (int i1 = 0; i < rowCount2; i++) {10 total→ 22 += quantities[i]4 * prices[i]3;11}printf("rows=%d total=%d ", rowCount, total);
13 printf("rows=%d total=%d\n", rowCount2, total22);14 return 0;15}outputrows=2 total=22