midterm-solution

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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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 .
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern