wpe3B5.jpg (4091 bytes) Question #5(a-c) / Answer


Question 5a

A program which handles text files saves some data on shutodwn and reads it on startup. Two functions, SaveInfo and LoadInfo carry out these tasks, using the file TempInfo.dat. The data is contained in a purpose-designed record ((C++ struct) of type SaveDataType.

Incomplete implementations of the two functions are as follows.

void SaveInfo(SaveDataType SaveData)
{ ofstream OutFile;
  // Missing statement (a)
  if (OutFile)
  {
    OutFile.write(......); // Incomplete statement (b)
    OutFile.write():
  }
}

SaveDataType LoadInfo(void)
{ ofstream InFile;
  SaveDataType Result;
    // Missing statement (c)
    if (InFile)
    {
      // Missing statement (d)
      InFile.close();
    }
    return Result;
}
     
  1. Write down the missing statement in line (a)
  2. Write down the full form of the statement in line (a)
  3. Write down the missing statement in line (c)
  4. Write down the missing statement in line (d)

[5]


Question 5b

Consider the following program fragment of C++ code, which uses functions from the MT262io library.

int InputInteger;
int Current;

  Current=0;
  InputInteger=ReadIntPr("Enter your integer: ");
  while ((Current*Current) <= InputInteger)
    Curent=Current+1;
  WriteStringCr("The answer is: ", Current); // Ouput line.     
  1. What output will be produced if the user enters 5 at the prompt?
  2. What output will be produced if the user enters 0 at the prompt?
  3. What output will be produced if the user enters -1 at the prompt?
  4. Give a more informative statement for the tring "The answer is "

[5]


Question 5c

Consider the following program fragment of C++ code, which uses functions from the MT262io library. The code is intended to search the array of 16 strings in the array are in alphabetical order. (The algorithm that the code attempts to implement is correct).

int Top; // Line 1
int bottom;
int Here;
boolean Found;

  Found=false;
  Top = 15;
  Bottom = 0;
  while Top > Bottom && !Found
  {
    Here = (Top+Bottom)/2);
    if (MyStrings[Here]=WantedString)
      Found=true;
    else
    {
      if (MyStrings[Here] = WantedString)
        Bottom=Here;
      else
        Top=Here;
    }
  }
     
  1. When an attempt was made to compile this code, several  C++ errors were generated. Find and explain how to correct, four such errors. If you wish to refer to lines, they are numbered from 1 as indicated.
  2. After correcting the errors, a C++ warning was still produced. Identity the line which produced the warning.

[5]

first previous key.gif (977 bytes) next last