Midterm_256 - CS256 Midterm (100 points) Fall 2008...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS256 Midterm (100 points) Fall 2008 Name____Suggested solution, for your reference only.________________ Note: This is a closed book test, however, you may use computers in CS Lab 8-52 – not your personal computers. You must take it in CS Lab 8-52, i.e. you cannot take it at home or other places. 1. (15 points) Enumeration type. (a) Define a enumeration type Month to establish relationship between the following: January 1 February 2 December 12 (b) Define an operator function + which will take two parameters: a Month m and an integer val , and will return a Month which is val months ahead. For example, if the month m is October, and the val is 5, the function should return March. enum Month {January = 1, February, March, April, May, June, July, August, September, October, November, December}; Month operator+ (Month m, int val) { int im; Month new; switch (m) { case January: im = 1; break; case February: im = 2; break; case December: im = 12; } im = (im+val)%12; if (im == 0) im = 12; switch(im) { case 1: new= January; break; case 2: new = February; break; case 12: new = December; break; } return new; }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2. (15 points) Is there anything wrong with the following program? If yes, try to correct it. If no, what is the output? (Note: this problem will be explained in the lecture. Solution omitted here.) (a) int a[5]={1,2,3,4,5}; int *ptr; ptr=*(&a+2); ptr=ptr+2; cout << *ptr << endl; (b) char *a = new char (‘A’); delete [] a; (c) char *str; strcpy (str, “hello”); 3. (15 points) Write two versions of
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/18/2008 for the course CS 256 taught by Professor Dr. yang during the Fall '08 term at Cal Poly Pomona.

Page1 / 6

Midterm_256 - CS256 Midterm (100 points) Fall 2008...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online