Name  ______________________________

 

CPTR246  Spring '11 (100 total points)                                                               Exam 2

 

1.      Structures:  Assume that the following code appears in your program. 

 

   struct professor {               // defines professor "type"

      int ID;                       // employee id number

      char name[40];                // employee’s name

      int count;                    // number of courses teaching this semester

      string courses[4];            // names of the courses this semester

      };

 

professor DrGood = {123, “I. B. Good”, 3, “CSC150”, “ITT221”, “TEC128”};

professor * profptr = & DrGood;

 

(a)  For each of the following, indicate if the reference is a syntax error or not.  If not, give the value of the reference?  (8 points)

 

      Reference                                Error? (yes or no)                 If no, give its value

 

            DrGood.count           __________________     ________________________

 

            DrGood->name           __________________     ________________________

           

            profptr->courses[0]    __________________     ________________________

 

            *profptr->name[0]      __________________     ________________________

 

 

(b)   Which ONE of the following statements displays Dr. Good’s employee ID number? (4 points)

 

a.       cout << DrGood << ID << endl;

b.      cout << DrGood.ID << endl;

c.       cout << DrGood->ID << endl;

d.      cout << DrGood[ID] << endl;

 

 

(c) Which TWO of the following for loops are valid ways to display the courses that DrGood is teaching? (4 points)

 

 

a.       for (int i = 0; i < 4; i++)

      cout << DrGood.courses[i] << endl;

b.      for (int i = 0; i < DrGood.count; i++)

cout << DrGood.courses[i] << endl;

c.       for (int i = 0; i < profptr->count; i++)

cout << profptr->course[i] << endl;

d.      for (int i = 0; i < profptr.count; i++)

cout << profptr.course[i] << endl;

 

 

2.      Null-terminated Character Arrays:

Consider the following definitions of null-terminated character arrays and other C++ code.

 

char str1[20] = “Hello world”;

char str2[20];

            char combo[40];

cout << "Enter your greeting: ";

cin.getline(str2, 20);

 

Using the functions described on the handout, write C++ code that will perform each of the following tasks. 

 

·         Using strcmp, write code that will display “Duplicate greetings” on the screen if str2 is equivalent to str1.  (4 points)

 

 

 

 

 

 

 

 

·         Using strcpy and strcat, write code that result in the string combo having the contents of str1 followed by the contents of str2.  For full credit, do so without changing the contents of either str1 or str2. (4 points)

 

 

 

 

 

 

 

 

·         Complete the following function that takes a null-terminated character array and a character as parameters.  It returns the number of times the character appears in the string.  For example, the function call countChar(“Hello world”, ‘l’) would return a 3. (4 points)

 

int countChar(char * str, char x){

 

 

 

 

 

 

 

 

 

 

 

 

}


3.      Pointer Basics 

 

(a)  What is a pointer?  (3 points)

 

 

 

 

 

 

(b)   To the right of the following code, write what would be displayed on the screen. (22 points – 2 points per line of output)

 

 

#include <iostream>

 

using namespace std;

 

int main() {

  int a = 10;

  int b = 20;

  int * xptr;

  int * yptr;

  int * zptr;

  int nums[4] = {21, 18, 22, 15};

 

  xptr = &a;

  yptr = &b;

  zptr = nums;

 

  cout << *xptr << " " << *yptr << " " << *zptr << endl;

  cout << *(nums + 2) << endl;

  cout << *nums + 2 << endl;

 

  *xptr = 15;

  *yptr = nums[3];

  *zptr = 20;

  zptr[2] = *zptr;

 

  cout << a << " " << b << endl;

 

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

      cout << zptr[i] << endl;

 

  if (zptr == nums)

    cout << "test 1 is true" << endl;

  else

    cout << "test1 is false" << endl;

 

  if (*xptr == *nums + 3)

    cout << "test 2 is true" << endl;

  else

    cout << "test 2 is false" << endl;

 

  cout << xptr[0] << endl;               

 

  return 0;

}

 


4.      Pointer parameters (the old C way)  In the following program, make all of the changes to convert the call-by-reference parameters in the function “computeOptions to be call by reference using pointer parameters (known as the old C way).  (8 points)

 

 

#include <iostream>

 

using namespace std;

 

void computeOptions(double itemCost, int rebate, double discount,

 

double & costWithRebate, double & costWithDiscount)

{

 

   costWithRebate = itemCost - rebate;

 

   costWithDiscount = itemCost - (itemCost * discount);

 

}

 

int main() {

 

   double price, discount, cost1, cost2;

 

   int rebate;

 

   cout << "What is the cost of the item: ";

 

   cin >> price;

 

   cout << "How much is the rebate (whole dollars)?";

 

   cin >> rebate;

 

   cout << "What percentage is the discount (e.g., enter .10 for 10%)?";

 

   cin >> discount;

 

   computeOptions(price, rebate, discount, cost1, cost2);

 

   if (cost1 < cost2)

 

         cout << "Take the rebate offer." << endl;

 

   else

 

         cout << "Take the discount offer." << endl;

 

   return 0;

}

 


5.      Linked Lists – Consider the following linked list of integers (sorted from lowest to highest) and the changes described.  Make the necessary changes in the drawing to indicate what must change in the linked list for each change described.  (9 points)

 

(a)    Add integer 64                                                                   (b) Add integer 15

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 (c)  Add integer 42                                                        

 

 



6.      Dynamically Allocated Arrays – Assume that the following code has been written:

 

      int r,c;

      cout << "How many rows do you have?";

      cin >> r;

      cout << "How many columns do you have?";

      cin >> c;

 

a.       Write code to dynamically allocate an array of integers called matrix that has r rows and c columns.  (4 points)

 

 

 

 

 

 

b.      Write code that will initialize every entry in matrix to zero.  (6 points)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

7.      Command Line Arguments and File Processing: (20 points)

Write a program that takes a file of integers as input.  For each integer, the program is to determine if it is even or odd.  (Recall, an integer is even if when divided by 2, the remainder is 0.  Otherwise, it’s odd.)  Keep track of how many of each (even and odd integers) there are and display it at the end.  Also, display the sum and average of all of the integers in the file. 

 

Important points:

·         The name of the file MUST come from the command line.  If it is NOT there, end the program with an appropriate error message.

·         Do NOT display any totals if the input file did not open properly.  Display an appropriate error message and end the program instead.

·         You may assume that the file contains only integers. 

·         However, do not assume that it contains at least one integer (i.e., your program should handle an empty file “gracefully”).

·         You do NOT need to have more than the main function.

·         You do NOT need to include any comments.

 

(An additional blank page has been added for your use.  You may remove it if you like.)