Name  ___________________


CPTR246  Spring '01 (100 total points)                                                 Exam 1


1.      Pointers:  (10 points)


Consider the following code:


              line #


      1   int x = 25;

      2   int * somePtr;

      3   somePtr = & x;


      5   cout << x << endl;            

      6   cout << *somePtr << endl;

      7   cout << &x << endl;

      8   cout << somePtr << endl;

      9   x = 50;

      10   cout << *somePtr << endl;

      11   *somePtr = 100;

      12   cout << *somePtr << endl;

      13   cout << x << endl;



Answer the following, giving ALL answers that apply:


a)      Give the line number(s) of the statement(s) that display(s) a 25 on the screen.




b)      Give the line number(s) of the statement(s) that display(s) a 50 on the screen.




c)      Give the line number(s) of the statement(s) that display(s) a 100 on the screen.




d)      Give the line number(s) of the statement(s) that display(s) a memory address in hexadecimal on the screen.




e)      Give the line number(s) of the statement(s) that make(s) the connection so that somePtr  “points to” the integer variable x.




2.      Pointers and arrays: (10 points)


What is the output of the following code?


      int digits[8] = {6,7,3,2,4,1,0,5};

      int * dptr = digits;



      cout << digits[0] << endl;



      cout << *(digits + 4) << endl;



      cout << *digits + 4 << endl;



      cout << dptr[6] << endl;



      for (dptr = &digits[2]; *dptr; dptr++){

            cout << * dptr << “ “;








  1. Consider the following variable definitions and function prototype:


         int values[100];      // An array to hold up to 100 integers

int size;       // The number of integers in the array


void loadArray(int arrayToFill[], int * n);

   // pre:   none

   // post:  numbers have been put into the array from

   //          some file and the variable n has been updated

   //          to be the number of elements in the array



Write a single C++ statement that calls the function loadArray, so that the array values and integer size are updated properly.  (Assume that the function has been written somewhere else in the program.)  (5 points)



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


char message1[20] = “past due”;

char message2[20];

char message3[80];

cout << "Enter optional message: " << endl;

cin.getline(message2, 19);


Using the string functions described on page 346-347, write C++ code that will perform each of the following tasks. 


·        Using strcmp, write code that will display “Invalid message” on the screen if message2 is equivalent to message1.  (3 points)









·        Using strlen, write code that will display the longest message (of message1 and message2) on the screen. (3 points)









·        Using strcat and strcpy, set message3 equal to “Your account is _____”, where the blank is filled in by the contents of message2.  (4 points)





5.      Formatting Output: (20 points) 


Write a function called formatStudentData that takes the three parameters listed below and displays them on the screen with the column widths and justifications indicated.  The function is not to return anything.


Student name (a character array)                           30            left-justified

Exam grades (an array of 3 integers)                                   5            right-justified

    (make 3 columns, one for each exam grade)

GPA (a double)                                                 8            right-justified,

                                                                                                  3 decimal positions



A sample call to the function would be


char name[30];

int exams[3];

double GPA;

 . . .

formatStudentData(name, exams, GPA);


6.      Verifying Input: (20 points)


Write a function called getEmail that will ask the user to enter his/her email address, get it from the keyboard, and verify that it is in the correct format.  Verify as follows:


·        There is exactly one ‘@’ symbol and it is not the first character

·        It has at least one period ‘.’ after the ‘@’ sign

·        The period ‘.’ is not the last character

·        There is no embedded whitespace (blanks, tabs, etc.) of any kind


The function takes one parameter, a character array to hold the email address.  It is to return true if a valid email address was entered by the user, false otherwise.



7.       Call by reference (the old C way) and verifying input: (25 points)


Write a function called getStudentData that asks the user for a student’s full name (character array with embedded blanks) and telephone extension number (integer).  The function takes two parameters corresponding to these pieces of information, all of which will be updated by the function.  The function returns nothing.


For full credit, you must

·        use pointer parameters for both parameters

·        use the extraction operator to get the telephone extension

·        properly handle the case when the user does not enter an integer when directed to do so, by notifying the user of his/her error and asking them to try again until they do so correctly