Name ___________________

 

CPTR246 Spring '00 (100 total points) Exam 1

 

1.     Pointers: (10 points)

 

What is the output of the following code? The diagram is here for your use, but it also shows you the address of the integer object i.

 

 

 

 

 


int i = 500;

int * aPtr;

 

aPtr = & i;

 

cout << i << endl;

cout << &i << endl;

 

cout << * aPtr << endl;

 

*aPtr = 100;

 

cout << *aPtr << endl;

cout << i << endl;

 

 

2.     Pointers and arrays: (10 points)

 

What is the output of the following code?

 

int values[5] = {95, 72, 89, 92, 81};

int * vptr = values;

 

cout << values[3] << endl;

 

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

 

cout << vptr[4] << endl;

 

cout << *(vptr + 1) << endl;

 

cout << *vptr << endl;

 

 

 


3.     Call by reference (the old C way): (10 points)

 

Change the call by reference using reference parameters (the way we learned to do call by reference in CPTR 125) for the getXY function to use call by reference using pointer parameters. Simply indicate the changes on the listing.

 

/************************************************

* Program: getPoints.C *

* Author: Eileen Peluso *

* Date: 09/10/98 *

* *

* Abstract: Program to demonstrate functions *

* call by value and call by reference *

* and Mercer's string class *

************************************************/

#include <iostream.h>

#include <math.h>

 

void getXY(char * message, double & x, double & y) {

// pre: message must contain instructions for the user

// post: x and y will contain values entered by the user

// purpose: to get the x, y coordinates of a point

cout << message << endl;

cin >> x >> y;

}

 

double calcDistance(double x1, double y1, double x2, double y2){

// pre: x1,y1 and x2,y2 are coordinates of points on the xy-plane

// post: the distance between the two points will be returned

 

double distance = sqrt( (x2 - x1) * (x2 - x1) +

(y2 - y1) * (y2 - y1));

return distance;

}

 

 

int main() {

double x1, y1; // coordinates of point 1

double x2, y2; // coordinates of point 2

cout.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(10);

getXY("Enter the coordinates of the first point", x1, y1);

getXY("Enter the coordinates of the second point", x2, y2);

cout << "The distance between them is " <<

calcDistance(x1, y1, x2, y2) << endl;

return 0;

}


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

 

char myString[30] = "This is my string.";

char yourString[30] = "That is your string.";

char herString[30] = "What is her string?";

char hisString[30];

cout << "Enter his string: " << endl;

cin.getline(hisString, 80);

 

Using the string functions described on page 325, write a single C++ statement that will perform each of the following tasks.

 

 

       Display the length of myString on the screen using the strlen function. (3 points)

 

 

 

 

       Copy the contents of myString to yourString using strcpy. (3 points)

 

 

 

 

       If the first 5 characters of hisString are equal to the first 5 characters of herString, display a message saying "The first 5 characters are equal!" on the screen. This is obviously an if statement. Use the function strncmp in the condition. (4 points)

 

 

 

 


5.     Formatting Output: (20 points)

 

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

 

CD title (a character array) 30 left-justified

Artist's name (a character array) 20 left-justified

Price of the CD (double) 8 right-justified, 2 decimal pos.

Number in stock (integer) 4 right-justified

 

A sample call to the function would be

formatCD("Kenny G's Greatest Hits", "Kenny G", 17.95, 5);

 


6.     Verifying Input: (20 points)

 

Write code (it doesn't need to be a complete function) that will ask the user to enter a phone number and verify that it is in the correct format. The correct format is as follows:

 

123 555-1212

 

where 123 is the area code, followed by a single space, followed by the phone number with the '-' in the normal position.

 

We want to be able to treat it as a string once we're done - - that means, make sure the terminating null is where it should be when you're finished. Use the following character array to hold the phone number:

 

char phone[13];

 

 


7.     Verifying input: (20 points)

 

Consider the following code, where the user is asked to enter an integer. If the user does not enter an integer when asked, the program will go into an infinite loop.

 

Correct the code so that if the user enters something other than an integer, it will display an appropriate message and force the user to enter a proper value.

 

int getInt(char * message, int low, int high){

// pre: low <= high

// post: returns the number entered by the user (which

// must be between low and high)

 

int value;

 

cout << message << " between " << low

<< " and " << high << endl;

 

cin >> value;

 

while (!(value <= high && value >= low)){

 

cout << "That was not in the range" << endl;

 

cout << "Please try again" << endl;

 

cin >> value;

 

}

 

return value;

 

}