cmsc 104, section 301, fall 20021 lecture 17, 11/04/02 homework 4a and 4b topics go over homework 4a...

39
CMSC 104, Section 301, Fall 2002 1 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

Upload: hubert-patrick

Post on 13-Dec-2015

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 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

CMSC 104, Section 301, Fall 2002 1 Lecture 17, 11/04/02

Homework 4a and 4b

Topics

• Go over Homework 4aProblems 2.7 - 2.15, Problems 3.11 - 3.13

• Go over Homework 4aProblems 4.5 - 4.8

Page 2: 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

CMSC 104, Section 301, Fall 2002 2 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 ) ;

Page 3: 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

CMSC 104, Section 301, Fall 2002 3 Lecture 17, 11/04/02

Homework 4a

2.7 Identify and correct errors:

c. firstNumber + secondNumber = sumOfNumbers

d. If ( number => largest ) largest == number ;

Page 4: 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

CMSC 104, Section 301, Fall 2002 4 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);

Page 5: 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

CMSC 104, Section 301, Fall 2002 5 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 );

Page 6: 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

CMSC 104, Section 301, Fall 2002 6 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 );

Page 7: 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

CMSC 104, Section 301, Fall 2002 7 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.

Page 8: 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

CMSC 104, Section 301, Fall 2002 8 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

Page 9: 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

CMSC 104, Section 301, Fall 2002 9 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.

Page 10: 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

CMSC 104, Section 301, Fall 2002 10 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

Page 11: 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

CMSC 104, Section 301, Fall 2002 11 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.

Page 12: 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

CMSC 104, Section 301, Fall 2002 12 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

Page 13: 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

CMSC 104, Section 301, Fall 2002 13 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?

Page 14: 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

CMSC 104, Section 301, Fall 2002 14 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 ___________.

Page 15: 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

CMSC 104, Section 301, Fall 2002 15 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);

Page 16: 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

CMSC 104, Section 301, Fall 2002 16 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” );

Page 17: 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

CMSC 104, Section 301, Fall 2002 17 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”);

Page 18: 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

CMSC 104, Section 301, Fall 2002 18 Lecture 17, 11/04/02

Homework 4a

2.14 Given y = ax3 + 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 ;

Page 19: 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

CMSC 104, Section 301, Fall 2002 19 Lecture 17, 11/04/02

Homework 4a

2.15 State the order of evaluation and find x.

a) x = y + 3 * 6 / 2 - 1;

Page 20: 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

CMSC 104, Section 301, Fall 2002 20 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 ;

Page 21: 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

CMSC 104, Section 301, Fall 2002 21 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 ) ) ) );

Page 22: 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

CMSC 104, Section 301, Fall 2002 22 Lecture 17, 11/04/02

Homework 4a

3.11 Identify and correct errors.

d) while ( y > 0 ) {

printf (“%d\n”, y );

++y;

}

Page 23: 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

CMSC 104, Section 301, Fall 2002 23 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 ________________.

Page 24: 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

CMSC 104, Section 301, Fall 2002 24 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.

Page 25: 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

CMSC 104, Section 301, Fall 2002 25 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.

Page 26: 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

CMSC 104, Section 301, Fall 2002 26 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 ________________.

Page 27: 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

CMSC 104, Section 301, Fall 2002 27 Lecture 17, 11/04/02

Homework 4a

3.13 Output of this program?#include <stdio.h>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;}

Page 28: 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

CMSC 104, Section 301, Fall 2002 28 Lecture 17, 11/04/02

Homework 4b

4.5 Identify and correct errors.

a) for ( x = 100, x >= 1, x++ )

printf( “%d\n”, x );

Page 29: 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

CMSC 104, Section 301, Fall 2002 29 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 );

Page 30: 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

CMSC 104, Section 301, Fall 2002 30 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);

Page 31: 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

CMSC 104, Section 301, Fall 2002 31 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);

Page 32: 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

CMSC 104, Section 301, Fall 2002 32 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) ;

Page 33: 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

CMSC 104, Section 301, Fall 2002 33 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;

Page 34: 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

CMSC 104, Section 301, Fall 2002 34 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);

Page 35: 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

CMSC 104, Section 301, Fall 2002 35 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);

Page 36: 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

CMSC 104, Section 301, Fall 2002 36 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

Page 37: 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

CMSC 104, Section 301, Fall 2002 37 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

Page 38: 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

CMSC 104, Section 301, Fall 2002 38 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"); }

Page 39: 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

CMSC 104, Section 301, Fall 2002 39 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.