Name ___________________
CPTR246 Spring '03 (100
total points)
Exam 2
1. 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() {
double a = 1.2;
double b = 1.2;
double * myptr;
double * yourptr;
double myarray[46] = {1.2, 2.3, 3.4, 4.5};
myptr = &a;
yourptr = &b;
cout << *myptr << " "
<< *yourptr << endl;
if (myptr == yourptr)
cout << "Test1 is true"
<< endl;
else
cout << "Test1 is false"
<< endl;
*yourptr = 0.5;
cout << *myptr << " "
<< *yourptr << endl;
cout << a << " "
<< b << endl;
cout << *myarray + 1 << endl;
cout << *(myarray + 1) << endl;
if (*myptr == *myarray)
cout << "Test2 is true"
<< endl;
else
cout << "Test2 is false"
<< endl;
myptr = myarray;
*(myptr + 2) = 7.8;
for (int i = 0; i < 4; i ++)
cout << myptr[i] << "
";
cout << endl;
return
0;
}
2.
Pointer parameters (the old C way) In the following program, make all of the changes 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). (8 points)
#include
<iostream>
#include
<cmath>
#include
<string>
using
namespace std;
void
compute(double x, double & fOfx, double & gOfx){
//
pre: x has been given a value
//
post: computes f(x) = 3x + 12 and g(x) =
12x + 3
fOfx = (3
* x) + 12;
gOfx = (12 * x) + 3;
}
int
main() {
double domainValue, f, g;
cout << "Enter the value for x:
";
cin >> domainValue;
compute(domainValue, f, g);
cout << f(x) = << f <<
and g(x) = << g << endl;
}
3. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in the drawing to indicate what must change in the linked list for each change described. (9 points)
(a) Add integer 20 (b) Add integer 60


(c) Add integer 10

4. Linked Lists: In the same manner as the previous drawings, draw a linked list that contains a single integer 5. (3 points)
5. Linked Lists: In the same manner as the previous drawings, draw an empty linked list. (3 points)
6. Linked Lists: Explain in
your own words why it might be better to use a linked list rather than
an array to hold a collection of objects.
(6 points)
7. Structures: Assume that the following code appears in your program.
struct employee { // defines employee "type"
int ID; //
employee id number
double hourlyRate; //
pay rate
char name[30]; //
employee's first name
double hours; //
hours this week
int weeks // number of weeks of vacation
};
employee employee1 = {123,
12.75, "Mary", 40, 1}; // first
employee
employee employee2 = {567, 10.75,
"John", 50, 2}; // second
employee
employee * empptr = &
employee1;
(a) Which of the following displays 12.75 on the screen? (Circle the letter for all that apply, if any.) (4 points)
a.
cout << employee1.hourlyRate << endl;
b.
cout << employee1->hourlyRate <<
endl;
c.
cout << empptr.hourlyRate << endl;
d.
cout << empptr->hourlyRate << endl;
(b) Write an if statement that determines which employee worked the most hours and displays his/her name on the screen. (Do not use literals, like Mary or John.) (5 points)
(c) Write a function called MoreVacation that takes two parameters, an employee pointer and an integer. The function is to add the integer to the number of weeks vacation that this employee has. It returns nothing. (8 points)
8. Command Line Arguments and Null-terminated Character Arrays: (30 points)
Write a complete program that computes the sum of all integers between two given positive integers. The positive integers must be given on the command line. If the two integers do not appear on the command line, or if something other than integers is entered on the command line, then terminate the program with an appropriate message. If the program were named sum.C, the following would be correct usage of the program:
sum.exe
1 3 displays 6 (that
is, 1 + 2 + 3)
sum.exe
10 20 displays 165
(Hint: Remember, the command line
arguments are null-terminated character arrays.
Check to make sure that each one contains only digits. Suggestion:
Write one function that checks one null-terminated character array and
then call it twice.)
You may use the following built in functions:
int
atoi(char * str); // include cstdlib
Converts a string (null-terminated character
array) containing an integer into that integer.
int
strlen(char * str); // include
cstdlib
Returns the number of characters in str.
int
isdigit(int c); // include
cctype
Returns true when c is a digit character 0 to 9
(An
additional blank page has been added for your use.)