// solution to lab8 problem 2 #include // write a function f that takes an integer x and return an integer // representing 3*x + 1. int f(int x) { return 3*x + 1; } // write a function s that takes an integer x and returns no value. // the function's code should first change the value of x to x+1, then // print out the new value of x. e.g., s(3) should print 4. void s(int x) { x = x+1; cout << "local x is now " << x << "\n"; } // write a function g that takes an integer n and a character. The // character is expected to be 'd' or 's'. If it's 'd', return 2*n, // otherwise, if it's 's' return n*n. If it's a letter that's not 'd' or // 's', return 0. int g(int n, char c) { if ( c == 'd') return 2*n; if (c == 's') return n*n; else return 0; // end outer else } // write a function allthree that takes in three 'bool' parameters and returns // true if all three boolean values are true, and returns false otherwise. // For example, allthree(3<4, 5==2+3, 6>1) should return true. bool allthree(bool a, bool b, bool c) { return a && b && c; } // complete the following main function: int main() { // declare variables x, and y, and assign them 3 and 4 respectively int x; int y; x = 3; y = 4; // declare a bool variable b; bool b; // assign y to the result of f applied to x; y = f(x); // call function s on x; -- note this is a statement, since s returns void s(x); // print out the value of x. Why did it print 3? Wasn't it changed to // 4 inside the function s?! You need to understand what's happening // here (local variables). cout << "x in main is still " << x << "\n"; // assign to x the result of calling g on y and 's'. x = g(y,'s'); // call the allthree function on the following boolean test values: // x is greater than 2 // y is greater x // y is less than 15 // assign the value returned by the function call to b. b = allthree( (x>2), (y>x), (y<15) ); // Print out the final values of x, y, and b, indicating which is which. cout << "x==" << x << " y==" << y << " b==" << b << "\n"; return 0; }