210 likes | 370 Views
Week 6. After CS50…. Before CS50 …. Jordan Jozwiak. Quiz 0. Thoughts? Statistics Mean: 73 Median : 75 Std Dev : 15. This Week. Hexadecimal Enumerated Types Structs File I/O. Hexadecimal Numbers.
E N D
Week 6 After CS50… Before CS50… Jordan Jozwiak
Quiz 0 • Thoughts? • Statistics • Mean: 73 • Median: 75 • StdDev: 15
This Week • Hexadecimal • Enumerated Types • Structs • File I/O
Hexadecimal Numbers • Hexadecimal is a way of representing numbers in base 16, with each digit ranging from 0 – F. • We usually represent numbers in base 10 and in binary we represent numbers in base 2, this is just one more way!
Hexadecimal Numbers • Hexadecimal is useful because it gives us a concise way of representing values in memory. • Every set of 4 bits (a ‘nibble’, or half a ‘byte’) can be represented by 1 hex digit!
Hexadecimal Numbers • Notationally, we write hexadecimal numbers preceded by ‘0x’, which simply denotes that what follows is a hexadecimal number. • The below value might be written: 0x9E71
Enumerated Types • Allows us to create our own type with a finite set of possible values. • Abstraction makes code easier to understand and work with.
Enumerated Types Examples of Possible Finite Sets: • {WIN, LOSE, DRAW} • {YES, NO, MAYBE} • {SMALL, MEDIUM, LARGE, XL} • {TALL, VENTI, GRANDE} • {WINDOWS, MAC_OS, LINUX} • int • main() • { • enum { SUCCESS, FAILURE }; • return SUCCESS; • }
enum day • { • SUNDAY, • MONDAY, • TUESDAY, • WEDNESDAY, • THURSDAY, • FRIDAY, • SATURDAY • }; • enum day get_next_day(enum day d); • enum day get_today(); • int main() • { • enum day today, tomorrow; • today = get_today(); • tomorrow = get_next_day(today); • ... • }
Enum with typedefs enum suit {HEARTS, CLUBS, DIAMONDS, SPADES}; enum suit mySuit1 = CLUBS; enum suit mySuit2 = DIAMONDS; enum suit mySuit3 = HEARTS; but it’s slightly more economical to write… typedefenum {HEARTS, CLUBS, DIAMONDS, SPADES} suit; suit mySuit1 = CLUBS; suit mySuit2 = DIAMONDS; suit mySuit3 = SPADES; Consider how well this would work with a switch statement!
Structs • Structs provide a way of bundling together related values. • Sets of ‘fields’ – variables each having their own distinct data types. • Useful when we want to keep track of multiple related properties.
Struct • Can access fields within a struct: • NameOfStruct.NameOfField • Can access fields from a pointer to a struct: • Pointer->NameOfField • (*struct).NameOfField
Structs structpkmn { char* name; char* type; int hp; };
Structs “.” vs “->” • typedefstruct date • { • int year; • int month; • int day; • struct date *tomorrowp; • } • date; • If datep were a pointer to the struct: • date * datep = malloc(sizeof(date)); • datep->tomorrowp->day • date.tomorrowp->day
Structs structpkmncharmander; charmander.name = GetString(); charmander.type = GetString(); charmander.hp = 40;
Struct // no longer need to // write ‘structpkmn’! pkmnmudkip; typedef struct { char *name; char* type; int hp; } pkmn;
File I/O • We are accustomed to reading from and writing to the terminal. • More formally, we read from stdin, write to stdout. • We can also read from and write to files!
File I/O • fopen – open a file • fclose – close a file • fread – read from a file • fwrite – write from a file • fseek - move to a particular point in a file
File I/O • Must always open a file before reading from or writing to it. • Should always close a file whenever you open one!
File I/O • fopen returns a FILE*, a pointer to a ‘file handle’. • We use the FILE* to access and close our file.
Coding time!! :D • In the CS50 Appliance… • cloud.cs50.net/~jjozwiak