Name (Last, First) ____________________________   __________________________

 

Semester:  Fall 2005

 

Lecture Professor Name (circle one)  Prof Gruber   Prof   Robbins   Prof Zelikovitz  

 

Question #

Total Possible

Total Received

1.

30

 

2.

7

 

3.

30

 

4.

5

 

5.

8

 

6.

6

 

7.

15

 

TOTAL

100

 

 


 

For each of the following exam questions, place your answers on the question paper.  There will be no scrap paper given.  If you need scrap, two blank pages are provided at the back of this exam paper.  Your name must appear at the top of each page, including the scrap pages in the back.

NO CALCULATORS WILL BE PERMITTED!!

 

1)      What is printed by the following program segments? (6 points each)

 

 

a)      int x=10, y, z;

y = 2 * x;

  z = 3 * y;

   if ((x > 10 || y < 30) && z < 65)

       cout<< endl << "Hi there, x is: " << x

          << ", and y is  " <<y;

   else

          cout << endl <<"Hi there, y is: " << y

               << ", and z is " <<z;

 

 

 

 

 

 

 

 

b)      bool again = true;

 

  int trials = 0, result = 0;

 

  while (again)

  {

       trials++;

       result += pow(trials,2);

       cout<<"trial: "<<trials<<endl

            <<"result: "<<result<<endl;

       if (result > 10)

            again = false;

  }

 

 

 


 

c)      int numberarr[6];

 

  int i;

 

 

  for (i = 0; i < 6; i++)

  {

       numberarr[i] = i;

       if (i > 3)

            numberarr[i - 1] = 15;

  }

 

  for (i = 0; i < 6; i++)

       cout<<numberarr[i]<<endl;

 

 

 

 

 

 

 

 

 

d)       

Input file contains:

11   -2   4  1   0  3

 

  int value, skip;

 

  ifstream readin;

  readin.open("c:\\data.txt");

 

  while (!readin.eof( ))

  {

       readin>>value;

       readin>>skip;

       cout<<"The current number is \t"<<value<<endl;

  }

 

 

 

 

 

 

 

 

 

e)      void figure(int, int &);

int main()

{

 

  int first = 1;

  int last = 25;

 

  cout<<"first is: "<<first<<endl

       <<"last is: "<<last<<endl;

 

  figure(first,last);

 

  cout<<"first is: "<<first<<endl

       <<"last is: "<<last<<endl;

}

void figure(int a, int & b)

{

  cout<<"IN FUNCTION: "<<a<<' '<<b<<endl;

  a++;

  b = a + b;

  cout<<"IN FUNCTION: "<<a<<' '<<b<<endl;

}

 

 


2)      (7 points) Match the term in COLUMN A with the BEST phrase in COLUMN B.  Write the appropriate letter of the phrase in the blank space to the left of the term

 

COLUMN A                                      COLUMN B

 

____1. GUI

 

____2. debugger

 

____3. RAM

 

____4. object code

 

____5. syntax

 

____6. source code

 

____7. algorithm

 

 

a. The machine language version of the high level language program.

 

b. The set of rules for formulating grammatically correct language statements.

 

c. Part of the development environment that allows users to step through code line by line, and view data values.

 

d. Where your programs and data are temporarily stored while you use the computer. 

 

e. Performs arithmetic and logic functions such as addition , subtraction , comparison .

 

f. A language, which translates each statement individually and executes immediately upon translation

 

g. User friendly interface for software that allows user to enter data into boxes and click on buttons.

 

h. A step by step problem solving process in which the solution is arrived in a finite amount of time.

 

i. Computer instructions written by the user which have not been compiled yet.

 

 


3)       Short Questions: (6 points each)

 

 

a)      Give the C++ statement(s) that will print all even numbers between one and 1,000.

 

 

 

 

b)      Change the following if – else statement to a switch statement.

 

