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;
}
else
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] == ‘-’)
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
(This function is to be used in the next (and last) question. There is additional room on the following page if needed.)
string colleges[6] = {“DeSales”, “Drew”, “Juniata”,
“Kings”, “Lycoming”, “Wilkes”);
int tickets [6][6];
. . .
// array tickets is loaded with
values