Logo C++ Online Judge

C++

TA:
Statistics

A query tool reads, filters, and displays records one at a time. Think of its objects as connected sections of a pipe: FileSource takes records from a file, and PositiveSource connects to another section and lets through only records whose value is greater than zero. Their relative order is preserved.

Source is the common interface for these objects. Calling source.next(out) means “please give me the next record”: if one is available, the object puts it in out and returns true; if none remain, it returns false, including on later calls. Because out is a reference parameter, the caller can read the record placed there.

Records do not flow automatically. Each call from report asks the preceding section for a record. If that section is PositiveSource, it asks its own input for records, skipping rejected ones until it finds a positive record or reaches the end. Every next call finishes.

// source.h
struct Record { int value; };
class Source {
public:
    virtual bool next(Record& out) = 0;
    virtual ~Source() = default;
};

// positive.h
class PositiveSource : public Source {
    Source& input_;
public:
    explicit PositiveSource(Source& input) : input_(input) {}
    bool next(Record& out) override {
        while (input_.next(out)) {
            if (out.value > 0) return true;
        }
        return false;
    }
};

// report.cpp
void report(Source& input) {
    Record row;
    while (input.next(row)) print(row);
}

The tool now needs a preview of the positive-value query: display its first k positive records, or all of them if fewer exist. Here k is nonnegative. A developer plans to add LimitSource, which takes another Source and returns at most its first k records. Once it reaches the limit, it stops calling its input’s next.

The developer plans to connect the objects as follows. FileSource takes an input filename; LimitSource takes an input object and a record limit.

FileSource file("records.txt");
LimitSource first(file, k);
PositiveSource positive(first);
report(positive);

A test using a file of entirely positive values gives the required preview.

All Source objects remain alive while they are used. Source& refers to an existing object without copying it; print only displays a record. You do not need to implement file reading or manage allocations.

Submit

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. Does this arrangement meet the preview requirement? Explain briefly and say what, if anything, should change. You do not need to construct a particular file. (40 points)
2. Describe a design for LimitSource that supports this preview. Explain how it works with the existing Source and report, and how it stops reading at the appropriate time. No code is needed. (60 points)
Drafts are saved in this browser every 30 seconds.