exam3-review - Exam3Review Problem1 Solution1...

Info icon This preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
    Exam 3 Review
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    Problem 1 Write a program that prints the tokens given on  the command line in reverse order.
Image of page 2
    Solution 1 int main (int argc, char **argv) { for (int i = argc -1; i >= 0; i--) cout << argv[i] << “ “; }
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    Problem 2 The class Point has the following contructor and  public function: Point(double, double) int distance_to_origin() Write a code fragment that creates an object of  type Point representing (-1.0, 2.5) and prints the  length of the line drawn from (-1.0, 2.5) to the  origin.
Image of page 4
    Solution 2 Point p1(-1.0, 2.5); int dist = p1.distance_to_origin(); cout << dist << endl; OR Point p1(-1.0, 2.5); cout << p1.distance_to_origin() << endl;
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
    Problem 3 Write a function called find_max that has a single  integer parameter.   The function reads integers from the keyboard until  EOT (ctrl+d) is entered. If at least one positive integer is read, the function sets  the input parameter equal to the maximum positive  integer read and returns true, otherwise sets the input  parameter to zero and returns false. Let us assume the user enters only integers and  whitespace.
Image of page 6
    Solution 3 bool find_max(int &max) { int cur; bool found_int = false; max = 0; while (cin >> cur) { found_int = true; if (cur > max) max = cur; } return found_int; }
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern