Name  ___________________

 

CPTR246  Spring '04 (100 total points)                                                                 Exam 2

 

1.      File I/O and Command-line Arguments  Modify the program below to (1) take its input from a file rather than cin and (2) get the name of the file from the command line.  If the filename is not on the command line, display an error message and end the program.  (16 points)

 

#include <iostream>

using namespace std;

const double PI = 3.14159;

 

 

int main() {

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

   double r;

 

   cout << "Enter the circle’s radius: (ctrl-d to end): ";

 

   cin >> r;

 

   while (!cin.eof()){

 

      cout << "For radius “ << r << “ the area is " << PI * r * r << endl;

 

      cout << "Enter the circle’s radius: (ctrl-d to end): ";

 

      cin >> r;

 

   }

 

   return 0;

 

}


2.      Pointer parameters (the old C way)  In the following program that computes the circumference and area of a circle, 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).  (10 points)

 

#include <iostream>

 

using namespace std;

 

const double PI = 3.14159;

 

void compute(double radius, double & circumference, double & area){

// pre:     radius is a positive real number

// post:  area and circumference are computed

 

      circumference = 2 * PI * radius;

 

      area = PI * radius * radius;

 

}

 

 

int main() {

 

      double r, circum, area;

 

      cout << "Enter the circle’s radius: " << endl;

 

      cin >> r;

 

compute(r, circum, area);

 

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

 

      cout << "The circumference is " << circum << endl;

 

      return 0;

 

}


3.      Pointer Basics  To the right of the following code, write what would be displayed on the screen. (26 points)

 

#include <iostream>

 

using namespace std;

 

int main() {

   int x = 10;

   int y = 20;

   int values[4] = {100, 200, 300, 400};

   int * oneptr;

   int * twoptr;

 

   oneptr = &x;

   twoptr = &y;

 

   cout << *oneptr << “ “ << *twoptr << endl;

 

   *twoptr = 10;

   if (*oneptr == *twoptr)

      cout << “Test1 is true” << endl;

   else

      cout << “Test1 is false” << endl;

 

   x = 5;

   *twoptr = 50;

   cout << x << “ “ << y << endl;

   cout << *oneptr << “ “ << *twoptr << endl;

 

   cout << *(values + 1) << “ “ << (*values + 1) << endl;

  

   oneptr = values;

   oneptr[0] = oneptr[1];

   values[1] = 250;

   *(values + 2) = 0;

   for (int i = 0; i < 4; i++)

      cout << values[i] << endl;

 

   return 0;

}

 

 


4.      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.  (12 points)

 

(a)    Remove integer 58                                                                   (b) Add integer 40

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


(c)  Add integer 5                                                                       (d) Remove integer 16

                                                   

 

 

 

 

 

 



5.      Linked Lists:  Explain in your own words how linked lists use memory more efficiently than arrays.  (4 points)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

6.      Structures:  Assume that the following code appears in your program. 

 

struct studentSchedule{

      int socSecNbr;          // unique student ID number

      string name;            // name of the student

      int numberOfCourses;    // how many courses they have this semester

      string course[6];       // the course codes

};

studentSchedule Oscar = {123456789, “Oscar”, 4, “CPTR246”, “MATH128”,

                              “SOC110”, “HIST128”};  

 

(a)    Which of the following statements displays the number of courses that Oscar is scheduled for? (4 points)

 

a.       cout << Oscar << numberOfCourses << endl;

b.      cout << Oscar.numberOfCourses << endl;

c.       cout << Oscar->numberOfCourses << endl;

d.      cout << Oscar[numberOfCourses] << endl;

 

 

(b)  Which of the following for loop displays the courses that Oscar is taking? (4 points)

 

a.       for (int i = 0; i < 6; i++)

cout << course[i].Oscar << endl;

b.      for (int i = 0; i < numberOfCourses; i++)

cout << studentSchedule.course[i] << endl;

c.       for (int i = 0; i < numberOfCourses; i++)

cout << Oscar.course[i] << endl;

d.      for (int i = 0; i < numberOfCourses; i++)

cout << Oscar[i].course << endl;

 

 

(c) Write a (free) function called AddCourse that takes two parameters, a pointer to a studentSchedule object and a string that contains the name of a course that the student wants to add.  If the student has 6 courses already, return false since there is no room for any more courses.  Otherwise, add the course to the array, update the numberOfCourses appropriately, and return true.  Therefore, the function returns a boolean.  (10 points)


7.      Null-terminated Character Arrays:

 

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

 

char str1[20] = “Hello world”;

char str2[20];

            char combo[40];

cout << "Enter your greeting: ";

cin.getline(str2, 20);

 

Using the string functions described on the following page, write C++ code that will perform each of the following tasks. 

 

·        Using strcmp, write code that will display “Duplicate greetings” on the screen if str2 is equivalent to str1.  (4 points)

 

 

 

 

 

 

 

 

·        Using strcpy and strcat, write code that result in the string combo having the contents of str1 followed by the contents of str2 without changing the contents of either str1 or str2. (4 points)

 

 

 

 

 

 

 

 

·        Complete the following function that takes a string (as a null-terminated character array) and a character as a parameter.  It returns the number of times the character appears in the string.  For example, the function call countChar(“Hello world”, ‘l’) would return a 3. (6 points)

 

int countChar(char * str, char x){

 

 

 

 

 

 

 

 

 

 

 

 

}