1 CS161 Introduction to Computer Science Topic #17.

12
1 CS161 CS161 Introduction to Introduction to Computer Science Computer Science Topic #17

Transcript of 1 CS161 Introduction to Computer Science Topic #17.

Page 1: 1 CS161 Introduction to Computer Science Topic #17.

1

CS161 CS161

Introduction to Introduction to Computer ScienceComputer Science

Topic #17

Page 2: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 2

Today in CS161• Review for the Final

– What material are you responsible for– Suggestions on how to prepare...– Sample Questions

• What’s CS162 Like...– Material Covered in CS162– Handouts for CS162

Page 3: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 3

Information about the Final Exam

• The final will cover our in class lectures• The final will also cover the reading

assignments for the term; refer to the outline distributed during the first day for a list of these assignments

• It is a closed book and closed notes exam

• There will be questions asking you write functions and evaluate conditional expressions. There will be set of short answer.

Page 4: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 4

Material Covered:

• Using functions, writing your own functions, function prototypes, actual and formal arguments (call by value versus call by reference), and return values

• The while statement, the for statement, and the do-while

• The increment and decrement operators, in both their prefix and postfix forms

• Arrays of characters and structures• Text files

Page 5: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 5

Sample Questions:

• Show how you would read in an array of characters from the keyboard. Make sure that you don’t read more characters than there is room for in the array:

char name[20];

(Answer: cin.get(name,20);cin.get(); )

Page 6: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 6

Sample Questions:

• When should we use call by value instead of call by reference?

(Answer: when you need a complete and duplicate copy of the data )

Page 7: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 7

Sample Questions:

• What are the benefits of call by reference? – List 2

(Answer: 1) Can change original valuesof the actual arguments2) Doesn’t require extra

memory to hold a duplicate copy )

Page 8: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 8

Sample Questions:

• Show how you would copy one array of characters (representing a “string”) into another, given:

char original_name[20];char copied_name[20];

(Answer:

strcpy(copied_name, original_name); )

Page 9: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 9

Sample Questions:

• Show how you would copy one array of characters (representing a “string”) into another, using a loop:

Answer: int len = strlen(original_name);for (int i=0; i<=len; ++i)

copied_name[i] = original_name[i];

Page 10: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 10

Think about:

• In the previous answer, understand why the following were used:– prefix increment versus postfix– strlen was used outside the loop instead of as

the test condition– how to turn the for loop into a while loop or a

do while loop– how to turn this into a function (called

string_copy)...what would the args look like?

Page 11: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 11

Think about:

• What is the difference between the following two statements:given:

char ch;

cin.get(ch);versus

cin >> ch;

Page 12: 1 CS161 Introduction to Computer Science Topic #17.

CS161 Topic #17 12

CS161 CS161

Introduction to Introduction to Computer ScienceComputer Science

What’s

CS162 Like?