1 homework –continue reading k&r chapter 2 –we’ll go over hw2 at end of class today...

21
1 Homework • Homework – Continue Reading K&R Chapter 2 – We’ll go over HW2 at end of class today – Continue working on HW3 • Questions?

Upload: trevor-spencer

Post on 18-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

1

Homework

• Homework– Continue Reading K&R Chapter 2– We’ll go over HW2 at end of class today– Continue working on HW3

• Questions?

Page 2: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

2

Forcing Groups of Bits Off• Given char n, how to turn off all bits except

the least significant 5 bits: n = n & ‘\x1f’

n = ‘\xa5’ 10100101

n & ‘\x1f’ 10100101

& 00011111 turn off all bits

00000101 except bottom 5

• Called "masking" the bits -- only see bits on in result where 1's found in mask value

Page 3: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

3

Forcing Groups of Bits Off

• x = x & ~077 (octal for a change)

Sets least significant 6 bits in x to 0

Even if you don't know size of x (e.g. size of int) ~077 = ~00 . . . 00111111

= 11 . . . 11000000 of required size

Extends itself with 1 bits on left for length of x

Page 4: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

4

Forcing Groups of Bits On

• Given n, how to turn on the MS two bits (if already on, leave on). n = n | ‘\xc0’

n = '\xa5'

n | '\xc0': 10100101

| 11000000 turn on MS 2 bits

11100101

Page 5: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

5

“Encryption” with Exclusive Or

• Show that x ^ (x ^ y) == y

char y =‘\xa5’ 10100101 (plain text bits)

char x =‘\x69’ 01101001 (encryption key)

x ^ y 11001100 (cypher text bits)

x ^ (x ^ y) 10100101 (decrypted bits)

Same as original value of y!

Page 6: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

6

Exchanging Data with Exclusive Or

• You can use exclusive or to exchange the values of two variables without using a temporary location for a “three way move”int i = 0x12, j = 0x34;

i = i ^ j; /* i = 0x26 */

j = i ^ j; /* j = 0x12 */

i = i ^ j; /* i = 0x34 */

Page 7: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

7

String Constants• String constant: "I am a string.“

– An array (a pointer to a string) of char values somewhere ending with NUL = '\0', the char with zero value.

– "0" is not same as '0'. The value "0" can't be used in an expression - only in arguments to functions like printf().

• Also have string functions:

See pg. 241, Appendix B and B3, pg. 249

#include <string.h>

With these definitions, can use: len = strlen(msg);

where msg is string in a string array

• NOTE: Write your own string functions for homework!!

Page 8: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

8

Enumeration Symbolic Constants

• enum boolean {FALSE, TRUE};Enumerated names assigned values starting from 0

• FALSE = 0

• TRUE = 1

• Now can declare a variable of type enum boolean:enum boolean x;

x = FALSE; 

• Just a shorthand for creating symbolic constants instead of with #define statements

Page 9: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

9

Enumeration Symbolic Constants

• If you define months as enum type

enum months {ERR, JAN, FEB, MAR,

APR, MAY, JUN, JUL,

AUG, SEP, OCT, NOV,

DEC};

enum months birthMonth = MAY;

printf("%d\n", birthMonth); /* prints 5 */

Page 10: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

10

const• "const" declaration is like "final" in Java

– warns compiler variable value shouldn't change– Commonly used for function arguments

• const char msg[ ] = "Warning: . . .";

• int copy(char to[ ], const char from[ ]);

• If logic of copy function attempts to modify “from” string, compiler will give a warning

• Exact form of warning and actual behavior of the code is installation defined

Page 11: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

11

Operators• Arithmetic Operators:

+ Add- Subtract* Multiply/ Divide% Remainder after division (modulo)

• Examples: int x, y; x / y truncates (no fractional part) x % y is the remainder when x is divided by y. Always true that: x == y*(x/y) + x%y

Page 12: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

12

Operators• Logical Operators:

&& logical and|| logical or! Not

• Examples:int year; if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)

printf( "%d is a leap year\n", year); else

printf( "%d is not a leap year\n", year);Why are inner parentheses actually not needed?

See precedence table, P 53. Good to mark for exams!!

Page 13: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

13

Relations / Comparisons

• We call a comparison between two arithmetic expressions a "relation" ae1 <= ae2 (Comparisons: <, <=, ==, !=, >=, > )

• A relation is evaluated as true or false (1 or 0) based on values of given arithmetic expressions

• if ( i < lim-1 == j < k) – What's it mean? – See precedence table P 53

• Instead of c != EOF, could write !(c == EOF)

Page 14: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

14

Type Conversion/* atoi: convert character string of digits to int (base 10) */int atoi(char s[ ]) /* name based on “ascii to integer”

*/{

int i, n;n = 0;for (i=0; s[i] >= '0' && s[i] <= '9'; ++i) /* "is a digit" */ n = 10*n + (s[i] - '0'); /* s[i]-'0' is char

add to 10*n is int */return n;

}

Page 15: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

15

Type Conversion

/* itoa: convert int n to characters in base 10 in array s */

void itoa (int n, char s[ ])

{

int i, sign;

if ((sign = n) < 0) /* record sign */

n = -n; /* make n positive */

i = 0;

Page 16: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

16

Type Conversion/* generate digits in reverse order */do { /* new loop type */ s[i++] = n % 10 + '0'; /* generate next digit */

/* what conversion takes place here? */} while(( n /= 10) > 0); /* delete digit from end of int */if (sign < 0) s[i++] = '-';s[i] = '\0';reverse (s); /* reverse digit string */

}

Page 17: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

17

Review HW2 Solution

• Let’s go over solutions to HW2– Learn to break down the problem logically – Learn to write pseudo code effectively– Learn to write C code from pseudo code

• These are key for being a good programmer

Page 18: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

18

trim

• Pseudo code for the trim programwhile there is still a line to process

for each character starting at the end of the line

find the first non blank character or the beginning of the line

if a non-blank character is found

add an EOL and a terminating zero

and print out the string

Page 19: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

19

trim

• While writing the pseudo code, think about:– A good strategy (e.g. scan line from the end

backwards rather than trying to go forwards)– Stopping each scan (e.g. not blank, not tab, etc)– What’s needed to make a shorter line printable

(e.g. add a new EOL and terminating zero)– What indicates that it is time to stop looping on

input lines (e.g. return from getline > 0 is false)

Page 20: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

20

reverse• Pseudo code for the reverse function

find length of the string to reverse

for each character from the beginning of the string to half way

copy the character here to a holding variable

copy the character the same distance from end to here

copy the holding variable to the same distance from end

Holding VariableString Array

Page 21: 1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?

21

reverse

• While writing the pseudo code, think about:– The basic steps needed (e.g., three way move)– The loop conditions (e.g., go halfway through)– The beginning/end as possible special cases

• What about a string that has an odd length?

• What about a string that has an even length?

– Does it matter if we move the middle character in an odd length string out and back in or not?