Download presentation
Presentation is loading. Please wait.
Published byHubert Patrick Modified over 9 years ago
1
CMSC 104, Section 301, Fall 20021 Lecture 17, 11/04/02 Homework 4a and 4b Topics Go over Homework 4a Problems 2.7 - 2.15, Problems 3.11 - 3.13 Go over Homework 4a Problems 4.5 - 4.8
2
CMSC 104, Section 301, Fall 20022 Lecture 17, 11/04/02 Homework 4a 2.7 Identify and correct errors: a. scanf( “d”, value) ; b. Printf( “The product of %d and %d is %d”\n, x, y ) ;
3
CMSC 104, Section 301, Fall 20023 Lecture 17, 11/04/02 Homework 4a 2.7 Identify and correct errors: c.firstNumber + secondNumber = sumOfNumbers d.If ( number => largest ) largest == number ;
4
CMSC 104, Section 301, Fall 20024 Lecture 17, 11/04/02 Homework 4a 2.7 Identify and correct errors: e.*/ Program to determine the largest of three integers /* f.Scanf(“%d”, anInteger);
5
CMSC 104, Section 301, Fall 20025 Lecture 17, 11/04/02 Homework 4a 2.7 Identify and correct errors: g.printf( “Remainder of %d divided by %d is\n”, x, y, x % y); h.If ( x = y ); printf( %d is equal to %d\n”, x, y );
6
CMSC 104, Section 301, Fall 20026 Lecture 17, 11/04/02 Homework 4a 2.7 Identify and correct errors: i.print( “The sum is %d\n,” x + y ); j.Printf( “The value you enter is: %d\n, &value );
7
CMSC 104, Section 301, Fall 20027 Lecture 17, 11/04/02 Homework 4a 2.8 Fill in the blanks: a)_________ are used to document a program and improve its readability. b)The function used to print information on the screen is __________. c) A C statement that makes a decision is ___. d) Calculations are normally performed by _______________ statements. e) The ____________ function inputs values from the keyboard.
8
CMSC 104, Section 301, Fall 20028 Lecture 17, 11/04/02 Homework 4a 2.9 Write a single C statement; a)Print the message “Enter two numbers.” b) Assign the product of variables b and c to variable a
9
CMSC 104, Section 301, Fall 20029 Lecture 17, 11/04/02 Homework 4a 2.9 Write a single C statement; c) State that a program performs a sample payroll calculation (i.e., use text that helps to document a program). d) Input three integer values from the keyboard and place these values in integer variables a, b, and c.
10
CMSC 104, Section 301, Fall 200210 Lecture 17, 11/04/02 Homework 4a 2.10 True or False. a) C operators are evaluated from left to right. b) The following are all valid variable names: _under_bar_, m928134, t5, j7, her_sales, his_account_total, a, b, c, z, z2
11
CMSC 104, Section 301, Fall 200211 Lecture 17, 11/04/02 Homework 4a 2.10 True or False. c) The statement printf(“a = 5;”); is a typical example of an assignment statement. d) A valid C arithmetic expression containing no parentheses is evaluated from left to right.
12
CMSC 104, Section 301, Fall 200212 Lecture 17, 11/04/02 Homework 4a 2.10 True or False. e) The following are all invalid variables names: 3g, 87, 67h2, h22, 2h
13
CMSC 104, Section 301, Fall 200213 Lecture 17, 11/04/02 Homework 4a 2.11 Fill in the blanks. a) What arithmetic operations are on the same level of precedence as multiplication? b) When parentheses are nested, which set of parentheses is evaluated first in an arithmetic expression?
14
CMSC 104, Section 301, Fall 200214 Lecture 17, 11/04/02 Homework 4a 2.11 Fill in the blanks. c) A location in the computer’s memory that may contain different values at various times throughout the execution of a program is called a ___________.
15
CMSC 104, Section 301, Fall 200215 Lecture 17, 11/04/02 Homework 4a 2.12 Assume x = 2 and y = 3. Output? a) printf( “%d”, x); b) printf( “%d”, x + x); c) printf( “x=“ ); d) printf( “x=%d”, x);
16
CMSC 104, Section 301, Fall 200216 Lecture 17, 11/04/02 Homework 4a 2.12 Assume x = 2 and y = 3. Output? e) printf( “%d = %d”, x + y, y + x); f) z = x + y ; g) scanf( “%d%d”, &x, &y ); h) /* printf( “x + y = %d”, x + y); */ i) printf( “\n” );
17
CMSC 104, Section 301, Fall 200217 Lecture 17, 11/04/02 Homework 4a 2.13 Destructive read-in? a) scanf( “%d%d%d%d%d”, &b,&c,&d,&e,&f); b) P = I + j + k + 7 ; c) printf( “Destructive read-in” ); d) printf( “a = 5”);
18
CMSC 104, Section 301, Fall 200218 Lecture 17, 11/04/02 Homework 4a 2.14 Given y = ax 3 + 7 ; Which are correct? a) y = a * x * x * x + 7 ; b) y = a * x * x * (x + 7) ; c) y = (a * x) * x * (x + 7) ; d) y = (a * x) * x * x + 7 ; e) y = a * (x * x * x) + 7 ; f) y = a * x * x * x + 7 ;
19
CMSC 104, Section 301, Fall 200219 Lecture 17, 11/04/02 Homework 4a 2.15 State the order of evaluation and find x. a) x = y + 3 * 6 / 2 - 1;
20
CMSC 104, Section 301, Fall 200220 Lecture 17, 11/04/02 Homework 4a 2.15 State the order of evaluation and find x. b) x = 2 % 2 + 2 * 2 - 2 / 2 ;
21
CMSC 104, Section 301, Fall 200221 Lecture 17, 11/04/02 Homework 4a 2.15 State the order of evaluation and find x. c) x = ( 3 * 9 * ( 3 + ( 9 * 3 / ( 3 ) ) ) );
22
CMSC 104, Section 301, Fall 200222 Lecture 17, 11/04/02 Homework 4a 3.11 Identify and correct errors. d) while ( y > 0 ) { printf (“%d\n”, y ); ++y; }
23
CMSC 104, Section 301, Fall 200223 Lecture 17, 11/04/02 Homework 4a 3.12 Fill in blanks. a) The solution to any problem involves performing a series of actions in a specific ___________. b) A synonym for procedure is ___________. c) A variable that accumulates the sum of several number is a __________. d) The process of setting certain variables to specific values at the beginning of a program is called ________________.
24
CMSC 104, Section 301, Fall 200224 Lecture 17, 11/04/02 Homework 4a 3.12 Fill in blanks. e) A special value used to indicate “end of data entry” is called a __________________, a __________________, a __________________, or a __________________ value.
25
CMSC 104, Section 301, Fall 200225 Lecture 17, 11/04/02 Homework 4a 3.12 Fill in blanks. f) A _________________ is a graphical representation of an algorithm. g) In a flowchart, the order in which the steps should be preformed is indicated by _____________________ symbols. h) The termination symbol indicates the _____________ and ___________ of every algorithm.
26
CMSC 104, Section 301, Fall 200226 Lecture 17, 11/04/02 Homework 4a 3.12 Fill in blanks. i) Rectangle symbols correspond to calculations that are normally performed by ________________ statements and input/output operations that are normally by calls to the ____________ and _________ standard library functions. j) The item written inside a decision symbol is called a ________________.
27
CMSC 104, Section 301, Fall 200227 Lecture 17, 11/04/02 Homework 4a 3.13 Output of this program? #include int main () { int x = 1, total = 0, y; while ( x <= 10 ) { y = x * x; printf(“%d\n”, y); total += y; ++x; } printf(“Total is %d\n”, total); return 0; }
28
CMSC 104, Section 301, Fall 200228 Lecture 17, 11/04/02 Homework 4b 4.5 Identify and correct errors. a) for ( x = 100, x >= 1, x++ ) printf( “%d\n”, x );
29
CMSC 104, Section 301, Fall 200229 Lecture 17, 11/04/02 Homework 4b 4.5 Identify and correct errors. c) Input is “100 A” and print the input. scanf( “%d”, &intVal ); charVal = getchar(); printf( “Integer: %d\nCharacter: %c\n”, intVal, charVal );
30
CMSC 104, Section 301, Fall 200230 Lecture 17, 11/04/02 Homework 4b 4.5 Identify and correct errors. d) for ( x =.000001; x <=.0001; x +=.000001) printf( “%.7f\n”, x);
31
CMSC 104, Section 301, Fall 200231 Lecture 17, 11/04/02 Homework 4b 4.5 Identify and correct errors. e) Output the odd integers from 999 to 1. for ( x = 999; x >= 1; x += 2) printf( “%.d\n”, x);
32
CMSC 104, Section 301, Fall 200232 Lecture 17, 11/04/02 Homework 4b 4.5 Identify and correct errors. f) Output even integers from 2 to 100. counter = 2; Do { if ( counter % 2 == 0) printf( “%.d\n”, counter); counter += 2; } While (counter < 100) ;
33
CMSC 104, Section 301, Fall 200233 Lecture 17, 11/04/02 Homework 4b 4.5 Identify and correct errors. g) Sum from 100 to 150. for ( x = 100; x <= 150; x ++) ; total += x;
34
CMSC 104, Section 301, Fall 200234 Lecture 17, 11/04/02 Homework 4b 4.6 Output? a) for ( x = 2; x <= 13; x += 2) printf( “%d\n”, x); b) for ( x = 5; x <= 22; x += 7) printf( “%d\n”, x); c) for ( x = 3; x <= 15; x += 3) printf( “%d\n”, x);
35
CMSC 104, Section 301, Fall 200235 Lecture 17, 11/04/02 Homework 4b 4.6 Output? d) for ( x = 1; x <= 5; x += 7) printf( “%d\n”, x); e) for ( x = 12; x >= 2; x -= 3) printf( “%d\n”, x);
36
CMSC 104, Section 301, Fall 200236 Lecture 17, 11/04/02 Homework 4b 4.7 Write for statements. a) 1,2,3,4,5,6,7 b) 3,8,13,18,23
37
CMSC 104, Section 301, Fall 200237 Lecture 17, 11/04/02 Homework 4b 4.7 Write for statements. c) 20,14,8,2,-4,-10 d) 19,27,35,43,51
38
CMSC 104, Section 301, Fall 200238 Lecture 17, 11/04/02 Homework 4b 4.8 Output? printf( "Enter two integers in the range 1- 20:\n"); scanf("%d%d", &x, &y) ; for ( i = 1; i <= y; i ++) { for (j = 1; j <= x; j++ ) printf( "@"); printf( "\n"); }
39
CMSC 104, Section 301, Fall 200239 Lecture 17, 11/04/02 Assignment and Next Read Sections 3.9 – 3.10 Next: Exam 2 on Wednesday, 11/06. Project 2 due Sudany, 11/10, midnight.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.