home *** CD-ROM | disk | FTP | other *** search
/ C++ for Dummies (3rd Edition) / C_FD.iso / WORKOUT / QUESTION.TXT < prev   
Text File  |  1996-09-06  |  10KB  |  276 lines

  1. 25-Minute Workout
  2. Part I: Questions
  3. Just to make sure that you were paying attention, I want to ask you a few questions. ôYou 
  4. vill answer!ö (The answers are on the disk. The answer files are named according to Part 
  5. number.)
  6. 1
  7. Declare the following. (In function declarations, assume that the functions take no 
  8. arguments.)
  9.         a. An array of ten integers
  10.         b. A pointer to an integer
  11.         c. An array of ten pointers to integers
  12.         d. A function returning a pointer to an integer
  13.           e. A pointer to a function that takes an integer and returns nothing
  14. 2
  15. Given that the function fn() takes an int followed by a float and returns an int, why will 
  16. the following not work?
  17. void anotherFn()
  18. {
  19.    int x;
  20.    x = fn(1, 2);
  21. }
  22. What can you do to make it work? Hint: DonÆt change the call in this prototypical 
  23. problem.
  24. 3
  25. WhatÆs wrong with the following displayArray() function?
  26. #include <stdio.h>
  27. void displayArray(int *pArray, int number)
  28. {
  29.    /*arrange in two columns*/
  30.    int i;
  31.    int noPairs = number / 2;
  32.    int odd = number % 2;
  33.    for (i = 0; i < (2 * noPairs);)
  34.    {
  35.       printf(ôarray[%2d] = %d,  array[%2d] = %d\nö,
  36.              i, pArray[i++], i, pArray[i++]);
  37.    }
  38.    if (odd)
  39.    {
  40.       printf(ôarray[%2d] = %d\nö, i, pArray[i]);
  41.    }
  42. }
  43.  
  44. int main()
  45. {
  46.    int array[] = {1, 2, 3, 4, 5};
  47.  
  48.    displayArray(array, 5);    /*may not generate proper 
  49. output*/
  50.    return 0;
  51. }
  52. 4
  53. Write and prototype a function that accepts from the keyboard two integer values, x and y, 
  54. and returns their values in arguments passed from the calling function.
  55. 5
  56. Assuming that you want to know the length in bytes of a particular structure, whatÆs 
  57. wrong with the following?
  58. /*function to return size of a MyStruct*/
  59. int sizeOfMyStruct()
  60. {
  61.    /*allocate an array of 2 MyStructs*/
  62.    struct MyStruct ms[2];
  63.  
  64.    /*now return the difference of the address of
  65.      the second element minus that of the first*/
  66.    return &ms[1] - &ms[0];
  67. }
  68. Part II: Questions
  69. Now weÆve come to my favorite part of every part, the workout. Slip on your leotards and 
  70. get out your keyboards. YouÆll have 25 minutes and no cheating.
  71. 1
  72.     a.    Given the following macro, list any potential problems you can find 
  73. with it:
  74. //TWO_MAX - return 2 * maximum of two numbers
  75. #define TWO_MAX (x, y) x > y ? (2 * x) : (2 * y)
  76.     b.    How can these problems best be avoided?
  77. 2
  78. C++ doesnÆt allow arguments other than the last to be given default values. Thus, the 
  79. following is wrong:
  80.  void aFunc(int x, float y = 0.0, char z) {
  81.    cout << ôx = ô   << x
  82.         << ô, y = ô << y
  83.         << ô, z = ô << z
  84.         << ô\nö;
  85. }
  86. Is there some way we can achieve the result we want in this example? (DonÆt overload 
  87. your brain thinking about this one.)
  88. 3
  89. Which of the following overloaded functions are called in each of the calls in main()? If 
  90. any of the calls are ambiguous (that is, they canÆt be resolved), list all candidates:
  91. void fn(int x) {}             //1
  92. void fn(char *x) {}           //2
  93. void fn(void *x) {}           //3
  94. void fn(int x, float y) {}    //4
  95. void fn(int x, double y) {}   //5
  96. int main()
  97. {
  98.    fn(1);                     //a
  99.    fn(ô1ö);                   //b
  100.    int i;
  101.    fn(&i);                    //c
  102.    fn(æ1Æ );                  //d
  103.    fn(1, 2);                  //e
  104.    return 0;
  105. }
  106. 4
  107. The following program compiles but does not link properly. Why?
  108. int printf(const char *, ...);
  109. int main()
  110. {
  111.    printf(ôHello world\nö);
  112.    return 0;
  113. }
  114. I mentioned the solution in the text, but donÆt go back and look until youÆve thought 
  115. about it.
  116. 5
  117. In Part I, you were asked to write and prototype a function that accepts two integer 
  118. values, x and y, from the keyboard and returns them in arguments passed from the calling 
  119. function. Do it again, but this time use referential arguments. Solve the problem twice, 
  120. the first time using scanf() and the second time using stream input.
  121. 6
  122. Let us assume the existence of a new mathematical class called Davis. (IÆll be 
  123. immortalized.) All the operations defined on real numbers are defined also on Davis. 
  124. Write the prototypes for a set of overloaded functions called multiply() that perform 
  125. multiplication between Davis numbers and Davis numbers as well as between Davis 
  126. numbers and floating-point numbers. (Note that a Davis times a Davis returns a Davis, 
  127. and a Davis times a float returns a Davis.)
  128. 7
  129. The first program that anyone writes in C is the following ôHello worldö program:
  130. #include <stdio.h>
  131. int main()
  132. {
  133.    printf(ôHello, world\nö);
  134.    return 0;
  135. }
  136. Rewrite this program to use stream output.
  137. Part III: Questions
  138. This part has only one exercise. The time has come to sink or swim. Write a program that 
  139. does the following.
  140. Model a set of courses and students. Students have names. Students can enroll in courses. 
  141. Students are given a grade (between 0 and 4) in each class. Courses can have different 
  142. numbers of semester hours.
  143. After all the students have been graded, the program should then spit out the following:
  144.     *    The average grade in each course
  145.     *    Each studentÆs grade in each course
  146.     *    The studentÆs average grade for all classes
  147.     *    The studentÆs grade in each course curved by the average of all grades 
  148. in the class
  149. Write your solution in C++. (I didnÆt really need to say that, did I?)
  150. Here are a few tips for solving this problem. One, use an object-oriented approach. 
  151. Protect your data members as much as possible.
  152. Two, study the requirements to find the candidate classes. Then examine the output 
  153. requirements to determine the member functions you will need. Make CRC cards if it 
  154. helps.
  155. Three, use the following simple curving formula:
  156. curved grade = 3.0F * (actual grade / average)
  157. This makes the average grade 3.0. Make sure, however, that the grade of any single 
  158. student does not exceed 4.0.
  159. Four, concentrate on what classes you need to solve this problem. To keep the program as 
  160. short as possible, you can hardcode the actual course objects and student objects that you 
  161. will need to test your classes. Just make sure that you provide enough courses and 
  162. students to show that the classes work.
  163. Part IV: Questions
  164. Once again, itÆs time to start sweating. Get out your keyboards and put away your notes. 
  165. You have 25 minutes @md and no cheating.
  166. 1
  167.     a.    Write a constructor for the class Student in the program you wrote for 
  168. the Part III Workout. (You did write that program, didnÆt you?) This 
  169. constructor should not make any restrictions on the length of the name. 
  170. Hint: Use new.
  171.     b.    Consider the following class:
  172. class Motor
  173. {
  174.    public:
  175.      Motor(int noCylinders, float displacement);
  176. };
  177. class Car
  178. {
  179.    public:
  180.  
  181.    //this constructor is not complete
  182.    Car(int color, float displacement,
  183.        int noDoors = 4, int noCylinders = 6)
  184.     {
  185.       //stuff goes here
  186.     }
  187.    protected:
  188.     Motor motor;
  189.     int   paintColor;
  190.     int   numberDoors;
  191. };
  192.         Complete the constructor for Car. DonÆt forget to initialize the member 
  193. motor.
  194.     c.    In the following declaration of student, what constructor call is invoked 
  195. on which members?
  196. Student otherStudent(ôDannyö);
  197. Student student[5] = {ôRandyö, otherStudent, ôTrudyö};
  198.     d.    What does the following do?
  199. class Student
  200. {
  201.    public:
  202.      Student(char *pName);
  203.      Student()
  204.      {
  205.         Student("no name");
  206.      }
  207. };
  208.     e.    Assuming that the intent in 1d was to avoid having two constructors, 
  209. how could you do this correctly? There are several ways to do this.
  210.  
  211. 2
  212.     a.    Why is the constructor Student::Student(Student) illegal? Hint: In older 
  213. versions of the compiler in which the constructor is not illegal, 
  214. invoking this constructor is always fatal.
  215.     b.    Why was the copy constructor in BUDGET4 declared protected? Hint: 
  216. What asset is it trying to protect?
  217. 3
  218.     a.    WhatÆs wrong with the following count() function? Hint: This is a 
  219. subtle problem.
  220. class Student
  221. {
  222.    public:
  223.     static int count;
  224. };
  225. int Student::count;
  226.  
  227. //count - count how many students between first and last
  228. void count(Student *pFirst, Student *pLast)
  229. {
  230.    Student *pS = pFirst;
  231.    pS->count = 0;        //start with 0
  232.