< previous page page_618 next page >

Page 618
// Reads the second list of numbers
// and compares it to the first list

// Precondition:
//     allOK is assigned
//  && length <= MAX_NUMBER
//  && firstList[0..length-1] are assigned
//  && The two lists are of equal length
// Postcondition:
//     Values from the second list have been read from the
//     input file
//  && IF all values in the two lists match
//          allOK == allOK@entry
//     ELSE
//          allOK == FALSE
//       && The positions and contents of mismatches have been
//          printed

{
    int counter;    // Loop control and index variable
    int number;     // Variable used for reading

    for (counter = 0; counter < length; counter++)
    {
            // Invariant (prior to test):
            //     firstList[0..counter-1] have been compared with
            //     the corresponding input values
            //  && 0 <= counter <= length
            //  && allOK == FALSE, if any mismatches occurred
            //           == allOK@entry, otherwise

        dataFile >> number;
        if (number != firstList[counter])
        {
            allOK = FALSE;
            cout << "Position " << counter    << ": "
                 < setw(4) < firstList[counter] < " != "
                 < setw(4) < number < endl;
         }
    }
}
Testing: The program is run with two sets of data, one in which the two lists are identical and one in which there are errors. The data and the results from each are shown below.

 
< previous page page_618 next page >