void fourteen() { int* s; int* p = new int; int* q = p; int* r = new int; *p = 8; s = p; *q = 25; *r = *s; q = r; cout << "*p = " << *p << endl; cout << "*q = " << *q << endl; cout << "*r = " << *r << endl; cout << "*s = " << *s << endl; }
*p = 25
*q = 25
*r = 25
*s = 25
Remark. It is very important for this kind of problem
not to cut corners. Draw a picture!
Show the pointers. Do not try
to avoid using pointers. Do not be sloppy.
If you cut corners or are sloppy, you will get the wrong answer.
Some students will ignore this advice. They will get the
wrong answer.
char* copy(char *s)
{
char* cp = new char[strlen(s) + 1];
strcpy(cp, s);
return cp;
}
Here are two solutions, one using recursion and the other using a loop.
Note that allPositive([]) should return 1, since there are no
non-positive members of [].
bool allPositive(List L)
{
if(L == emptyList) return 1;
else if(head(L) <= 0) return 0;
else return allPositive(tail(L));
}
bool allPositive(List L)
{
List p = L;
while(p != emptyList) {
if(head(p) <= 0) return 0;
p = tail(p);
}
return 1;
}