if (x == 3)

          cout<<"Try again";

     else if (x == 0)

          cout<< "Tommorrow";

     else if (x == 1 || x == 2)

          cout<< "Today";

 

 

 

 

 

 

c)      Write the C++ statement(s)   (NOT A PROGRAM) that will print the following limerick exactly as it appears below.  The indented lines are indented one tab position.  Watch the punctuation marks!

 

 

There was a Young Lady of Norway,

Who casually sat on a doorway;

When the door squeezed her flat,

She exclaimed, “What of that?”

This courageous Young Lady of Norway.

 

 

 

 

 

 

 

 

 

d)      Declare an array that holds 100 floating point numbers.  Write the code that will set all elements of the array to be equal to -1.

 

 

e)      Given the following declaration:

 

                        int nums [3] [2];

Draw a diagram of nums showing the number of rows and columns.  Draw the picture of the filled in array after the following code executes.

 

 

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

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

              nums [i] [j] = i  * 10;

 

 

 

 

 

 

 

4)      (5 points)  Given the declarations:

int * pointer1;

  int x, y;

 

  int * pointer2;

 

pointer1 = & x;

  pointer2 = &y;

  x = 15;

 

  y = 64;

 

Assume that the address of x is 00138674, and the address of y is 00138688.

 

What is printed by the following statements?

 

 

a)      cout<< *pointer1;

b)      cout<<x + y;

c)      cout<<*pointer2;

d)      cout<<pointer2<<' '<< &y;

e)      cout<<*pointer1 + *pointer2;

 

5)      Function Coding: (8 points )

 

A value-returning function  checkzeros, accepts three float parameters (sent by value).  It returns true if any of the parameters are equal to zero, and false otherwise.  Write the prototype for this function, and write the function definition (header and body).  Also give an example of how the function would be called from the main program.

 

            prototype:_______________________________________________

 

 

            example call:_______________________________________________

 

 

            function definition (header and body):

 

 

 

 

 

 

 

 

 

 

 

 

6)      (5 points) Given the declarations:

 

char  message[15] = “Good Luck”;

char  punctuation[4];

 

 

a)      What is the value of message[3]?

 

b)      What is the value of strcmp(message,”Good Day”)?

 

c)      After the statement strcpy(punctuation,”!$*”), what is in punctuation[2]?

 

d)      Give the C++ code that changes all occurrences of the letter ‘s’ to the letter ‘t’.

 

7)      (15 points) Let us assume that the New York Lottery has a file on disk that contains information about the first prize winners for the  month of October in the Take Five Drawing.  Each line of the file “Octoberwin.dat”, contains the day of the month, the number of winners, and the amount of money won per person.  A few lines in the file are shown:

 

1          3          30906.00

3          1          89018.50

4          3          31100.50

.

.

The first line means that on October 1st, there were 3 winners who won $30,906.00 each.

The second line means that on October 3rd there was 1 winner who won $89,018.50.

 

Since there were some days with no first prize winners, we do not know how many lines there are in the file.

 

Write a C++ program (put our code on  the next page) that will do the following:

 

i)        Open the file and read in the data.

 

ii)       Call a value returning function that returns the character ‘Y’ if the winners that day won more than $50,000 each, and returns a ‘N’ otherwise.  Make sure that you write the code for this function.

 

iii)     Compute the total number of winners for the  month of October.

 

iv)     Compute the average prize size for the month of October (remember that there are different number of winners for each day).

 

v)  Print the output so that it is organized as below.  (Of course there will be more lines, because this is shown only for the lines above). Note the  dollar amounts are printed to two decimal places.  All printing is done in the main function.

 

 

DATE              WINNERS                  PRIZE AMOUNT       OVER 50 THOUSAND

-----------------------------------------------------------------------------------------------------

1                      3                                  $30906.00                   N

3                      1                                  $89018.50                   Y

4                      2                                  $31100.50                   N

 

Total number of winners:  6

Average Prize:  $40656.25

 

 

Program Code Here:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SCRAP

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

SCRAP