{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

finalF98sol

# finalF98sol - EGN 3210 Sample Test 2 NAME#1 Solutions S.S...

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

EGN 3210 Sample Test 2 Dr. Fernando Gonzalez NAME Solutions S.S.# #1. (Functions) Write a function that receives 3 integers, and returns the value of the largest one through the function name. int largest( int a, int b, int c) { int max; max = a; if (b > max) max = b; if (c > max) max = c; return max; } #2. (Functions) What is the output of this segment of code. int z = 0; Ans. void bill (int c) X = 5, Y = 10, Z = 15 { int x = 0; X = 7, Y = 24, Z = 36 static int y = 0; x = x + c; X = 36, Y = 96, Z = 144 y = y + 2 * c; z = z + 3 * c; printf (“ x = %d, y = %d, z = %d \n”,x,y,z); } main() { int x = 5; bill (x); <-- x = 5 x = x + 2; bill (x); <-- x = 7 bill (z); <-- z = 36 }

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

View Full Document
#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1. int f( int x) { if (x < 2) return x; else return f(x - 1) + f(x - 2); } #4. (Arrays) Write a loop to double the value of each component of an array of integers called A. Stop at the component containing 0. Ex. before: A = {2,5,7,0,3,5} after A = {4,10,14,0,3,5}. Declare the array A and any variable needed. i = 0; while (A[i] != 0) { a[i] = A[i] * 2; i++; }
#5. (Structures and functions) Declare a structure called point that stores 2 integers, x and y. Declare another structure called segment that stores 2 points, p1 and p2. Finally write a function that given a segment and a point determines if the point is on the segment. The function returns true if the point is on the segment and false otherwise. A point is on a segment if the distance from one end of the segment, p1, to the other end, p2, is the same as the distance from p1 to the point plus the distance from the point to p2. struct point { int x; int y; } struct segment { point p1; point p2; } double dist( point p1, // returns the distance point p2) // between two points { return sqrt( (p2.x - p1.x)*(p2.x - p1.x) + (p2.y - p1.y)*(p2.y - p1.y) ) } int point_on_segment( segment s, point p) { return dist(s.p1,s.p2) = = dist(s.p1,p) + dist(p,s.p2); }

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

View Full Document
#6. (Pointers) What is the output of the following code segment. Assume the address of x is
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 12

finalF98sol - EGN 3210 Sample Test 2 NAME#1 Solutions S.S...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online