Homework2_Solution

.pdf

School

San Francisco State University *

*We aren’t endorsed by this school

Course

213

Subject

Computer Science

Date

Dec 6, 2023

Type

pdf

Pages

6

Uploaded by EarlBoar3947

Report
ENGR213 Spring 2023 Homework 2 Solution Part A (40%) 1. Determine which of the following are valid identifiers. If invalid, explain why. (5%) A. Name and address: Blank spaces are not allowed B. 123-45-6789: An identifier must begin with a letter or an underscore 2. Determine which of the following are valid constants. If invalid, explain why. (10%) A. 0.8E 8 valid integer constant? Invalid because of blank space B. 018CDF valid integer constant? Invalid, in hexadecimal constant there must be x (i.e., 0x18CDF) C. \ a’ – valid character constant? Valid D. “The professor said, “please don’t cheat in Exam” – valid string constant? Invalid, the quotation marks and apostrophes within the string must be expressed as escape sequence (i.e., “The professor said, \ “please don\ ’t cheat in Exam \ ”) 3. Write appropriate declarations and assign the given initial values for each group of variables and arrays. (10%) A. Floating-point variables: a= -8.2, b = 0.005 Integer variables: x = 129, y = 87, z = -22 Character variable: c1= ‘w’, c2 = ‘&’ B. One- dimensional character array: message = “ERROR”
ENGR213 Spring 2023 4. What will be output of the following c program? 5% A. 20 B. 0 C. Compilation error D. None of these 5. Run the following program and analysis the program output. 10% A. What is the output of this program? Why? c1 = a, c2 = b c1 = 97, c2 = 98 The reason why the first line is printing chars even with an integer initialized to it is because %c will automatically see that as an ASCII code and finds the corresponding character. The second line only requires an integer, so the program will choose the integer version of char which is the ASCII code to print. B. If we change the line 5, 6 to C1 = 197; C2 = 198; What is the output of this program after you change the code? Why?
ENGR213 Spring 2023 c1 = [?], c2 = [?] c1 = -59, c2 = -58 The first printf() gives character output translated from ASCII table but there’s no corresponding character in number 197 and 198 (because the range limit is 0 to 127) it’s showing a question mark symbol instead. For the second printf( ), since c1 and c2 are declared as a char, data type char has a range limit from -128 to 127. If a positive number exceed the boundary it loops from the other end, which is -128. In this case, a positive number 128 as type char represents as -128 and 129 as -127. So 197 prints as -128+(197-127) -1 = -59; and 198 prints as -128+(198-127) -1 = -58. C. If we change the line 4 to int c1,c2; What is the output of this program after you change the code? Why? c1 = [?], c2 = [?] c1 = 197, c2 = 198 The first printf() is the same as (2) for the same reason. The second printf() gives the number of 197 and 198 instead of negative number because c1 and c2 are declared as data type int, which has a much larger range limit than 3 digits. So the number may not loop back as a negative number as output
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help