Advertisement
Guest User

Untitled

a guest
May 24th, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. /*
  2.  
  3. 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).
  4.  
  5. a. Declare a pointer variable named fp that can point to a variable of type string.
  6. b. Declare fish to be a 5-element array of strings.
  7. c. Make the fp variable point to the last element of fish.
  8. d. Make the string pointed to by fp equal to "yellowtail", using the * operator.
  9. 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".
  10. f. Move the fp pointer back by three strings.
  11. 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".
  12. h. Without using the * operator, but using square brackets, set the string pointed to by fp to have the value "eel".
  13. 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.
  14. 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.
  15.  
  16. */
  17.  
  18. #include <iostream>
  19. #include <string>
  20. using namespace std;
  21.  
  22. int main() {
  23.  
  24.     string* fp; //PART A: done
  25.     string fish[5]; //PART B: done
  26.  
  27.     //initialize all elements to 0
  28.     for (int i = 0; i < 5; i++) {
  29.         fish[i] = "0";
  30.     }
  31.  
  32.     fp = &fish[4]; //PART C: done
  33.     *fp = "yellowtail"; //PART D: done
  34.  
  35.     //PART E:
  36.     /*I really have no idea how to go about this because I'm not allowed to use the fp pointer
  37.     or brackets. Maybe I'm misinterpreting the question but this really seems impossible.
  38.     Declaring another pointer seems like cheating but I don't know how else to do it?*/
  39.  
  40.     //fp = fp - 1;
  41.     //*fp = "salmon";
  42.  
  43.  
  44.     fp = fp - 3;//PART F: done
  45.  
  46.     //for (int i = 0; i < 5; i++) {
  47.     //  cout << *fp << endl;
  48.     //  fp = fp + 1;
  49.     //}
  50.  
  51.     // PART G and H
  52.     /*Similar to E, I have no idea how to go about this. */
  53.     fp = fp + 2;
  54.     *fp = "perch";
  55.  
  56.     // PART I and J
  57.     /*I don't know what the initialization expression means here. */
  58.  
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement