CPS 188 DECLARATIVE, CPS 188 RECURSIONS AND CPS 188 ARRAY
POINTERS EXAM WITH COMPLETE SOLUTIONS!!
CPS 188 DECLARATIVE
Explain pointers using the bookshelf analogy
Use the analogy of a bookshelf when referring to computer memory. Each book on the
shelf (data or variable) has its own unique location, as seen by the labels on the shelves.
This unique location is known as the memory address, and it helps find the data you are
looking for. In the case you wanted to remember where a specific book was but you
couldn't remember the name, you could remember the label on the bookshelf instead-
the location. In programming, the label is like a pointer, you can find and interact with
the data at that unique memory address, like using the shelf label to find out who the
author is, what genre it belongs to, etc.
How would I use the swap function to swap two pointers?
void swap(int a, int b) {
int temp = *a;
a = b;
*b = temp;
}
What is wrong with this code? What would I add to fix it? Why?
void swap(int a, int b) {
a = b;
b = a;
}
void swapCorrect(int a, int b) {
, int temp = *a;
a = b;
*b = temp;
}
If you try to simply use a = b, b = a, then you would lose the original value of a before you
could even swap the values. Using a temporary variable allows you to hold the original
value of a while you swap
What is wrong with this code?
void ascendingOrder(int a, int b, int *c) {
if (a > b) {
swap(a, b);
}
if (b > c) {
swap(b, c);
}
if (a > c) {
swap(a, c);
}
}
Error in the swap function. In the third if statement, the correct code should be
if (a > b) {
swap(a, b);
}
What is wrong with this code?
POINTERS EXAM WITH COMPLETE SOLUTIONS!!
CPS 188 DECLARATIVE
Explain pointers using the bookshelf analogy
Use the analogy of a bookshelf when referring to computer memory. Each book on the
shelf (data or variable) has its own unique location, as seen by the labels on the shelves.
This unique location is known as the memory address, and it helps find the data you are
looking for. In the case you wanted to remember where a specific book was but you
couldn't remember the name, you could remember the label on the bookshelf instead-
the location. In programming, the label is like a pointer, you can find and interact with
the data at that unique memory address, like using the shelf label to find out who the
author is, what genre it belongs to, etc.
How would I use the swap function to swap two pointers?
void swap(int a, int b) {
int temp = *a;
a = b;
*b = temp;
}
What is wrong with this code? What would I add to fix it? Why?
void swap(int a, int b) {
a = b;
b = a;
}
void swapCorrect(int a, int b) {
, int temp = *a;
a = b;
*b = temp;
}
If you try to simply use a = b, b = a, then you would lose the original value of a before you
could even swap the values. Using a temporary variable allows you to hold the original
value of a while you swap
What is wrong with this code?
void ascendingOrder(int a, int b, int *c) {
if (a > b) {
swap(a, b);
}
if (b > c) {
swap(b, c);
}
if (a > c) {
swap(a, c);
}
}
Error in the swap function. In the third if statement, the correct code should be
if (a > b) {
swap(a, b);
}
What is wrong with this code?