Corrections, clarifications, and other announcements regarding this programming assignment will be found below.
The goals of this assignment are to gain experience:
This homework assignment must be done individually. Collaboration on the homework assignment is not allowed.
Note: You are allowed to double-check your answers by writing, compiling, and running C++ code. However, for the the questions that ask you to "Explain your results" you need to explain your reasoning to demonstrate your understanding of why the result is what it is.
for (int i = 0; i < 8; i++) *(arr + i) = *arr + i;
int *p, a[5] = {1, 2, 3, 4, 5}; p = a; p++; p[2] = 0;
struct DataItem { int id; StockItem *itemPtr; };
Write a function named switchIt that takes two parameters of type DataItem and swaps the itemPtr fields of the two parameters. Note: your function should only swap pointers (i.e., you are swapping StockItem *s not StockItems).
struct Color { int red; int blue; int green; };
Complete the following setColor function. This function takes an array of pointers to Colors and initializes the array at the given index to be a new Color with the given red, blue, and green values. You will need to use a 'new' command, but you do not need to worry about the corresponding 'delete' command.
void setColor(Color **arr, int index, int red, int blue, int green) {
struct Node { // represents an individual node in the chain char value; Node *next; // pointer to the next node in the chain }; Node *head; // pointer to the first node in the chain
and that head is pointing to a chain of nodes containing 'a', 'b', 'c', 'd' (in that order). Write a code fragment that moves the node containing 'c' to the beginning of the chain. That is, initially we have
+---+ +---+---+ +---+---+ +---+---+ +---+---+ head | --+--> |'a'| --+--> |'b'| --+--> |'c'| --+--> |'d'| \ | +---+ +---+---+ +---+---+ +---+---+ +---+---+
and after the code fragment executes, we have
+---+ +---+---+ +---+---+ +---+---+ +---+---+ head | --+--> |'c'| --+--> |'a'| --+--> |'b'| --+--> |'d'| \ | +---+ +---+---+ +---+---+ +---+---+ +---+---+
Make sure your code moves the node itself (and not just the value within the node).
void scramble(int *x, int &y, int * &z) { *x = y; x = z; *x = 2; z = new int(4); y = 6; } void testIt() { int a = 1; int *b = new int(3); int *c = new int(5); int **d = &b; scramble(&a, *c, *d); cout << a << " " << *b << " " << *c << " " << **d << endl; }
What is printed when the testIt() function is executed? Explain your results. Hint: use a memory diagram to understand the code's execution (though it isn't required for your submission).