Name ______________________________
CPTR246
Spring '08 (100 total
points)
Exam 2
1. Structures: Assume that the following code appears in your program.
struct student { // defines student "type"
int ID; // student id number
double GPA; // grade
point average
char name[40]; // student’s name
string courses[4]; //
four courses scheduled
char email[30]; // email address
};
student Oscar = {567, 2.01, “Oscar
Mayer”, “CPTR000”,
“BSKT100”,
“GOOF101”, “FUN110”,
“oscar@yahoo.com” };
student * studentptr = & Oscar;
(a) For each of the following, indicate if the reference is a syntax error or not. If not, give the value of the reference? (8 points)
Reference Error? (yes or no) If no, give its value
Oscar.ID __________________
________________________
Oscar->ID __________________
________________________
studentptr.GPA __________________
________________________
studentptr->GPA __________________
________________________
(b) Using the definitions above, write a for loop that displays the courses that Oscar is scheduled for, one per line. (Do not use any string literals.) (8 points)
2. Null-terminated Character Arrays:
Use pages 453-454 of your textbook and the cctype functions at the bottom of the ASCII chart handout (which can also be found on page 1025-1026) as needed.
(a) To the right of the following code, write what would be displayed on the screen. (10 points)
#include <iostream>
using namespace std;
int main()
{
char s1[20] =
"cat";
char s2[20] =
"dog";
char s3[20] =
"cow";
char s4[20] =
"horse";
char s5[20] =
"pigeon";
char s6[20] =
"eagle";
strcpy(s1, s2);
cout << s1
<< endl;
cout << s2
<< endl;
cout <<
strcat(s3, s4) << endl;
cout << s3
<< endl;
strncpy(s5, s6, 3);
cout << s5
<< endl;
return 0;
}
(b) Circle T or F to indicate whether each of the following evaluates to true (non-zero) or false (zero), respectively. (2 points each)
char name [20]= “Oscar Mayer”;
char message[30] = “The cost is
$200.00.”
T F strlen(name) == 20
T F isalpha(name[0])
T F isdigit(message[16])
T F strcmp(name, “Oscar Mayer”)
3. Pointer Basics
(a) What is a pointer? (3 points)
(b) To the right of the following code, write what would be displayed on the screen. (24 points)
#include
<iostream>
using namespace std;
int main() {
int x = 78;
int y = 99;
int * oneptr;
int * anotherptr;
int tests[5] = {88,
93, 78, 99, 90};
oneptr = & x;
anotherptr = &
y;
cout << * oneptr
<< " " << * anotherptr << endl;
x = 93;
*oneptr = 88;
cout << * oneptr
<< " " << * anotherptr << endl;
cout << x
<< " " << y << endl;
anotherptr =
oneptr;
cout << * oneptr
<< " " << * anotherptr << endl;
cout << x
<< " " << y << endl;
cout << *tests
<< endl;
cout <<
*tests + 1 << endl;
oneptr = tests;
*(oneptr + 1) = 90;
for (int i = 0; i
< 5; i ++)
cout << oneptr[i]
<< " ";
cout <<
endl;
return 0;
}
4.
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 “rectangle” 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 rectangle(double l, double w, double & area,
double & perimeter){
//
pre: l and w have been given values
//
post: area and perimeter of the
rectangle are computed
area = l * w;
perimeter = 2 * (l +
w);
}
int main() {
double length,
width, area, perimeter;
cout <<
"Enter the length and width of your rectangle: ";
cin >> length
>> width;
rectangle( length,
width, area, perimeter);
cout << “area
= “ << area << “ and perimeter) = “ << perimeter <<
endl;
}
5. You and a classmate are looking over the shoulder of an upperclassman who is testing a program called widget.exe. At the UNIX prompt you see that she types the following:
widget.exe < file1.dat
Your classmate doesn’t understand what the “< file1.dat” is for or how it works. Explain it to him. (5 points)
6. Formatting output: Write cout statements to display the following as described.
int age = 21;
double cash = 12.0;
string name = “Oscar
Mayer”;
Print a line that has age right justified in a column that is 5 characters wide. (2 points)
On one line, print name left-justified in a column of width 30 followed by cash displayed with 2 decimal positions, right-justified in a column of width 8. (4 points)
7. Command Line Arguments and File Processing: (20 points)
Write a program that takes a file of real numbers as input, reads each real number, doubles it, and then writes it to an output file (one number per line). Example runs:
./doubler.exe
input.dat output.dat
./doubler.exe
myfile.dat yourfile.dat
Important points:
· The names of the files MUST come from the command line. If they are NOT there, end the program with an appropriate error message.
· You may assume that the file contains only real numbers.
· Do NOT open the output file if the input file did not open properly.
· You do NOT need to have more than the main function.
· You do NOT need to include any comments.
(An
additional blank page has been added for your use.)