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.(4 points) 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!

 

 

Mr. Franklin was flying his kite
In the middle of a dark stormy night.

"It's for science," he said

And he went on ahead.
Now he's generally thought of as bright!!!

 

 

 

 

 

 

 

 

 

 

 

 

2. (4 points) What value is assigned to the integer variable y ?

 

 

a)    y = 3/5 * 3 + 4;

 

 

 

 

 

b)    y = 53 % (4 * 3) + 1

 

 


3. (15 points) What is printed by each of the following program segments?  Place your answers in the boxes at the side.

 

a)

 
a)   int i,j;

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

{

     cout << "*****";

     cout<<endl;

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

     {

         cout << "*   *" << endl; 

                   //3 spaces between

     }

}

cout << "*****"<<endl;

 

 

 

 

 

b)  int A[2][3], i, j;

b)

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

     {

          for(j=0; j<3; j++)

          {

              A[i][j] = i * j + 2;

              cout << A[i][j] <<"  ";

          }

          cout << endl;

     }

 

c)         int A[8]={1,2,4,6,7,7,2,1};

c)

 
     int i = 2;

     while (i <= 5)

     {

          A[i] = A[i] - i; 

          i++;

     }

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

     {

          cout << A[i] <<"   ";

     }


d)

 
d)         int k = 6;

     do{

          cout<<"Goodbye" << endl;

     }while(k < 5);

     cout << "Hello" << endl;

 

 

 

e )        int m = 5;

e )

 
     while (m >= 1)

     {

          cout << "m = " << m << endl;

          m = m - 2;

     }   

 

 

4. (8 points) Matching column.  Write the appropriate letter in the blank space to the left of the term.

 

____1. RAM

 

____2. syntax

 

____3. secondary storage

 

____4. low level language

 

____5. high level language

 

____6. compiled language

 

____7. source code

 

____8. programming

a. Directs and controls the overall operation of the computer.

 

b. Examples are machine and assembly languages.  This type of language is dependent on the computer that is using it.

 

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

 

d. The process of writing a piece of software.

 

e. Instructions which resemble written language and can be run on a variety of computer types.

 

f. Volatile memory where your programs and data are stored while you use the computer.

 

g. Where all the arithmetic and logical functions, such as addition, subtraction, comparison, etc. are performed.

 

h. A permanent storage area for programs and data.

 

i. Contains the fundamental instructions for a computer which cannot be lost or changed by the casual computer user

 

j. A language where the source code is translated as a whole before any one statement is executed.

 

k. A program written in a computer

5. (4 points) What is printed by the following C++ program?

 

#include <iostream.h>

#include <iomanip.h>

 

int

main()

{

     float num1;

 

     num1 = 123.456;

 

cout << '|' << num1 << '|' << endl;

 

     cout << setiosflags(ios::fixed);

     cout << setiosflags(ios::showpoint);

 

     cout << setprecision(2) << num1 << endl;

 

     cout << '|' << setw(8) << num1 << '|' << endl;

 

cout << '|' << setw(1) << num1 << '|' << endl;

}

 

 

 

 

 

 

 

 

 

 

 


6.    (6 points) A C++  program contains the following statements:

 

float a = 0.1;

float b = 0.3;

float c, *pa, *pb;

 

pa = &a;

*pa = 2 * a;

pb = &b;

c = 3 * (*pb - *pa);

 

Suppose each floating-point number occupies 4 bytes of memory.  Assume further that the address of variable a is  1130, the address of variable b is  1134, and the address of variable c is  1138.

 

Draw a layout of memory, its variables and its pointers.  Then answer the following questions. 

 

i)                    What value is assigned to &b?_______________________

 

ii)                   What value is assigned to pa?    ___________________________

 

iii)                 What value is represented by *pa? __________________________

 

iv)                 What value is represented by &(*pa)? _______________________

 

v)                  What value is assigned to c?    ____________________________

 

vi)                 What value is represented by *(&a)? ________________________


7. Use the following C++ program to answer questions a - e

 

#include <iostream.h>

const float num = 2.5;

void stuff (float& a, int b);

 

void

main()

{

     float  r = 0;

     int s, t;

 

     s = 5;

     t = 10;

 

     cout << "The value of r, s, and t are: "

          << r << ' ' << s << ' ' << t << endl;

 

     stuff(r, t);

 

     cout << "The value of r, s, and t are: "

          << r << ' ' << s << ' ' << t << endl;

 

     stuff(r, s);

 

     cout << "The value of r, s, and t are: "

          << r << ' ' << s << ' ' << t << endl;

}

 

void stuff(float& a, int b)

{

     float c;

 

     c = a + num;

 

     ++a;

     ++b;

 

     cout << "In stuff the values of a, b, and c are: "

          << a << ' ' << b << ' ' << c << endl;

}

 

