Name  ______________________________

 

CPTR246  Spring '13 (100 total points)                                                               Exam 2

 

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

 

struct employee {             // defines employee "type"

      int ID;                       // employee id number

      double hourlyRate;            // pay rate

      char name[30];                // employee's first name

      double hours;                 // hours this week

      int weeks;                    // number of weeks of vacation

};

 

employee employee1 = {123, 12.75, "Mary", 40, 1};     // first employee

employee employee2 = {567, 10.75, "John", 50, 2};     // second employee

employee * empptr = & employee1;

 

(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

 

            employee1.ID           __________________     ________________________

 

            empptr[ID]             __________________     ________________________

           

            employee2.name         __________________     ________________________

 

            employee2.name[0]      __________________     ________________________

 

 (b) Write if/else statements that determines (assuming the number of hours worked has changed and you don’t know the values) which employee worked the most hours and displays his/her name on the screen. (Do not use literals, like “Mary” or “John”.  Display the literal “none” if they worked the same number of hours.) (4 points)

 

 

 

 

 

 

 

 

 

 

 

(c) Complete the function below called MoreVacation that takes two parameters, an employee pointer and an integer. The function is to add the integer to the number of weeks vacation that this employee has. It returns nothing. (4 points)

 

void MoreVacation(employee * empptr, int newWeeks){

 

 

 

}


2.      Null-terminated Character Arrays:

Using the lavender handout containing Figures 21.21, 21.34, and 21.17, answer the following questions:

 

1  #include <iostream>

2  using namespace std;

3

4  int main()

5  {

6       char s1[20] = "cat";

7       char s2[20] = "dog";

8       char s3[20] = "cow";

9       char str[20];

10      char * strptr;

11

12      // question (a)  ß----------- what must go here for (a)

13      cout << str << endl;  //ß---- output line for question (a)

14

15      // question (b)  ß----------- what must go here for (b)

16      cout << strptr << endl;  //ß---- output line for question (b)

17

18      return 0;

19 }

 

(a)    Which ONE of the following statements, if placed at line 12, would cause the string “cat” to be displayed by line 13? (3 points)

 

a.       str = s1;

b.      *str = *s1;

c.       strcpy(str, s1);

d.      strcpy(s1, str);

 

 

(b)   Which ONE of the following statements, if placed at line 15, would cause the string “cowdog” to be displayed by line 16? (3 points)

 

a.       strcat(s3, s2);

b.      strcpy(strptr, strcat(s3, s2));

c.       strcat(s2, s3);

d.      strcpy(strptr, strcat(s2, s3));

 

 

(c)    Which ONE of the following statements is false? (3 points)

 

a.       s1 is a character pointer

b.      str is a character pointer

c.       strptr is a character pointer

d.      None of the above.  That is, they are all true.

 


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

 

#include <iostream>

using namespace std;

 

void funfun(char * x){

  *x = 'L';

  *(x + 5) = 'L';

}

 

int main() {

  int x = 10;

  int y = 20;

  int * aptr;

  int * bptr;

  int * cptr;

  int array[5] = {10, 20, 30, 40, 50};

  char name[20] = "Harry Potter";

 

  aptr = &x;

  bptr = &y;

  cptr = array;

 

  cout << *aptr << " " << *bptr << " " << *cptr << endl;

 

  x = 15;

  *bptr = 25;

  *(cptr + 3) = 15;

 

  cout << x << " " << y << endl;

  cout << *aptr << " " << *bptr << endl;

 

  *aptr = *cptr;

 

  if (aptr == array)

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

  else

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

 

 

  if (*aptr == *array)

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

  else

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

 

  cout << *(array + 3) << " " << (*array + 3) << endl;

 

  cout << bptr[0] << endl;                 

 

  funfun(name);

  cout << name << 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 “compute to be call by reference using pointer parameters (known as the old C way).  (8 points)

 

#include <iostream>

 

using namespace std;

 

const double PI = 3.14159;

 

 

void compute(double radius, double & circumference, double & area){

// pre:     radius is a positive real number

// post:  area and circumference are computed

 

      circumference = 2 * PI * radius;

 

      area = PI * radius * radius;

 

}

 

 

int main() {

 

      double r, circum, area;

 

      cout << "Enter the circle’s radius: " << endl;

 

      cin >> r;

 

   compute(r, circum, area);

 

      cout << "The area is " << area << endl;

 

      cout << "The circumference is " << circum << endl;

 

      return 0;

 

}


5.      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 values that has r rows and c columns.  (4 points)

 

int * values;

 

 

 

 

b.      Write code that displays the integers in the array values by rows (that is, go to a new line after each row), with column widths of 6.  You may assume that iomanip has been included.  (6 points)

 

 

 

 

 

 

 

 

 

 

 

 

 

c.       Assume that the array values has been loaded with data.  Write code to count the number of integers in the array that have the value 0.  Set the Boolean variable sparse to true if 90% or more of the entries are zero, and false otherwise.  (10 points)

 

      bool sparse;

 

 

 

 

 

 

 

 

 

 

 

 

 

 


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

 

Write a program that takes a file of integers as input.  The program is to determine how many of the integers are equal to 0, how many are less than 0, and how many are greater than 0.  Display those totals (identifying which is which) at the end. 

 

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” – that is, print zero totals).

·         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.)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

(An additional blank page has been added for your use.)