exam3-review

# exam3-review - Exam3Review Problem1 Solution1...

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

Exam 3 Review

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

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

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

View Full Document
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.
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;

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

View Full Document
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.
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; }

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern