Lecture #15

November 5, 1999

Bob Pitha

bpitha@cs.ucsd.edu

http://www-cse.ucsd.edu/classes/fa99/cse5a

let’s talk about the quiz

•      Stats:

–   average score: 15.5

–   each question got answered correctly by someone

question 1

Text Box: Which of the following are valid identifiers, and which are not? 
For those that are not, give a brief reason.
a. AP&M
b. Sam_I_am
c. I-am-Sam
d. Double
e. _CSE5A_
 


•      Remember the rules for identifiers:

–   Letters, digits, and underscore

–   Can’t start with a digit

let’s talk a little more about identifiers

•      Identifiers are just names you can use in your program

–   they’re kind of a general-purpose set of rules for naming things

•   variables

•   functions

•   new data types

•  

We haven’t covered these yet

 
data structures

•   symbolic constants

valid identifiers

•      There’s a difference between a legal identifier and one that can be used in a particular context

–   legal means it just passes the simple rules for identifiers

–   useable means the compiler will let you use it

•      The main reason a legal identifier may not be used is that it’s already used in a way that would conflict

conflicting names

•      For example, main is a perfectly valid identifier

–   it’s all letters

–   it doesn’t begin with a digit

•      But I can’t name a variable main, because it would conflict with the function named main.

•      Similarly, when your book says you shouldn’t name variables starting with an underscore, it’s not because it’s not a legal identifier

–   it’s just that sometimes you’ll find that the name has already been used

so back to question 1…

Text Box: Which of the following are valid identifiers, and which are not? 
For those that are not, give a brief reason.
a. AP&M		cannot contain &
b. Sam_I_am
c. I-am-Sam
d. Double
e. _CSE5A_
 


so back to question 1…

Text Box: Which of the following are valid identifiers, and which are not? 
For those that are not, give a brief reason.
a. AP&M		cannot contain &
b. Sam_I_am
c. I-am-Sam
d. Double
e. _CSE5A_

 


•      It is perfectly legal to mix upper and lower case

–   case-sensitive means that 2 identifiers are considered different even if they only differ by the case of one or more letters (i.e., i vs. I)

so back to question 1…

Text Box: Which of the following are valid identifiers, and which are not? 
For those that are not, give a brief reason.
a. AP&M		cannot contain &
b. Sam_I_am
c. I-am-Sam		cannot contain -
d. Double
e. _CSE5A_

 


so back to question 1…

Text Box: Which of the following are valid identifiers, and which are not? 
For those that are not, give a brief reason.
a. AP&M		cannot contain &
b. Sam_I_am
c. I-am-Sam		cannot contain -
d. Double
e. _CSE5A_

 


•      There is a data type named double, which means it is a keyword that cannot be used as a variable

–   but it’s still a valid identifier

–   anyway, this is Double, not double.

so back to question 1…

Text Box: Which of the following are valid identifiers, and which are not? 
For those that are not, give a brief reason.
a. AP&M		cannot contain &
b. Sam_I_am
c. I-am-Sam		cannot contain -
d. Double
e. _CSE5A_

 


•      This is a legal identifier

–   just as your book says, it’s a good idea to avoid identifiers that begin with a single underscore

question 2

Text Box: 2. How many bits are in a byte on most common computers?
8
 


•      There are some special-purpose computers that have other byte sizes, but these are rare. All common modern computers use an 8-bit byte.

question 3

Text Box: 3. We have talked about 7 basic data types. List them.
	char
	short (or short int)
	int
	long (or long int)
	float
	double
	long double
 


question 4

 

 

Text Box: 4. Computers store numbers in binary (base 2), where each digit represents a successive power of 2. We sometimes use hexadecimal (base 16), to  represent numbers when working with computers. Fill in this table by converting the supplied number to the other representations.

decimal

binary

hexadecimal

10

1010

 

 

1111

 

 

 

2c

 

question 4

decimal

binary

hexadecimal

10

1010

 

 

1111

 

 

 

2c

 

•      Let’s look at these numbers that are already filled in…

–   so first, let’s review what we know about binary


 


question 4 – binary review

•      So 1010 in binary is:

1*23 + 0*22 + 1*21 + 0*20

8 + 2

10

question 4

decimal

binary

hexadecimal

10

1010

 

15

1111

 

 

 

2c

 

•      Similarly, 1111 in binary is:

1*23 + 1*22 + 1*21 + 1*20

8 + 4 + 2 + 1

15

question 4

decimal

binary

hexadecimal

10

1010

a

15

1111

 

 

 

2c

 

•      Now for some hex:

–   remember the hex digits are

1 2 3 4 5 6 7 8 9 a b c d e f

•   where a-f represent 10-15

–   so this first one is easy: 10 in hex is a

question 4

decimal

binary

hexadecimal

10

1010

a

15

1111

f

 

 

2c

 

•      And 15 is just f

question 4

decimal

binary

hexadecimal

10

1010

a

15

1111

f

44

 

2c

 

•      Now going from hex to decimal:

Text Box: hn hn-1 hn-2 … h2 h1 h0
hn΄16n + hn-1 ΄ 16n-1 + hn-2 ΄ 16n-2… h2 ΄ 162 + h1 ΄ 161 + h0 ΄ 160

 


–   2c is:

   2*16 + c

   32 + 12

   44

question 4

decimal

binary

hexadecimal

10

1010

a

15

1111

f

44

 

2c

•      Now the hard one: convert 44 to binary

–   this essentially means expressing 44 as a sum of powers of 2

•   so what powers of 2 can we sum up to make 44?

–   What’s the largest power of 2 that is less than 44?

•   32 (25)

–   in binary, therefore, 32 is 100000

question 4

decimal

binary

hexadecimal

10

1010

a

15

1111

f

44

 

2c

 

•      So 44 is 25 + 12

–   so we need to express 12 as a sum of powers of 2

–   what’s the largest power of 2 that is less than 12?

•   8 (23)

•      So 44 is 25 + 23 + 4

question 4

decimal

binary

hexadecimal

10

1010

a

15

1111

f

44

101100

2c

 

•      So 44 is 25 + 23 + 4

–   so we need to express 4 as a sum of powers of 2

–     4 is just 22

•       So 44 is 25 + 23 + 22

–   which means in binary, 44 is 101100

•     that is, 1*25 + 0*24 + 1*23 + 1*22 + 0*21 + 0*20

a comment about conversions

•      What’s the point of this?

–   these were simple numbers.

–   it helps to know simple hex/binary/decimal numbers

extra credit

Text Box: Believe it or not, hexadecimal is used as a convenience. Can you tell me why hexadecimal is a convenient way to represent binary numbers?
 


•      It turns out that when the order of one base is a power of the order of another (i.e., 16 is 24) it is very convenient to convert between them

–   you only have to look at each group of 4 bits

•   4 bits represents a number from 0 to 15

•   1 hex digit represents a number from 0 to 15

question 5

Text Box: 6. Is this a legal expression?
	'a' + 'b'
Briefly justify your answer
 


•      This is a perfectly legal expression

–   ‘a’ and ‘b’ are character constants

–   remember that characters are stored as numbers

–   so ‘a’ and ‘b’ are simply integers, and they can be added together

homework #3

•      Should be on the web page sometime today

•      This is the first real program you get to write

–   a program to print out a Fibonacci series

•   0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

–   Your program should prompt the user for the number of elements to print

•   I’ve given you the code to use to read the number in from the keyboard