a) (1 points) Which variable(s) are global to main and stuff?_________________________

 

b) (1 points) List the reference parameter(s) in stuff. _______________________________

 

c) (1 points) Which variable(s) are local to stuff?__________________________________

continued on next page

 

d)  (3 points) A CSC 126 student from the evening class, added the following function call to the program. 

 

stuff(t,r);

 

The compiler flagged this statement with an error message.  Why?

 

 

 

 

 

e) (4 points) What is printed by this program?

 

 

 

 

 

 

8. a) (3 points) Sort the following numbers using the bubble sort.  Do not write any code. Show the values after each pass (iteration)  through the numbers.

 

6    12    38    3    6    37

 

 

 

 

 

 

 

 

 

 

 

b. (3 points) Using the same unsorted array: 6   12   38   3   6    37

Show the first three passes for the selection sort.

 

 

 


9. (6 points) Determine if the following relational expressions evaluates to a 0 (false) or a 1 (true).  Assume the following declarations:

 

            int i = 10, j = 14, k = 24;

     double x = 11.5;

 

           

a) i + 2 == k – 12

Ans:

 

 

b) j % i * k || k % i * j

 

Ans:

c) (i + (2 * j)) > k

Ans:

 

 

 

 

10. (4 points) Convert the following if…else chain to a switch statement.  Write ONLY THE SWITCH STATEMENT.

 

if (code == 1)

cout<< ”The sum is “ << fnum + snum << endl;

 

     else if (code == 2)

           cout<< "The product is" << fnum * snum << endl;

     else

           cout << ”The difference is: " << fnum – snum << endl;


11.(6 points)  Assuming the following is the input file called personnel.txt (and it opens properly), what is the output of the following program:

 

The input file personnel.txt

                        Roy                  163-98-4182   7.32     37

                        Richter 189-53-2147   8.32     40

                        Nolan               145-32-9826   6.54     40

                        Jeter                 163-09-4263   9.80     35

 

The program:

 

#include <fstream.h>

#include <iomanip.h>

#include <stdlib.h>

 

void main()

{

     ifstream in;

     char ch, name[30], ss[12], a;

     float rate, hours, net;

 

     in.open("personnel.txt",ios::nocreate);

     if (in.fail())

     {

           cout<<"\nThe input file failed to open"<<endl;

           exit(1);

     }

     cout<<"\nSoc. Sec. No.\t\tName\t\tNet Pay\n";

     cout<<"-------------\t\t----\t\t-------\n";

 

     cout<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint)

           <<setprecision(2);

 

     while ((ch=in.peek())!=EOF)

     {

           in >> name >> ss >> rate >> hours;

           net = rate * hours;

           cout<< ss << "\t\t" << name << "\t\t" << net << endl;

     }

 

     in.close();

     return;

}


12. (a - d are 2 points each)  Suppose you have strings:

 

char S1[20] = “fast”;

char S2[20] =  “break”;

char S3[20];

 

int X, Y, Z;

 

a)                  What is the value of  S2[1]? ______________________

 

 

b)                  Z = strcmp (S1,   S2); Is Z positive, negative or 0?_________________

 

 

c)                  After strcat (S2, “neck”) what is in S2?_____________________

 

 

d)                  After stcpy (S3, S1) what is in S3?________________

 

 

For strings S5 and S6 of unknown length and value.

 

 

e)                  (4 points) Write C++ to count the number of occurrences of the letter 'z'   in string S5.

 

 

 

 

 


13. (15 points)  The Bite-a Vite-a vitamin store needs an “expert” computer programmer to help keep track of their inventory.  The store maintains all their vitamin stock information on disk.  The data is organized as follows: 

 

The first column contains the vitamin name (A, B, C, etc.)

The second column contains the unit price of a single jar of that particular vitamin. 

The third column has the number of jars of that vitamin in the store. 

 

For example:

 

A  12.95   23

K   9.99   56

Z   6.99   25

 

Write a C++ program, on the next page,  that will do the following:

 

  1. Read data from a data file called inventory located on the store’s hard drive in a directory called  c:\store.   The store inventory fluctuates so you do not know how much data there is to process.

 

  1. Call a function that will return the total value of the store’s inventory for that one vitamin product (unit_price  * number_of_jars).

 

  1. You will also calculate:

i)                    The average price of a vitamin

ii)                   The total inventory (total number of jars)

 

  1. Print the output so that it is organized as follows:

 

 

Vitamin                 Price                         Inventory                     Total Inventory

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

A                            12.95                                23                        297.85

K                              9.99                                56                        559.44

Z                               6.99                                25                        174.75

 

The average price for a vitamin is:  $9.98

The total store inventory is 104 jars of vitamins

 

 

 

 

 

 

 

      

 


 

WRITE YOUR PROGRAM IN THE SPACE PROVIDED BELOW