midterm-solution

midterm-solution - University of California, San Diego...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: University of California, San Diego Department of Electrical and Computer Engineering ECE15 Solutions to Midterm Exam Fall 2008 Problem 1 (12 points) Consider the following C program. The program contains numerous errors, which you will be asked to correct. Nevertheless, it is easy to figure out what the program is supposed to do. int main(); double input // user input double result; /* result of the computation scanf("%lf", input); result == (input>0) ? input*input :-1*input*input; printf("The resut of the program is %d n", resut); return 0; a. Briefly explain in your own words what this program is supposed to do. Use at most a couple of sentences to do so. Answer: The program reads a floating-point number entered by the user. It then prints the floating-point number whose sign is the same as the sign of the input, and whose magnitude is the square of the input’s magnitude. b. Correct all the errors in the program, directly in the box below. You do not need to provide an explanation for the corrections. However, you do need to make sure that the corrected program will compile successfully (under any ANSI C compliant compiler), run without runtime errors, and do what you said it is supposed to do in part ( a ). Answer: The corrections are marked in red. Note also the absence of ; after int main() . #include <stdio.h> int main() double input ; // user input double result; /* result of the computation */ scanf("%lf", & input); result = (input>0) ? input*input :-1*input*input; printf("The result of the program is %lf n", result ); return 0; Problem 2 (20 points) Each of the following exercises contains several lines of code excerpted from a C program. Your task is to determine, in each case, which is more likely: these lines of code will cause a compilation error , they will cause a run-time error , or they will not cause any errors. Recall that: Compilation errors are errors that prevent the creation of an executable file, since the corresp- onding C code is not valid. In what follows, you should assume that any piece of code that is not valid according to the ANSI C standard will cause a compilation error. Run-time errors are errors that pass the compilation stage successfully, but cause an error during the execution of a program. A classic example of a run-time error is division by zero . For the purposes of this problem, if a program enters into an infinite loop (from which is does not exit by itself), this should be also considered a run-time error....
View Full Document

This note was uploaded on 04/15/2010 for the course ECE ECE15 taught by Professor Vardy during the Fall '08 term at UCSD.

Page1 / 8

midterm-solution - University of California, San Diego...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online