CS18
typedef int array[20]; array d;
typedef struct Complex { double re, im; } complex; complex x;
double data[] = { 1.0, 2.0, 3.0, 4.0 };Assume that a double occupies four bytes and that data is allocated starting at address 1000 in memory.
cout << data;1000
double* p = data + 1; p += 2; cout << *p;4.0
double* p = data; p += 2; cout << p;1008
const int LIST_MAX = 100; class List { private: int l[LIST_MAX]; // Holds the items of the list. int size; // Number of items in the list. public: List(void) : size(0) {} void ListRetrieve(int index, int& value, int* success); int ListLength(void); int ListIsEmpty(void); // ... };
Implement ListRetrieve() (read the prototype carefully),
ListLength(), and
ListIsEmpty().
void List::ListRetrieve(int index, int& value, int *success) { if (0 <= index && index < size) { value = l[index]; *success = 1 } else *success = 0; } int List::ListLength(void) { return size; } int List::ListIsEmpty(void) { return size == 0; }
struct point { double x; double y; };Show the class declaration and the definition of the class' constructors (one for creating a rectangle and one for creating a circle) and a public method which computes and returns the area of an object.
enum ObjectType { CIRCLE, RECTANGLE }; class Object { private: ObjectType type; point ll; point ur; point c; // Center or circle. I could have used one of the other // two to save some space. double radius; public: // Circle constructor. Object(double x, double y, double rad); // Rectangle constructor. Object(double llx, double lly, double urx, double ury); double Area(void); }; Object::Object(double x, double y, double rad) { type = CIRCLE; c.x = x; c.y = y; radius = rad; } Object::Object(double llx, double lly, double urx, double ury) { type = RECTANGLE; ll.x = llx; ll.y = lly; ur.x = urx; ur.y = ury; } double Object::Area(void) { switch (type) { case RECTANGLE: return (ur.x - ll.x) * (ur.y - ll.y); break; case CIRCLE: return PI * radius * radius; break; default: // This is a major fault. Terminate. exit(1); break; } }