Download Practice Midterm Exam 2 - Fall 2006 - Computer Program for Science | CSC 107 and more Exams Computer Science in PDF only on Docsity!
CSC 107: Fall 2006 Midterm
Directions:
- Clearly write your name at the top of every page.
- Skim the entire exam before you start working. Not all questions are of equal difficulty; start with the easiest ones before moving to the harder ones.
- No computers or collaborations allowed.
- If you need extra space for a problem, use the back of the previous page and then, if you need even more space, ask for scratch paper.
- Indicate your final answer clearly. Erase, scratch out, or otherwise clearly show anything that is not part of your answer. I will grade anything I see that might be your answer.
- When a question asks you to write code, you must supply working C code to earn full credit. Minor syntactic errors will not be penalized, but what the code does must be clear.
- If you have a question about what something means, ask. Better to ask and get the question right, then guess and get a 0.
Points Breakdown:
- 18 points
- 20 points
- 25 points
- 25 points
- 10 points
- 2 points
Total Points: 100 points
Problems:
- [6 * 3 points] Each of the following printf, scanf, fprintf, and fscanf commands contain exactly one error. The error may be a syntax error or a run-time error. Using the following variable declarations, rewrite each commands to remove the error. All file output should use the fOutput file pointer; all file input should use the fInput file pointer.
int iA, iB, iC; double dX, dY, dZ; FILE (^) *fInput, *fOutput;
(a) [3 points] fscanf(‘‘Hi, Mom! I am rocking this midterm’’);
(b) [3 points] fprintf(FILE (^) *fOutput, ‘‘%d %d’’, iB, iC);
(c) [3 points] scanf(‘‘%d’’, iA);
(d) [3 points] printf(‘‘%lf’’ dZ);
(e) [3 points] fscanf(fInput, ‘‘%lf %d’’, &iA, &iB);
(f) [3 points] printf(‘‘%d %lf’’, &iC, dZ);
- [25 points] Use the provided function to complete the following program. For each of the solar system’s 4 inner planets (e.g., Mecury, Venus, Earth, and Mars), compute and print out how much a sack of potatos weighs on them. For full credit, you must use the symbolic constants and computeWeight.
#include <stdio.h> #include <stdlib.h>
#define MERCURY_GRAVITY 0. #define VENUS_GRAVITY 0. #define EARTH_GRAVITY 1. #define MARS_GRAVITY 0.
#define SACK_MASS 5 // Potatos are sold in 5 lb. bags
double computeWeight( double planetsGrav, double mass) { return (planetsGrav (^) * mass); }
int main( void ) { double potatoNewWeight;
/* Add your code here (^) */
- [25 points] Two cars, one going due North and one going due East, collide in an intersection. After the crash, the cars stick together. Complete the following C program so that it computes the angle and speed which the mass of crumpled steel travels.
#include <stdio.h> #include <stdlib.h>
int main( void ) { /* To solve this problem you will need to:
- 1.^ Declare^ the^ variables^ you^ need
- 2.^ Open^ file^ named^ ‘‘cars.dat’’^ for^ reading
- 3.^ Read^ first^ car’s^ mass^ &^ velocity^ (both^ are^ doubles)
- 4.^ Read^ second^ car’s^ mass^ &^ velocity^ (both^ are^ doubles)
- 5.^ Close^ the^ file
- 6.^ Compute^ the^ angle^ they^ travel^ after^ the^ accident^ using:
- angle^ =^ atan((massCar2^ * vCar2)^ /^ (massCar1^ * vCar1));
- 7.^ Compute^ the^ velocity^ of^ the^ 2-car^ wreck:
- velocity^ =^ ((massCar2^ * vCar2)^ /
- ((massCar1^ +^ massCar2)^ * sin(angle)));
- 8.^ Print^ out^ value^ computed^ for^ angle^ and^ velocity.