Exam 1Makeup
(Last Mod: 27 November 2010 21:38:42 )
Ground Rules
This assignment is optional - you are not required to do it.
This assignment is worth 50 points. Your score on this assignment will be added directly to your Exam #1 score.
This assignment is a solo effort. If you have any questions, you may ask me for hints, suggestions or clarifications. You are NOT to discuss the assignment with ANYONE else. Do your own work - collaboration is surprisingly easy to detect. Any indication of collaboration will result in the individuals involved receiving no credit on the entire assignment, even if collaboration is only apparent on a single problem.
No credit will be awarded for work received after the due date.
While you are not required to turn in any source code files, you are expected to verify that your answer is correct by writing a test program. As a result, partial credit will be very limited - you should KNOW that your answer is correct when you turn it in.
You need to show your work to receive credit.
From the text:
Section 1.3, Exercises 1-28
Section 4.11, Exercises 1-9
Long Answer
(1) Two points define a straight line. One standard formula for such a line is y=mx+b where x and y are the variables and m and b are the parameters. However, for lines with nearly vertical slope, it is sometimes preferred to invert this expression and write it as x=ny+a. Write a code fragment that takes two points, (x1,y1) and (x2,y2) and determines the corresponding m and b or the corresponding n and a depending on the slope of the line. Set a flag (named verticalflag) to a true value if the angle of the line is within 10 degree of vertical. Assume that the coordinates of the points are of type double and have already been entered. Likewise, assume that all your code fragment has to do is set the already declared variables to the appropriate values. You only need to set the pair of variables corresponding to the value of verticalflag - the user is expected to consider the other pair undefined.
(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 (it is accurate to within one part per million for |x| < 0.1).
Write a complete function called sinc(x) that returns a double such that:
sinc(x) = sin(x) / x
Use the above approximation to avoid problems in the vicinity of zero.
(3) Given two points (x1,y1) and (x2,y2), write a code fragment that computes the y value corresponding to an x value (already entered by the user) assuming a linear relationship between y and x.
(4) Given two positive floating point values x and y, write a code fragment that outputs x^y. You may 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 a positive floating point value).
(6) What is the output of the following program:
(7) What is the value of x at the end of the execution of the following code fragment:
(8) Draw a flowchart for the following while loop
}
(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: {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(3) should return 3 while fib(7) should return 21. 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: {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(3) should return 3 while fib(7) should return 21. 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 if 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!" 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?
(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 the above series (just the five terms shown) to calculate the sine of x.
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 four places after the decimal point. There should be several blank lines 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:
(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 of type double) 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) 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 an integer 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 to 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.
Multiple Choice and Short Answer
1)
Which of the following statements does not have the same effect as all
the others?
(a)
k = k+1;
(b)
k =+ 1;
(c)
++k;
(d)
k += 1;
2)
What are the values of the variables after the following statements are
executed?
j
= 1;
k
= j++;
m
= (++k = = j++);
(a)
j = 2 k = 2
m = 0
(b)
j = 3 k = 3
m = 1
(c)
j = 3 k = 2
m = 1
(d)
j = 3 k = 2
m = 0
3)
Which of the following are complementary pairs of operators?
(a)
(= = , !=), (>,<), (>=, <=)
(b)
(= = , !=), (>=,<), (>, <=)
(c)
(= = , !=), (>,>=), (<=, <=)
(d)
(= = , !=), (>,<), (>=, =<)
4)
If j = 0, k = 2 and m = 14, what value is stored in n?
n
= (k && m) + (j < (k/m)) + (j || (!m)) +(m/k);
(a)
0
(b)
1
(c)
7
(d)
8
5)
What is the purpose of type casting?
(a)
To create new data types.
(b)
To change the data type of a variable.
(c)
To convert the data stored in a variable to a different type before
using it in an expression.
(d)
To prevent the loss of data when passing values to functions.
6)
Which operator is not defined for floating point data types?
(a)
++
(b)
&&
(c)
<=
(d)
+=
7)
Which of the following statements is false?
(a)
A logical operator will always return a value of 0 or 1.
(b)
A value is a logical FALSE if it is equal to 0.
(c)
A value is a logical TRUE if it is equal to ?1.
(d)
Negative values cannot be interpreted as logical values.
8)
Modulo division of m by n (m%n) is used to return
(a)
The result of an integer division.
(b)
A value of 1 if m can be evenly divided by n.
(c)
The same value that would be returned by m ? n*(m/n).
(d)
The result of a floating point division even if the operands are
integers.
9)
What is the proper format specifier for an argument of type double?
(a)
%d in both scanf() and printf().
(b)
%f in printf() and %lf in scanf().
(c)
%lf in both printf() and scanf().
(d)
%f in printf() and %d in scanf().
10)
Which of the following
assignments produces a value of zero?
(a)
result = 9%3 ? 1;
(b)
result = 8%3 ? 1;
(c)
result = 2 ? 6%3;
(d)
result = 2 ? 8%3;
11)
Consider the following statement:
int i = 100, j = 0; Which of the following statements is true?
(a)
1 < 3
(b)
!(j<1)
(c)
(i>0) || (j>50)
(d)
(j<1) && (i<=10)
12)
The expression (!((4-4%3) < 5 && (6/4 >3))) is
(a)
true.
(b)
false.
(c)
invalid.
(d)
none of the above.
13)
Which of the following is a valid function definition statement?
(a)
function cube (double x)
(b)
double cube (double x)
(c)
double cube (x)
(d)
cube (double x)
14)
In a function call, the actual parameters are separated by
(a)
commas.
(b)
semicolons.
(c)
colons.
(d)
spaces.
15)
What must be true of an else statement?
(a)
It must contain a logical test.
(b)
It must appear immediately after an if()
statement in the program structure.
(c)
It is associated with the closest if() statement to it.
(d)
It must be properly indented for the compiler to determine which if()
statement is controlling it.
16)
What must be true of a switch() statement.
(a)
The controlling expression must return an integer result (which
includes chars).
(b)
A default case is required and must appear last in the case list.
(c)
The only way to exit a switch() structure is through the use of the break statement.
(d)
Multiple values can be listed on the case line ? such as case
1,2,3
17)
Why does C offer three different looping structures?
(a)
Because no one looping structure can implement all of the different
types of loop logic that might be needed by a program.
(b)
Because the for() loop is only capable of executing a finite and
predetermined number of times and the while() loop is not guaranteed to
execute the loop code at least once.
(c)
Because each structure lends itself to a certain type of looping logic
and program readability and maintainability are enhanced if the structure used
matches the logic implemented.
(d)
For compatibility with other languages.
18)
Which of the following is not a purpose of a function prototype?
(a)
To force the compiler to use the return type specified by the
prototype.
(b)
To permit the compiler to compile code where it encounters a call to
the function prior to encountering the function definition itself.
(c)
To permit the function definition to be kept in a completely different
.c file.
(d)
To allow the compiler to do type checking on function calls.
19)
What is the difference between the following two statements?
#include
<filename.h>
#include
?filename.h?
(a)
The <> version can only be used for standard library files.
(b)
The ?? version looks for the file in the specified path or current
directory first.
(c)
The ?? version is for C++ header files and the <> version is
for C header files.
(d)
There is no difference, both are allowed and are equivalent.
20)
What is the purpose of indenting various lines of code different
amounts?
(a)
It is a requirement of the language ? the amount of indenting is used
by the compiler to determine code structure.
(b)
It is purely to aid the programmer in determining code structure
quickly and accurately ? the compiler ignores all indenting.
(c)
The indenting of certain functions, such as loops and if()...else
structures, are mandatory and the rest is just to make the appearance of the
code more consistent.
(d)
It is purely for aesthetic value and is completely arbitrary. There is
no significant advantage to indented code versus non-indented code.
21)
What are the three basic building-block structures of a structured
program?
(a)
Input statements, output statements, computation statements.
(b)
Goto?s, loops, I/O.
(c)
Sequences, selections and repetitions.
(d)
Looping structures, switching statements, computed goto statements.
22)
What is the value of fives(ceil(sqrt(62.5)))?
int
fives(int n) {return( ((n%5) != 0) );}
(a)
0
(b)
1
(c)
7
(d)
8
23)
Which of the following is not an advantage of modularizing a program?
(a)
The problem can be broken down into self-contained sub-problems.
(b)
The code can be designed, implemented and tested in manageable pieces.
(c)
The code can be reused both in the same project and in future projects.
(d)
The details of how individual tasks are performed is readily apparent
to all users.
24)
What is a sentinel signal?
(a)
A special data record used to indicate the last record in a data file.
(b)
An special variable whose value is set by fopen() and that can be
queried to determine the reason that a file open operation failed.
(c)
Any out-of-bounds condition such as division-by-zero that would
indicate a problem with the code logic.
(d)
A value stored at the beginning of a data file telling how many valid
records are in the file ? also known as a header record.
25)
If you desire to print certain characters using printf(), such as ?
and ?, you must precede them with a special character. What is that
character:
(a)
%
(b)
\
(c)
/
(d)
&
Short Answer (4pts each)
26)
What should always be done after executing an fopen() function call and
what are the potential consequences if this is not done?
27)
What are the three types of errors that can be found in a program and
which of them can the compiler NOT identify and trap?
28)
What are the three looping structures available in C?
29)
If a (non-global) variable is not initialized, what value will it have
by default?
30)
What is the value stored in ?x? in the following expression?
x = ((x=10/3)>3);