Logo C++ Online Judge

C++

TA:
统计

A program is being written to follow this instruction:

Report the average of 100 temperature readings, ignoring invalid readings.

The function read_sensor() gets one reading from a thermometer. It returns an integer from 0 through 1000 for a valid reading, or a negative integer for an invalid reading. Each call finishes within one millisecond. There is no guarantee about how often the thermometer will produce a valid reading.

Consider implementations A and B below. If either implementation finishes, the program divides sum by count using real division and displays the average. If count is zero, it displays "no usable data" instead. All sums fit in int.

/* A */
int sum = 0, count = 0;
for (int received = 0; received < 100; ++received) {
    int x = read_sensor();
    if (x >= 0) {
        sum += x;
        ++count;
    }
}
/* B */
int sum = 0, count = 0;
while (count < 100) {
    int x = read_sensor();
    if (x >= 0) {
        sum += x;
        ++count;
    }
}

On a test in which the first 100 readings are all valid, A and B display the same average.

The program is later given two explicit requirements:

  • R1: Display the average of 100 valid readings.
  • R2: Make no more than 1000 calls to read_sensor before finishing.

The thermometer behaves as described above; these requirements do not change which readings it may produce.

提交

Written answers receive AI rubric assessment.

Up to 3 submission attempts per student. Sign in to see your remaining attempts.

Each submission includes all questions. Review your answers before submitting.

Short explanations are welcome. You may leave answers blank; unanswered questions receive zero credit.

1. Explain the interpretations of the original instruction implemented by A and B. Do the instruction and the all-valid test select one of them? Include whether each interpretation puts a fixed limit on the number of readings taken. (40 points)
2. Write a short, precise instruction to replace R1 and R2 that you would consider useful for this thermometer. Keep both requirements if you think they can always be met; otherwise explain what you changed and why. Your instruction should make clear what the program reports and when it finishes. (60 points)
Drafts are saved in this browser every 30 seconds.