Exam #2 Review
(Last Mod: 27 November 2010 21:38:41 )
Links:
Bookmarks:
In addition to the topics covered on Exam #1, A review of the Course Syllabus shows that the following topics are fair game for the exam:
Functions - writing and using.
Argument Passing - Call by value and Call by reference.
Preprocessor - basic directives and macros.
Recursion.
Arrays - One dimensional and multidimensional.
Strings and string functions.
Pointers and pointer arithmetic.
Pointers as arguments to functions.
Pointers and multidimensional array.
Pointers to pointers.
Command Line arguments.
Review Questions
Specifically included as part of the review material for this is Exam is all material that was part of the Exam 1 Review plus the Exam itself.
(1) Write a parameterized macro, named distance, that computes the distance between two points. The macro takes four arguments, (x1,y1) and (x2,y2), in that order.
For instance, the following line of code:
d = distance(5.0, 9.0, 8.0, 13.0);
should store the value 5.0 in the variable d.
(2) The sine function, sin(x), may be approximated by the following power series:
sin(x) = x - (x^3)/6 for sufficiently small values of x.
The above approximation is accurate to within one part per million for |x| < 0.1, but it's accuracy degrades rapidly for values beyond that. Consider this sufficient for the purposes of this problem.
Write a complete function called sinc(x) that returns a double such that:
sinc(x) = sin(x) / x
Use the above approximation for sin(x) to ensure that your function has no problems at or in the vicinity of zero.
Without using a calculator (and, hence, you MUST show your work!), evaluate sinc(x) for x = 0.06.
(3) Given two points (x1,y1) and (x2,y2), write a code fragment that computes the y value corresponding to an x value assuming a linear relationship between y and x. You may assume that all necessary variables have been declared and initialized.
(4) Write a complete function called XtotheY() that takes two positive floating point values and returns the value obtained if the second argument is the exponent to which the first argument is raised. You may NOT use the pow() function. Hint: If you want to get any credit on this problem, do NOT use the pow() function!
(5) Given a positive floating point value x, write a complete function that outputs the logarithm of x to the base b (where b is also a positive floating point value).
(6) What is the output of the following program. Hint: Look at it very carefully.
int main()>
{
int i;
for(i=0; i<5; i++);
{
printf("%i\n",i);
}
return(1);
}
(7) What is the value of z at the end of the execution of the following code fragment:
int main()>
{
double x,y,z;
....
x = 2.0;
y = 8.0;
z = x+y / ((y>x)?(x):(y));
....
(8) Draw a flowchart for the following while loop
int main()
{
int i;
while(i++<42)
{
printf("%i\n",i);
}
return(0);
}
(9) How should two doubles be compared for equality? Write a code fragment that performs this check without using any functions from the math library.
(11) Each member of the Fibonacci Sequence is obtained by adding the two prior members. The first two members are defined as themselves. Therefore, the sequence looks like this: {0,1,1,2,3,5,8,13,21,34,....}. Write a complete C function that takes an integer n and that uses recursion to return an int that is the nth member of the Fibonacci Sequence. So fib(0) should return 0 while fib(7) should return 13. What is the largest value of n such that fib(n) can be represented as a 16-bit signed integer.
(11) Each member of the Fibonacci Sequence is obtained by adding the two prior members. The first two members are defined as themselves. Therefore, the sequence looks like this: {0,1,1,2,3,5,8,13,21,34,....}. Write a complete C function that takes an integer n and that does not use recursion to return an int that is the nth member of the Fibonacci Sequence. So fib(0) should return 0 while fib(7) should return 13. What is the largest value of n such that fib(n) can be represented as a 16-bit signed integer.
(12) Write a complete C function of type int that accepts an integer k and returns k! (k factorial).
(13) What are some of the ramifications of not checking whether a file open operation was successful?
(14) Write a complete C program that prints "Hello World!" (including the quotation marks) to a text file called world.txt in the current directory.
(15) Write a complete C program that implements the following flowchart. What is the value of i and j upon completion?
(16) What, if anything, is wrong with the following code fragment? What are the likely ramifications?
#include <stdio.h> // FILE, fopen(), fprintf()
int main(void)
{
FILE *datfile;
datfile = fopen("A:\myfile.dat", "wt");
if(datfile=NULL)
return(-1);
fprintf(datfile, "My output file.\n");
fclose(datfile);
return(0);
}
(17) The series expansion for the sine function is:
sin(x) ~= x - (x^3/3!) + (x^5/5!) - (x^7/7!) + (x^9/9!) - .......
Write a flowchart for a code fragment that uses a loop to compute sin(x) using this this series. Assume that the number of terms to be used has already been entered and stored in the variable n.
Write a function called my_sin() that uses the above code fragment to return sin(x).
(18) Write a complete C program that generates a table of trigonometric values from 0 to 90 degrees in increments of 0.1 degree. The table is to contain the following columns:
deg sine cosine tangent
The trig values should have six digits after the decimal point. There should be a blank blank line every ten degrees and the above header should appear at the top of every group of ten degrees.
In other words, the output should resemble the following:
deg sine cosine tangent
0.0 0.000000 1.000000 0.000000
0.1 0.001745 0.999998 0.001745
...
9.8 0.170209 0.985408 0.172730
9.9 0.171929 0.985109 0.174528
deg sine cosine tangent
10.0 0.173648 0.984808 0.176327
...
(19) The figure below represents a rectangular window with a circular arc above it. Write a code fragment that uses W and H (previously entered and stored in the double variables w and h, respectively) to compute the radius of the arc (to aid in laying out the arc on the wall), the arclength of the curved upper edge (to determine how much trim is needed) and the area of the arc (to use in heat-loss calculations). The units of H and W are consistent (i.e., the same).
(20) Write a complete C function that accepts the following arguments in the following order:
For instance, given the following code fragment:
double x[10][20];
double y;
y = 3.141592654;
SetElement(x, 20, 5, 15, y);
should have the same effect that you would expect from:
x[5][15] = y;
You may not use any array notation (i.e., you can't use the square brackets) anywhere within your function. You are to use pointers only.
(EXTRA CREDIT) This WILL be the ten-point extra credit problem on Exam #2.
For large values of n, the factorial of n can be closely approximated by the following equation:
For instance, for n = 10 the above equation, known as the Stirling Formula, yields a value that is within 1% of the correct answer.
(8 pts) Write a complete C function called fact_digits() that takes a double n as the argument and returns a floating point (double) value that is the number of digits in n!. Your function needs to be able to accept values of n for which n! would be far too large to accommodate even in a variable of long double. For instance, the user might wish to use your function to discover what the first value of n! is that has more than one trillion (1012) digits. A long double, on most compilers, can't even represent a number with 15,000 digits. Hint: How many digits are in 12345? What is the base ten log of 12345?
(2 pts) Within 1%, what IS the first value of n for which n! has more than a trillion digits. Hint: You can do this reasonably quickly on your calculator - but be sure to be finished with the rest of the exam first. Show your work for full credit.