Name  ______________________________


CPTR246  Spring '12 (100 total points)                                                               Exam 2


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


   struct student {              // defines student "type"

         int ID;                 // identification number

         string name;            // full name

         int credits;            // credits earned to date

         string nicknames[4];    // nicknames for students



   student st1 = {1, "Joseph James Cool", 32, "Joe Cool", "JoeC", "JJ", ""};

   student * st2 = &st1;

   student cptr300[30];


(a)    Which ONE of the following statements displays student st1’s name? (3 points)


a.       cout << st1(name) << endl;

b.      cout << st1[name] << endl;

c.       cout << << endl;

d.      cout << st1->name << endl;


(b)   Which ONE of the following statements also displays student st1’s name? (3 points)


e.       cout << st2(name) << endl;

f.       cout << st2[name] << endl;

g.      cout << << endl;

h.      cout << st2->name << endl;


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


i.        cptr300 is a student pointer

j.        st1 is a student pointer

k.      st2 is a student pointer

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


(d) Assume that the array cptr300 has been loaded with the data for 30 students.  Write code that will display (one line per student, in column form) every student’s ID, name, credits, and first nickname. (7 points)




2.      Null-terminated Character Arrays:

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


char first[20] = “John”;

char last[20] = “Doe”;

            char full[40];

            char name[40];

cout << "Enter your full name (first and last only): ";

cin.getline(name, 40);


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


·         Using strcpy and strcat, write code that results in the string full having the contents of first followed by a space and then followed by the contents of last.  For full credit, do so without changing the contents of either first or last. (4 points)









·         Using strcmp, write code that will display “Welcome back, ______”  (where the blank contains the contents of first) on the screen if full contains the same string as name.  (4 points)









·         Complete the following function that takes a null-terminated character array and a character as parameters.  The purpose of the function is to replace every occurrence of the second parameter in the first parameter with an ‘*’   For example, the function call starOut(“Hello world”, ‘l’) would result in the string containing “He**o wor*d”. (4 points)


void starOut(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. (21 points – 3 points per line of output)


#include <iostream>

using namespace std;


void funfun(int * x){

  *x = 25;



int main() {

  int x = 5;

  int y = 10;

  int * aptr;

  int * bptr;

  int * cptr;

  int array[6] = {15, 25, 35, 45, 55, 65};


  aptr = &x;

  bptr = &y;

  cptr = array;


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


  x = 15;

  *bptr = 25;

  *(cptr + 3) = 15;

  funfun(array + 2);


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

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

  if (*cptr == *array)

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


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


  aptr = cptr;


  if (cptr[3] == x)

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


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


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


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



#include <iostream>


#include <iomanip>


using namespace std;


void compute(double itemCost, double discount, double taxrate,


double & newcost, double & aftertaxes)



   newcost = itemCost * (1 - discount);


   aftertaxes = newcost * (1 + taxrate);




int main() {


  double price, discount, taxrate, cost1, cost2;


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


  cin >> price;


  cout << "What is the percentage off (e.g. 10% off would be .10): ";


  cin >> discount;


  cout << "What is the tax rate (e.g. 6% would be .06): ";


  cin >> taxrate;


  compute(price, discount, taxrate, cost1, cost2);


  cout << "The discounted price is $" <<


fixed << setprecision(2) << cost1 << endl;


  cout << "After taxes, that'd be $" <<


fixed << setprecision(2) << cost2 << 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 scores that has r rows and c columns.  (4 points)







b.      Write code that will initialize every entry in scores to 100.  (6 points)














c.       Write code to determine if the array scores has a column of all zeros.  Set the Boolean variable found­ to true if there is a column of all zeros and false otherwise.  (10 points)


      bool found;















6.      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 positive, negative, or zero.  Keep track of how many of each there are and display those totals 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”).

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