Lab 16

  1. Consider the following C++ program.
  2. #include <iostream> using namespace std; int fun(int &x, int y) { if (y <= 0) return x; x = x + 1; y = y + 1; cout << x << y << endl; return x * y; } int main() { int x = 5, y = -1; cout << fun(x, y) << endl; // line a fun(x, 1); // line b fun(y, 1); // line c fun(y, x); // line d cout << fun(x, 2) << endl; // line e return 0; }

    What is the output from the program at each of the following lines:
  3. Consider the following C++ program.
  4. #include <iostream> using namespace std; int fun(int &x, int &y) { if (y <= 0) return x; x = x + 2; cout << x << y << endl; return x * y; } int main() { int x = 2, y = 0; cout << fun(x, y) << endl; // line a fun(y, x); // line b fun(x, y); // line c fun(y, x); // line d cout << fun(x, y) << endl; // line e return 0; }

    What is the output from the program at each of the following lines:
  5. Write a complete C++ program using the main function given below. Write a function called sort that sorts three integer parameters into decreasing order. For example, a program that uses the function sort follows.
  6. int main() { int a = 2, b = 7, c = 1; sort(a, b, c); cout << a << b << c << endl; // prints 721 return 0; }
  7. Write a complete C++ program that converts Eastern time to a different time zone.
  8. int main() { int hour, minutes; string zone; cout << "What is the current hour? "; cin >> hour; cout << "What is the current minutes? "; cin >> minutes; cout << "Enter time zone (Pacific, Mountain, Central): "; cin >> zone; timeZoneConvert(hour, zone); cout << "The current time in " << zone << " is " << hour << ":" << minutes << endl; return 0; }

    Sample run of program:
    What is the current hour? 1 What is the current minutes? 56 Enter time zone (Pacific, Mountain, Central): Pacific The current time in Pacific is 22:56

    Another sample run of program:
    What is the current hour? 11 What is the current minutes? 37 Enter time zone (Pacific, Mountain, Central): Mountain The current time in Mountain is 9:37

    Another sample run of program:
    What is the current hour? 14 What is the current minutes? 25 Enter time zone (Pacific, Mountain, Central): Central The current time in Central is 13:25
  9. Write a complete C++ program using the main function given below. In this program, write a function called digitMatch. The function has three integer parameters: first, second, and third that are positive and have the same number of digits. It returns the number of positions where their digits match. For example, if teh function was applied to 17345, 97813 and 17313 it would return one because only the second digits match (the 7 that is second from the left).
  10. You can assume that the user gives you inputs matching the input requirements.
    int main() { cout << digitMatch(168, 567, 767) << endl; // prints 1 cout << digitMatch(143, 243, 343) << endl; // prints 2 return 0; }