Exam #2 CISC1110, MW 10:35-12:40pm Fall 2011 Name 1 Evaluate each expression according to C++ rules (8 pts) Given: Integers a = 3, b = 2, c = 5, and float d = 40 a a * c - 10 = b a % b + (a * d) + 7 = c b / 4 + (a - c) = d b / (a + c) * d = 2 Convert to Base 10 (decimal) (4 pt) a 10101001 (Binary) b DE (Hex) 3 Use the segment of code below to answer the questions (8 pts) float cube(float x) { return pow(x, 3); a What is the name of the function? b How many parameters does it take? c Does the function return a value? If yes then what type of value? d Write an example statement using the function to cube the number 4
Use the code below to answer Question #4 void funcret1(int); void funcret2(int, int); void funcret3(); int radius, height, one; int main() { int one, two, three, b; string lastname; return 0; double quantity; void funcret1(int num1) { double accountnumber; int limit = 10; void funcret2(int x, int y) { string firstname; static int count = 1; while (count < limit) { int die1; char roll; string firststring; void funcret3() { int a;
4 Fill in the scope (accessibility) of each identifier in the table below (15 pts) Identifier Accessible In main() Accessible In funcret1(int) Accessible In funcret2(int, int) Accessible In funcret3() radius Y Y Y Y height one two b firstname lastname num1 accountnumber x y roll count limit die1 a firststring 5 Write the header and body for the following function (10 pts) // Parameters: start and end time in military format, ie 14:30 is 2:30pm // Returns: the number of minutes that has elapsed from the start to the // end time in hours and minutes // For example minutes(13, 20, 14, 45) would return 1 hour 25 minutes void minutes(int hr1, int min1, int hr2, int min2, int& hours, int& minutes)
6 Show exactly what will be printed out by the following program (15 pts) #include <iostream> using namespace std; void foo(int, int, char); void bar(int&, int, char&); int baz(int&); int main() { int x = 1, y = 7; char c = 'C'; cout << "1: x " << x << ", y " << y << ", c " << c << ", d "; cout << baz(++x) << endl; foo(x, y, c); cout << "2: x " << x << ", y " << y << ", c " << c << ", d "; cout << baz(x++) << endl; bar(x, y, c); cout << "3: x " << x << ", y " << y << ", c " << c << endl; return 0; // end of main void foo(int a, int b, char c) { a *= 3; b = a * 2; c = 'B' + a; cout << "foo: a " << a << ", b " << b << ", c " << c << endl; void bar(int& x, int y, char& z) { x++; y = y % 2; z = 'G'; cout << "bar: x " << x << ", y " << y << ", z " << z << endl; int baz(int a&) { return a + 5;
7 Write the definition for each function (include the header) Name all function foo (20 pts) a Write the definition of a function that takes as input a decimal number and returns the decimal portion of the number only For example, given 3278, the function would return 078 b Write the definition of a void function that takes as input two decimal numbers If the first number is not zero, it outputs the second number divided by the first number; otherwise, it outputs a message indicating that the second number cannot be divided by the first number because the first number is 0 c Write the definition of a boolean function with two parameters of type int The function should return true if the first integer is divisible by the second integer d Write the definition of a void function that takes as input two parameters of type int, for example sum and x The function updates the value of sum by adding the value of x to sum Use a reference variable to pass the value of sum to the calling function
8 Write a complete C++ program to do following (20 pts) The following formula gives the distance between two points, (x1, y1) and (x2, y2) in the Cartesian plane: Given the center and a point on the circle, you can use this formula to find the radius of the circle Write a program that prompts the user to enter the center and a point on the circle The program should then output the circle's radius, diameter, circumference and area Your program must have and utilize the following functions: // Parameters: 4 integers representing 2 points (x1, y1) (x2, y2) // Returns: distance between two points float distance(int x1, int y1, int x2, int y2); // Parameters: 4 integers representing 2 points (x1, y1) (x2, y2) // Returns: calls the distance function and returns the radius of the // circle float radius(int x1, int y1, int x2, int y2); // Parameters: 1 float, the radius of a circle // Returns: Circumference of a circle float circumference(float radius); // Parameters: 1 float, the radius of a circle // Returns: Area of a circle float area(float radius); Assume pi = 314