Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- For each of the following parts, write a single C++ statement that performs the indicated task. For each part, assume that all previous statements have been executed (e.g., when doing part e, assume the statements you wrote for parts a through d have been executed).
- a. Declare a pointer variable named fp that can point to a variable of type string.
- b. Declare fish to be a 5-element array of strings.
- c. Make the fp variable point to the last element of fish.
- d. Make the string pointed to by fp equal to "yellowtail", using the * operator.
- e. Without using the fp pointer, and without using square brackets, set the fourth element (i.e., the one at position 3) of the fish array to have the value "salmon".
- f. Move the fp pointer back by three strings.
- g. Using square brackets, but without using the name fish, set the third element (i.e., the one at position 2) of the fish array to have the value "perch".
- h. Without using the * operator, but using square brackets, set the string pointed to by fp to have the value "eel".
- i. Using the == operator in the initialization expression, declare a bool variable named d and initialize it with an expression that evaluates to true if fp points to the string at the start of the fish array, and to false otherwise.
- j. Using the * operator in the initialization expression, but no square brackets, declare a bool variable named b and initialize it to true if the string pointed to by fp is equal to the string immediately following the string pointed to by fp, and false otherwise.
- */
- #include <iostream>
- #include <string>
- using namespace std;
- int main() {
- string* fp; //PART A: done
- string fish[5]; //PART B: done
- //initialize all elements to 0
- for (int i = 0; i < 5; i++) {
- fish[i] = "0";
- }
- fp = &fish[4]; //PART C: done
- *fp = "yellowtail"; //PART D: done
- //PART E:
- /*I really have no idea how to go about this because I'm not allowed to use the fp pointer
- or brackets. Maybe I'm misinterpreting the question but this really seems impossible.
- Declaring another pointer seems like cheating but I don't know how else to do it?*/
- //fp = fp - 1;
- //*fp = "salmon";
- fp = fp - 3;//PART F: done
- //for (int i = 0; i < 5; i++) {
- // cout << *fp << endl;
- // fp = fp + 1;
- //}
- // PART G and H
- /*Similar to E, I have no idea how to go about this. */
- fp = fp + 2;
- *fp = "perch";
- // PART I and J
- /*I don't know what the initialization expression means here. */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement