Interactive Problems

Example Driver (of An Obvious Interactive Problem)

C

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>

int main() {
    FILE *pfin, *pfout;
    pfin = fdopen(3, "r");
    pfout = fdopen(4, "w");

    int n;
    scanf("%d", &n);

    bool ok = false;

    int l = 25;
    while(l--) {
        int x;
        int e = fscanf(pfin, "%d", &x);
        if(e != 1) {
            break;
        }

        if(x < n) {
            fprintf(pfout, "Bigger\n");
            fflush(pfout);
        } else if(x > n) {
            fprintf(pfout, "Smaller\n");
            fflush(pfout);
        } else {
            fprintf(pfout, "Bingo!\n");
            fflush(pfout);
            ok = true;
            break;
        }
    }

    if(ok) {
        printf("0\n");
    } else {
        printf("1\n");
    }

    return 0;
}

The first thing you need to do in a driver is open two special files: pfin and pfout. Pfin is connected to the output of the submitted program. And, pfout is connected to the input of the submitted program. So anything that the submitted program outputs (e.g. using printf) can be read from pfin. And, anything you write to pfout can be read by the submitted program (e.g. using scanf).

At the end, you need to print a single integer from the driver. If the integer is 0, it implies that the submitted program behaved in an expected way (i.e. the test case passed successfully). Otherwise, you can print a non-zero positive number to indicate that the submitted program failed to produce the correct behavior.

The driver program can read the test input through scanf. For this problem, each test input has a single integer.

#Contents of Test Input
154372
2878223

The output part of a test is irrelevant in case of interactive problems. It is also not expected that interactive problems will have sample test cases. Rather, an example interaction should be included as a part of the output specification in the statement.

Search