Name  ___________________


CPTR246  Spring '07 (100 total points)                                                                 Exam 2


1.      Pointer parameters (the old C way)  In the following program that computes the taxes and total cost for a hotel stay, make all of the changes necessary to convert the call-by-reference parameters in the function “computeHotelStay to be call by reference using pointer parameters (known as the old C way).  (10 points)



#include <iostream>


const double stateTax = .06;

const double countyTax = .015;


using namespace std;


void computeHotelStay(double roomRate, int nights,

                        double & state, double & county, double & total)



  double costBeforeTaxes  =  roomRate  *  nights;


  state = costBeforeTaxes  *  stateTax;


  county = costBeforeTaxes  *  countyTax;


  total = costBeforeTaxes  +  state  +  county;



int main() {

  double roomRate, sTax, cTax, cost;

  int nights;


  cout << "What is the cost of the hotel room per night: ";

  cin >> roomRate;


  cout << "How many nights are you staying: ";

  cin >> nights;


  computeHotelStay(roomRate, nights, sTax, cTax, cost);


  cout << "The total cost is " << cost << endl;


  return 0;



2.      Pointer Basics  To the right of the following code, write what would be displayed on the screen. (24 points)


#include <iostream>


using namespace std;


int main() {

  int x = 10;

  int y = 20;

  int * aptr;

  int * bptr;

  int * cptr;

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


  aptr = &x;

  bptr = &y;

  cptr = values;


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


  x = 30;

  *bptr = 40;


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

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


  if (*cptr == values[0])

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


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


  if (cptr[3] == x)

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


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


  cout << (*values + values[4] + *bptr) << endl;


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


  cout << aptr[0] << endl;       // extra credit (2 points)



  return 0;



3.      Structures:  Assume that the following code appears in your program and the array myClass has been loaded with information on 4 students.  Circle the letter of the correct answer.


struct studentSchedule{

      int socSecNbr;          // unique student ID number

      string name;            // name of the student

      int numberOfCourses;    // how many courses they have this semester

      string course[6];       // the course codes


studentSchedule myClass[4];   // an array of students

studentSchedule Jane = {123456789, “Jane Doe”, 4, “CPTR125”, “MATH128”,

                         “PHIL115”, “SOC110”, “”, “”);


(a)    Which of the following statements displays Jane’s social security number? (4 points)


a.       cout << socSecNbr << endl;

b.      cout << Jane(socSecNbr) << endl;

c.       cout << Jane.socSecNbr << endl;

d.      cout << Jane[socSecNbr] << endl;



(b)  Which of the following for loops displays the names of the students in the array myClass? (4 points)


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

      cout << myClass[i].name << endl;

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

cout <<[i] << endl;

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

cout << myClass[i].name[i] << endl;

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

cout << name.myClass[i] << endl;



4.      Input:  Explain why we need to be careful when we mix extraction (>>) with getline and get when reading input from either cin or an input file.  (6 points)






  1. Null-terminated Character Arrays:

Consider the following definitions of null-terminated character arrays (also known as C-strings) and other C++ code.


char firstName[20];

char lastName[20];

            char fullName[40];

cout << "Enter your first name: ";

cin.getline(firstName, 20);

cout << "Enter your last name: ";

cin.getline(lastName, 20);


Using the string functions described on pages 446-447 of your text, write C++ code that will perform each of the following tasks. 


·        Using strcmp, write code that will display “Cool name!” on the screen if the person has the same first and last name  (4 points)













·        Using strcpy and strcat, write code that results in the string fullName having the contents of firstName followed by the contents of lastName (with a space in between) without changing the contents of either firstName or lastName. (6 points)











6.      Complete the following function that takes two parameters, a string (as a null-terminated character array) and a character.  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. (6 points)


Hint:  It will likely be easiest to code it without the use of the string functions from the text.  However, if you prefer, there are additional string functions on pages 1089-1090.


int countChar(char * str, char x){





















7.      Formatting output:  Write cout statements to display the following as described.  (6 points)


  int x = 10;

  double y = 12.345678;



a. Print x right justified in a 10-digit field.







b.  Print y to the nearest hundredth.







c.  Print x in hexadecimal form preceded by 0x.


8.  Command Line Arguments and File Processing: (30 points)


Write a complete program that does the following:


Ø      The program takes an input file that contains integers.  (You can assume that the file contains integers and only integers.)

Ø      The user may enter the name of the file on the command line.  If they do not, prompt them for the name of the file.

Ø      If the user gives you a bad file name, display an error message on the screen and end the program.

Ø      Count how many integers are on the file and compute their sum.  Display both values on the screen.






































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