networking · beginner · ~15 min
Estimate the recv-loop iteration count.
A client reading at most chunk bytes per recv needs several calls to read a whole response. Compute how many recv calls are required.
Implement int reads_needed(int total, int chunk) that returns the number of recv calls (a ceiling division of total by chunk). Return 0 if chunk <= 0.
total: total bytes to read.chunk: maximum bytes read per call.Returns ceil(total / chunk), or 0 when chunk <= 0.
reads_needed(1000, 256) -> 4
reads_needed(512, 256) -> 2
reads_needed(100, 0) -> 0
chunk <= 0: return 0 (avoid dividing by zero).Two integers: total bytes and chunk size per read.
ceil(total / chunk), or 0 if chunk <= 0.
Use ceiling division; guard chunk <= 0 by returning 0.
int reads_needed(int total, int chunk) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.