Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Part A: Templates

 

Fill in the blanks in each of the following.

 

  All function-template definitions begin with the keyword ______________, followed by a list of

    template parameters to the function template enclosed in    template, angle brackets < >

 

  The ______________________operator is used with a class-template to tie each

    member function definition to the class template’s scope. Scope resolution

 

  Template-definition parameters are used to accomplish one or more of the following

 

   Specify the kinds of arguments to the function

   Specify the return type of the function

   Declare variables in the function.

 

  Class templates are called  ___________________________ types.  Parameterized

 

  Function templates are an alternative to ______________________ functions.  Overloaded

 

  Write a templated function that will accept two integers or two doubles (the values are

    not identical) and return which value is largest.

 

template <class T>

T maximum(T value1, T value2)

{

  T maximumValue = value1;

  if (value2  > maximumValue)

     maximumValue = value 2)

  return maximumValue;

}

 

 

Part B(1): One-Dimensional Arrays

 

Fill in the blanks in each of the following.

 

  The elements of an array are related by the fact that they have the same

     ________________________ and __________________________  name, Type

 

  The number used to refer to a particular element of an array is called its

     ____________________________  subscript or index

 

  A (an) _________________________ should be used to declare the size of an array,

    because it makes the program more scalable.  constant variable

 

  Given a two-dimensional array int a[m][n]. The m refers to ____________________

    and the n refers to ___________________________________________ number rows, number columns

 

  Write code below that will:

 

     Create a one-dimensional array named myArray that contains 500 integers.

 

            int myArray [500];

 

     Populate the above array such that each element is 1 more than its subscript. For

        example, the element with subscript 5 would contain the value 6.

 

            for (int i = 0;  i<500; i++)

                        myArray[i] = i + 1;

 

     Find the sum of the elements in the above array

 

            int sum = 0;

            for (int j = 0; j < 500; j++)

                        sum = sum + myArray(j);

 

     Find the smallest element in the above array.

 

            int smallest = myArray[0];

            for (int k = 1; k<500;k++)

                        if ((myArray[k] < smallest)

                                    smallest = myArray[k];

 

     Print all of the elements in the above array, each on a separate line

 

            for (int i = 0; i<500; i++)

                           cout << myArray[i]<<endl;

 

  Assume that an array named theArray has been declared and populated with 1000 integers.

 

     Write code that will call a function named printLast and pass it an array named theArray. The function

        does not return anything; its purpose is to print the last element in the array.

 

       printLast(theArray, 1000);

 

     Write the function named printLast as described above.

 

      void printLast(int theArray[ ], 1000)

      {

            cout << theArray[99]<<endl;

      }

 

  Given the following code. Write code in the space provided that will populate array b such that the integers

     in ODA are reversed.

 

#include <iostream.h>

int main ()

{

            int ODA [ ];    

            for (int i = 0; i < 1000; i++)

                 ODA[i] = I;

            int b[ ];

            for (int i = 0; i<1000;i++)

                        b[i] = ODA[999-i];

            return 0;

}

 

Part B(2): Two-Dimensional Arrays

 

  Write code that will create a two-dimensional array named twoDArray that consists of 10 rows and

    15 columns of doubles.

 

            Double twoDArray[10][15];

 

  Populate this array with elements that are the sum of the row and column subscript numbers. For example,

    the element in the row whose subscript is 4 and column whose subscript is 5 would be 9.

 

for (int row = 0; row < 10; row++)

             for (int col = 0; col < 15; col++)

             twoDArray[row][col] = row + col;

 

 

Part C: Files

 

  In order to read from or write to a file, you must include a preprocessor directive.

     Write  that preprocessor directive below.

 

#include <fstream.h>  or

 

#include <fstream>

using namespace std;

 

  Assume that the required preprocessor directive has been written and that you are

     writing code in the main. Write code that will open a file named myFile on z drive and

     write the following sentence to the file: This is the second semester of C++.

 

ofstream outFile (“z:\\myFile.txt”)
if (outFile.is_open())              

    outFile<<”This is the second semester of C++.”  

    outFile.close();

    

  Assume that the sentence above has been successfully written to the file named

    myFile on z drive. Write code in the main that will open and read from the file.

 

char buffer[256];                                               
     ifstream inFile (“z:\\myFile.txt”);   

 

     while (! inFile.eof() )                               

     {
         inFile.getline (buffer,100);             
         cout << buffer << endl;                                 
     }
     return 0;
 }

 

 

Part D: Exception Handling

 

Place a check mark (ü) in the appropriate column to indicate whether the statement is true or false.

 

 

Statement

True

False

An exception is an indication of a problem that occurs during a program’s execution.

ü

 

Exception handling enables the programmer to remove error-handling code from the “main line” of the program’s execution.

ü

 

A try block consists of keyword try followed by braces that define what happens if an exception occurs.

 

ü

A catch block consists of keyword catch followed by braces that define a potential error situation.

 

ü

If an exception parameter includes an optional parameter name, the catch handler can use that parameter name to interact with a caught exception object.

ü

 

When a try bock terminates, local variables defined in the block go out of scope.

ü

 

When a try block terminates due to an exception, the program searches for the first catch handler that can process the type of exception that occurred.

ü

 

If no exceptions occur in a try block, the program ignores the catch handler(s)  for that block.

ü

 

To throw an exception, use keyword throw followed by an operand that represents the type of exception to throw.

ü

 

Common examples of exceptions are out-of-range array subscripts, arithmetic overflow, and division by zero.

ü