CS 23
Answer each of the following questions (each is worth 3 pts.):
lpr -Plp rational.cc rational.h
~kelliher/pub/cs23/hw3/ to the directory ~tom/hw3/ ? Your
current position within the file system is ~tom/ .
cp ~kelliher/pub/cs23/hw3/* hw3(Not the only way.)
g++ -g -o doom main.cc memory.cc io.cc
ctrl-z
fg
ls # Misses the dot files, but close enough.
Answer each of the following questions (each is worth 9 pts.):
char *makecopy(char *orig);The function should return the NULL pointer if a copy cannot be made.
char *makecopy(char *orig)
{
char *copy = new char[strlen(orig) + 1]; // Don't forget a position
// for the '\0' character.
if (copy == NULL)
return NULL;
strcpy(copy, orig);
return copy;
}
#include <iostream.h>
int main()
{
int *p;
int *q;
int array[6] = {63, 98, 52, 67, 78, 34};
p = q = array;
cout << (void*) array << endl; // Assume that 0x1000 is printed.
p = array;
*p = 6;
q = p + 3;
cout << (void*) q << endl;
cout << *q << endl;
++p;
*p = 9;
cout << q - p << endl;
for (p = array + 1; p < array + 5; ++p)
cout << (void*) p << "\t" << *p << endl;
return 0;
}
This is printed:
0x1000 0x100c 67 2 0x1004 9 0x1008 52 0x100c 67 0x1010 78
Answer each of the following questions:
A public member may be accessed by any function. A private member may be accessed only by a member function.
class Factorial
{
private:
int fact;
public:
Factorial(int n);
int Yield(void);
};
Factorial::Factorial(int n)
{
int i;
assert(n >= 0);
for (fact = 1, i = 2; i <= n; ++i)
fact *= i;
}
int Factorial::Yield(void)
{
return fact;
}
Write a recursive function which determines if a string is a palindrome. A palindrome is a string which reads the same way forwards as it does backwards (e.g., radar).
int palindrome(char *s)
{
return palinCheck(s, s + strlen(s) - 1);
}
int palinCheck(char *b, char *e)
{
if (b >= e)
return 1;
else if (*b == *e)
return palinCheck(b + 1, e - 1);
else
return 0;
}
Assume that you are given a List class with the standard list operations:
void QueueInsert(List &l, int i); int QueueRemove(List &l);QueueInsert() should insert i at the tail of the queue. QueueRemove() should remove and return the integer at the head of the queue. Your functions should properly handle error conditions.
void QueueInsert(List &l, int i)
{
assert(!l.full());
l.insert(i, l.size() + 1);
}
int QueueRemove(List &l)
{
int val;
assert(!l.empty());
val = l.retrieve(1);
l.delete(1);
return val;
}