Homework 3 Homework 3 is due on Friday September 16 at 4 00pm Remember to include your Discussions section e g AB1 and follow the complete Homework submission guidelines Please ask all questions about this assignment during the TA and UA office hours Questions sent via e mail will not be answered Please remember to submit your homework before the deadline and staple your submission Homework that is not stapled or late will NOT be graded No exceptions Most of the problems in this homework require showing your work If you do not show your work half of the points will be taken away Introduction to C programming 1 Code transformation Before a program written in C can be executed on a computer what step is required to be done first Briefly explain this step 2 Basic I O in C Write a few lines of code that will accomplish the following 1 Ask the user to first enter his her weight in kilograms and second enter the height in meters For every field to be filled in print on screen the messages Type your weight and Type your height respectively 2 Print on screen on a different line the message Your BMI is and the BMI value of the user according to the following formula For example if the user s height is 1 80 m and the weight is 75 kg you should print to screen Your BMI is 23 148 Note for this problem there is no need to write any include preprocessor directives in the solution Hint the formula is easy to implement if you remember the definition of power of a number 3 Operators What is the value each of the following C expressions evaluate to For parts 1 to 4 assume a and b have been initialized with 0x2016 and 0x2020 respectively for part 5 assume the values of a and b are 201610 and 202010 respectively Write your answers as hexadecimal numbers 1 2 3 4 5 a b a b a a b a b 4 Conditional Constructs Give the output for the following code segments assuming that x equals 5 at the beginning of each segment a b c Show exactly what would be displayed on the terminal If nothing would be displayed write No output a if x 0 printf First sentence n if x 5 printf Second sentence n else if x 0 printf Third sentence n else if x 5 printf Last sentence n b y 3 if x 3 y x 2 else if x 5 y x 1 if y 3 printf y 3 n else if y 3 printf y 3 n else printf y 3 n c if x 5 printf First option n else if x 0 printf Second option n 5 Range Checking Let the variable ascii be of type char In general it could contain any valid ASCII character The following code prints the variable ascii to the terminal Modify the code to only print numbers 0 9 or the addition or substraction characters printf c ascii 6 Nested Conditional Constructs Manually trace the following code segments assuming that x equals 12 Show exactly what would be displayed on the terminal a if x 0 printf x 0 n if x 10 printf x 10 n if x 12 printf x 12 n else printf Hello world n b if x 12 printf True n else printf False n 7 Iterative Constructs Manually trace the following code segments Show exactly what would be displayed on the terminal a int a 0 for a 10 a 10 a a 1 printf d n a printf Done n b int a b for a 1 a 6 a a 1 for b 3 b 1 b b 1 printf d d d n a b a b 8 To Read or Not to Read Solve this problem using an EWS Linux machine You receive an email from your friend He says Hey man I quickly programmed this code we need Here it is careless programmer c You can compile it using the command gcc careless programmer c o solution and you can run it using solution Thanks Your friend 1 Download careless programmer c to an EWS Linux machine From the directory where you downloaded run the two commands your friend gave to compile the program and then to run it What does the program print to the screen 2 Read the source code for careless programmer c Does the output you observed match your expectations after reading the source code 3 Compile careless programmer c with this command instead gcc careless programmer c o solution Wall Now the compiler will warn you about a line in the code causing problems The warning will look like this careless programmer c LINE NUMBER 2 warning suggest parentheses around assignment used as truth value Wparentheses The message on your screen will report an actual number in place of LINE NUMBER Which line number is the compiler warning you about 4 How can you fix this line of code 5 Modify careless programmer c to fix that line of code Then compile the fixed code and run it using the same commands you used for question 1 What is the output now
View Full Document