Docsity

CGS 3460 Lecture 22: Arrays and Type Conversion, Quizzes of Computer Science

Partial preview of the text

Download CGS 3460 Lecture 22: Arrays and Type Conversion and more Quizzes Computer Science in PDF only on Docsity!

ArraysCGS 3460, Lecture 22Mar 1, 2006Hen-I Yang

Reminder „

Quiz 3 will be held on Mar 8th (Wed) instead ofMar 10th (Fri).

„

Quiz 3 includes homework 3, textbook up to Sec7.1, and slides up to last Friday (2/24)

„

Always put return 0 as the last statement in yourcode

„

TA will post a couple sample codes to help youlearn nested loop.

„

It is strongly discouraged to simply use hundredsof statements for Homework 3.

Hint: Do you really need to keep all the data around?

Previously… „

int „ float „ char „ Sizeof() „ type conversion

Agenda „

Types „ Arrays „ One-dimension array „ Multi-dimension arrays

Type Conversion „

Allows mixing of basic types.

„

Compiler takes care of the dirty work. (implicitconversions)

„

Coercion

happens when (usual arithmetic conversions)

operands are of different types

right hand side does not match result of the left handside

argument declaration and invocation

return type

Problem with Type Conversion „

int Æ float

may lose precision

float Æ int

not a good idea

long Æ int

overflow problem

Casting (Explicit Conversion) (type-name)

expression „

Forced type conversion

„

Useful, but also dangerous

Use of casting to get the value you want

Use of casting to avoid overflow „

Casting allows programmers to avoid therestriction setup by type system, but also exposeto all sort of vulnerability because of the removalof such restrictions

typedef Typedef int Bool; „

Define new type

„

After typedef, new type can be used just likebasic type

„

Can define a type with a more proper name(more informative)

„

Enhance portability

„

More powerful than #define macro,

Change once, effective all

can be used anywhere as synonym of original type

Obey scope rule

More about Arrays „

0-based index „ Out of bound problem „ Traversing through elements „ Array initialization „ sizeof(a)/sizeof(a[0]) „ Supermarket problem (problem 2) inhomework 2

Summary „

More about Types „ Array „ One-dimension array