SampleExam(2) - -1- Name: 77 v 1. (16 points) Four code...

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

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: -1- Name: 77 v 1. (16 points) Four code fragments and four flow diagrams (flowcharts) follow. Note that each diagram has two action boxes and one or two question boxes. All four represent distinct patterns of logic. Match each code fragment to the corresponding flow diagram and Show how the code fits into the boxes. ANSWERS: The correct diagram for each fragment is shown in (fill in the underlined blanks with the corresponding letter) (1) Ego) <3) (4); 32/ f‘\_, L ’L ./ . ************** _ (1) if (r < 0) { v ll 0 } if (h < 0) { V II H. } ************** ************** (2) if (r < 0 ) { v = O; } else if (h < 0) { l v = 1; } ************** ************** (3) if(t<1){ v=t; } else { v = 1; } ************** ************** (4) if (r < 10) printf( “Too big\n”); a = PI * r * r; ************** '2' Name: - w -» » - 2. (30 points) Correct the errors (if any) in each statement by writing the corrected statement to the right of each: ANSWERS a. Correct the following declarations: (l) integer count; (2) double weight = 1,024.5; (3) int k; count; (4) int k==10; (5) duble age; b. Correct the following preprocessor commands: (1) #include <stdio> 1t ificlvflf I w“; iii/3?; ta (2) #define NORMAL = 98.6; "H ate? in 11*? (3) #define TOP 1,000 4T (Inga/i “$533) (4) #include stdio.h 0. Given the declarations on the first two lines that follow, correct each of the following statements: int age, count; double price, weight; 1 Scan“ “‘1 ”, & rice ); €121»: ' H v- i . V” g P I 2 scanf( “7.1 ”, wei ht ); i, I , I / g g M l ‘ (3) printf ( ‘ “Xi ’ ’ , &count); (4) scanf(‘ “Xi’ ’ , age); Cl. What is wrong with the following if statements? They are supposed to identify and print out the middle value of three double values x, y, and 2. Correct them (if necessary) to the right. \ u—h- .., ~ .~‘ 0‘ i M 7 if (x > y) I C (V4 «"3 2 if ’3‘; i ' w { if (x < z); printf( “x=%g”, x ); else printf( “z=%g”, z ); } (“3" gm » Else I V " ‘y { if (y < 25:; ~ , .v, 1,; , printf( ti" =7.g: J , y ); a A g i else printf( “z=%g”, z ); 3. '3' Name: . (24 points) Answer parts a,, b., and C. separately. a. In the following program, circle each error (if any) and show how to correct it on the right. » #include ‘ Gail? ham :1) air 5w . #define PI 3.14159 int main(void) { double (1; V . . /m§ pr1ntf( ‘ ‘Clrcumferencefli’ ’) ; fix printf( ‘ ‘Let’s compute! ! ’sznm if" Y {,8} 3’ 1‘1; l 5:; printf( ‘ ‘Enter a number: ’ ’); m scanf(“%é?’,Ch );'F __2Lg1LLEd_L;iiQi3ai{lh___.___—————————————— c = d printf( “c = zg>\n”, C ); return 0; b. Given the three declarations that follow, state on the line to its right whether the result of evaluating the expression is true or false. 5:} :: *3?“ v u I ’00 int i = 7; float f = 5.5; 1,“ char :2 = ’w’; i , ‘i/ (1) (i >?él= 6) W (C =/= ’W’) ":9 fl \/ X \/ (2) (i >= 6) H (C == 119) 2’9: "V (3) (f < 11) 8:8: (1 > 100) invite: (4)' (c != ’p’) H ((i + f) <= 10) c. The following loop fragments are to print the numbers from 1 to 3; fix them (if necessary) so int k; has been made. W) /\ they do. (Assume the declaration: (1) for (k = o; k < 3; ++k) {printf( “k = Xi”, k )3} (2) k = 1; do{ fl L Printf( “k = ‘11”, k); {57: é; if k++ - ’ } while ( k < 3); 4. '4’ Name: ~ *' (15 points) Write a complete C main program (function) that asks the user to enter a keyboard character and then outputs the following decisions about Whether the entered character was an uppercase letter, a lowercase letter, the character for a decimal digit, or something else. After outputting its decision about the single character entered the program exits normally. (Alphebetic characters are the letters A—Z, and a—z; decimal digits are 0—9.) ,. r m 1% HIV“ 7“ i’fit a V'\JI~ Wig}, ‘ [l‘i~ «9" i: / 18";- ZLL if N r 5/: u f, \ Wat i» u r l 1 i" f :6“ W :J a 1] ': iéz\{r \ ‘1» 4‘2 v ,I ' ‘7 ‘ "L r 2, .3 5 , J i / a ‘3“ x i g f L‘ L,» l l x x in h {i r \C \ (7 O ’f 1 t / t, , » a I I ‘ .‘7 7 i I V H ,> x a x V r '{r t‘ , \v‘. 1 83 ‘> '5' Name: N (15 points) Write a complete C main program (function) that asks the user to enter a decimal integer k between 1 and 10, calculates k! (k-factorial), prints out the decimal values k z: and k! 2, and exits. If the user enters a number other than one between 1 and 10, the programs prints an “out of range message” and exits. If the user enters anything other than a decimal value, it prints an “input not recognized” message and exits. ‘li warhfiizéf 4:341" » J ‘ l {muggy} ( D 2 .. \ A / V, g I» t“ " r «- —<_‘>J{ .1115» J,‘ J; > " \an ‘ r . * L’ 1 / l 'z’e’l :2? 2"" x \ a. x \ k‘ 5 \p\ 9i; ‘ (j “.1? a; .w\ l‘rww » r V O. J 2’ ’1.) Inltr‘xf“,i‘m.‘ 1.! '1' v" i A w , l "3' {» ’l ' ref“ . e v i a: at « wit; 34» 1 - ' ...
View Full Document

Page1 / 5

SampleExam(2) - -1- Name: 77 v 1. (16 points) Four code...

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

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