SECTION 10 / 15

Build the prize algorithm from smaller procedures

3 min read
On this page 3 headings

Now connect the pieces. First compute the third distinct mark level in every subject once. Then call a Boolean procedure on each student: does at least one subject reach its cutoff? Only eligible students enter the total-score ranking.

SubjectTopper uses OR and includes equality at the cutoff.
SubjectTopper uses OR and includes equality at the cutoff.
Saved cutoffValue
Maths389
Physics388
Chemistry392
Procedure SubjectTopper(X, Maths3, Physics3, Chemistry3)
    return (X.Mathematics >= Maths3 or
            X.Physics >= Physics3 or
            X.Chemistry >= Chemistry3)
End Procedure

Maths3 = TopThreeMarks(Scores, "Mathematics")
Physics3 = TopThreeMarks(Scores, "Physics")
Chemistry3 = TopThreeMarks(Scores, "Chemistry")
for each row X in Scores {
    if (SubjectTopper(X, Maths3, Physics3, Chemistry3)) {
        Insert X.Total and X.CardNo together into the best 3 slots
    }
}
REAL COURSE DATA · FOLLOW THE PROCEDURE

Check subject eligibility, then rank totals

Read the current record, check the explanation, and watch the variables. Use Back to revisit any step.

Ready

CURRENT RECORD

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Start at the first step. Watch the current row and the values we remember.

A Boolean return is already a condition

SubjectTopper returns True or False. if (SubjectTopper(...)) has the same effect as comparing that result with True. OR means at least one qualifying subject, not all subjects. A mark exactly equal to the cutoff qualifies because the comparison is >=.

Scores and IDs move together

if (X.Total > FirstTotal) {
    ThirdTotal = SecondTotal; ThirdID = SecondID
    SecondTotal = FirstTotal; SecondID = FirstID
    FirstTotal = X.Total; FirstID = X.CardNo
}

Use analogous paired moves for insertion into second or third place. An ID of −1 can mark an empty prize slot because all real CardNo values are nonnegative. You could keep IDs alone and look up marks later, but keeping both avoids repeated lookups.

For these cards, the final paired slots are (281,11), (276,23), (261,18): Rahul, Deepika and Abirami. Our lab processes the same supplied records and shows eligibility separately from the running leaders. Subject cutoffs are saved before the ranking scan rather than recomputed inside every visit.

All three paired update cases

FirstTotal = -1; SecondTotal = -1; ThirdTotal = -1
FirstID = -1; SecondID = -1; ThirdID = -1
for each row X in Scores {
    if (SubjectTopper(X, Maths3, Physics3, Chemistry3)) {
        v = X.Total
        if (v > FirstTotal) {
            ThirdTotal = SecondTotal; ThirdID = SecondID
            SecondTotal = FirstTotal; SecondID = FirstID
            FirstTotal = v; FirstID = X.CardNo
        } else if (FirstTotal > v and v > SecondTotal) {
            ThirdTotal = SecondTotal; ThirdID = SecondID
            SecondTotal = v; SecondID = X.CardNo
        } else if (SecondTotal > v and v > ThirdTotal) {
            ThirdTotal = v; ThirdID = X.CardNo
        } else {
            Record any equality with a retained score for tie review
        }
    }
}
Check the selected IDs, representation rule and any boundary ties
before declaring the final prize winners.

This expands the source’s strict-comparison update template. Equal totals do not create another distinct total level in that template. A general three-student award algorithm must retain tied candidates or apply its stated tie policy; do not assume these strict comparisons alone resolve ties. Our supplied final winning totals are distinct.

Try it yourself

Does a mark equal to a cutoff qualify?

Show explained answer

Yes. Use >=, not >.

Try it yourself

Why is OR appropriate in SubjectTopper?

Show explained answer

The rule asks for a qualifying mark in at least one subject.

Try it yourself

What happens if you move SecondTotal but forget SecondID?

Show explained answer

The score can become attached to the wrong person.

Try it yourself

Does SubjectTopper know that its thresholds are third distinct marks?

Show explained answer

No. It simply compares against the values passed to it. The caller is responsible for computing the intended thresholds.

Try it yourself

Why compute the thresholds before the main loop?

Show explained answer

They stay fixed for every student and avoid repeatedly scanning the dataset for the same information.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