Homework 3 Homework 3 is due on Friday October 13th by 11 59 PM Please submit your Homework on BlackBoard as a pdf file and follow the complete Please ask all questions about this assignment during office hours or post them on Campuswire submission guidelines Homework Be sure to show your work on all your homework problems otherwise you risk not receiving credit It is recommended that you do not use a calculator or online tools etc when you do your homework Remember that there will be no calculators allowed on exams Introduction to C programming 1 sample c Read and analyze the program below then answer the following questions about the program s behavior when compiled and executed by a user 1 2 If the user enters the following sequence of samples once per prompt each followed by Enter 12 20 100 21 17 19 43 104 8 5 what exactly does the program output after all samples have been entered If the user enters the following sequence of samples once per prompt each followed by Enter 37 16 319 0 12 41 8 4 13 3 what exactly does the program output after all samples have been entered sample c include stdio h int main int i int sample int A int B Read the first sample printf Enter the first sample The expression scanf returns the number of values converted If the human user types a number as expected the expression s value is 1 Otherwise the human did something wrong so we end the program if 1 scanf d sample printf Numeric samples only n Quit indicating failure anything non zero by convention return 3 Process the first sample A sample B sample for i 2 10 i i i 1 Read another sample printf Enter sample d i if 1 scanf d sample printf Numeric samples only n Quit indicating failure anything non zero by convention return 3 Process the next sample if A sample A sample if B sample B sample Print the results printf The A value is d n A printf The B value is d n B Program has finished successfully so return 0 by convention return 0 3 4 What is the relationship between the samples entered by the user and the variable A printed at the end of the program In other words what value is printed for A in terms of the samples What is the relationship between the samples entered by the user and the variable B printed at the end of the program In other words what value is printed for B in terms of the samples 2 average c 3 Read and analyze the program below then answer the following questions about the program s behavior when compiled and executed by a user 1 2 If the user enters the following sequence of samples once per prompt each followed by Enter 110 120 130 140 150 160 170 180 190 200 what exactly does the program output after all samples have been entered If the user enters the following sequence of samples once per prompt each followed by Enter 29 29 29 29 29 29 29 29 29 29 what exactly does the program output after all samples have been entered 3 4 In one or two sentences explain why the value printed by the program in response to the samples given in part b does not match the value that a human might expect Use line numbers for specificity In one or two sentences suggest a change to the program to make the calculation of the average more accurate average c include stdio h int main int i int sample int average 0 for i 1 10 i i i 1 Read a sample printf Enter sample d i if 1 scanf d sample printf Numeric samples only n Quit indicating failure anything non zero by convention return 3 Process the next sample average average sample 10 Print the results printf The average is d n average Program has finished successfully so return 0 by convention return 0 3 Binary arithmetic 1 2 3 4 5 6 7 X Y X Y Y 3 X Y X Y 3 X Y 3 4 cryptic c Section 1 5 4 of Lumetta s notes explains the meaning of C operators 5 Number of executions Consider the C loop shown below Variables x and i both have type int for i 0 x i i i 3 This is the loop body How many times does the loop body execute 1 2 3 when variable x is 35 when variable x is 52 when variable x is 12 4 Calculate the decimal value for each of the following C expressions assuming that variable X has value 102 variable Y has value 31 and both have type i nt assume 32 bit 2 s complement Download compile and execute the program your name do not include the prompt for input nor your typing Please note that you are not asked to understand the output nor to understand the program which makes use of C constructs that you will learn in ECE220 We are only checking that you know how to compile and execute the program Type in your name without spaces and record the output of the program after you have entered cryptic c is inserted at one of the insertion points in the code below 4 when variable x is 0 6 Conditional construct The conditional construct if X 6 printf Y if Y 16 printf X INSERTION POINT A else INSERTION POINT B INSERTION POINT C prints X when Y 16 never prints Y and never prints XY 7 Truth table See Exams page Solve problem 6 from UIUC Spring 2016 Midterm 1 exam For each insertion point A B and C indicate under what conditions in other words for what values of variables X and Y the resulting code prints nothing X Y and XY For example if the conditional construct based on X is NOT inserted the answer should appear as follows The code
View Full Document