Name  ___________________


CPTR246  Spring '02 (100 total points)                                                                 Exam 2


1.      Pointer Basics  Next to each cout statement, write what would be displayed on the screen. (16 points)


int x = 0;

int y = 1;

int * xptr;

int * yptr;

int numbers[5] = {7, 9, 14, 12, 10};


xptr = &x;

yptr = &y;


cout << * xptr << “ “ << * yptr << endl;



x = 3;

yptr = numbers;



cout << * xptr << “ “ << * yptr << endl;



cout << * numbers << endl;



cout << * numbers + 2 << endl;



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



cout << yptr[*xptr] << endl;

2.      Pointer parameters (the old C way)  In the following program that computes the perimeter and area of a triangle, make all of the changes necessary 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).  (9 points)


#include <iostream>

#include <cmath>

#include <string>


using namespace std;


void compute(double a, double b, double c, double & area, double & perimeter){

// pre:    a, b, and c are the sides of a valid triangle

// post:  area and perimeter are computed


   perimeter = a + b + c;


   double s = perimeter / 2.0;      // semiperimeter (half of perimeter)


   area = sqrt(s * (s - a) * (s - b) * (s - c));   // formula given in text





int main() {


   double a, b, c, area, perimeter;


   cout << "Enter the three sides of your triangle: " << endl;


   cin >> a;


   cin >> b;


   cin >> c;


   if ((a + b > c) && (a + c > b) && (b + c > a)){


         compute(a, b, c, area, perimeter);


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


         cout << "The perimeter is " << perimeter << endl;





         cout << "That was not a valid triangle!" << endl;


   return 0;



3.      Command Line Arguments: (15 points)


Complete the following program that takes an integer and doubles it.  The integer must be entered on the command line.  If an integer is NOT in the command line, end the program with an appropriate message.  (The isInt function is provided for you.)  Example runs:


double.exe 5     (displays 10)

double.exe 100   (displays 200)



#include <iostream>

#include <cctype>


bool isInt(char * s){

// pre:     s1 is a null-terminated char array

// post:    returns true if s1 is an integer, false otherwise

// purpose: to determine if null-terminated char array contains an integer


int i(0);

if (s[i] == ‘-’)


for (; s[i] != ‘\0’; i++){

            if (!isdigit(s[i]))

                  return false;


      return true;



4.      Strings  Write the following function twice, first using null-terminated character arrays and then using string objects.  (30 points)


Write a void function called “convert” that converts a word to “pig Latin.”  It takes one parameter (a string) which you can assume has just one word in it.  (Now I know real “pig Latin” gets more complicated than this, but let’s keep it simple!)  Take the first letter off of the string and put it on the end, then add “ay” to the end.  For example, “test” would become “esttay” and “finish” would become “inishfay.”


Using null-terminated character arrays:





















Using string objects:  (Hint:  Use the .erase and .append functions.)

5.      2-dimensional Arrays


  1. Define a 2-dimensional array of integers named labGrades with 22 rows and 13 columns. (5 points)





  1. Write a function called highestRow that takes two parameters, the first, a 2-dimensional array of integers that has 6 columns, and the second, an integer containing the number of rows in the array.  The purpose of the function is to find the row whose sum is the largest.  It returns the index of that row.  (20 points)


(This function is to be used in the next (and last) question.  There is additional room on the following page if needed.)



















































  1. The array “tickets” defined below contains the number of tickets sold this year for each of six sports (basketball, football, lacrosse, swimming, tennis, and wrestling) at six colleges.  Each row has the information for one college.  Each column has the information for one sport.  Using the function in the previous question, write code that displays the name of the school that sold the most tickets this year.  (The 6 names in the array “colleges” are in the order corresponding to the rows in the array “tickets.”)  (5 points)



string colleges[6] = {“DeSales”, “Drew”, “Juniata”,

“Kings”, “Lycoming”, “Wilkes”);

int tickets [6][6];

. . .    //  array tickets is loaded with values