week 6 jordan jozwiak after cs50… before cs50…. quiz 0 o thoughts? o statistics o mean: 73 o...
TRANSCRIPT
Week 6Jordan Jozwiak
After CS50…Before CS50…
Quiz 0O Thoughts?
O StatisticsO Mean: 73O Median: 75O Std Dev: 15
This WeekO HexadecimalO Enumerated TypesO StructsO File I/O
Hexadecimal NumbersO Hexadecimal is a way of
representing numbers in base 16, with each digit ranging from 0 – F.
O We usually represent numbers in base 10 and in binary we represent numbers in base 2, this is just one more way!0 1 2 3 4 5 6 7 8 9 1
011
12
13
14
15
0 1 2 3 4 5 6 7 8 9 A B C D E F
Hexadecimal NumbersO Hexadecimal is useful because it
gives us a concise way of representing values in memory.
O Every set of 4 bits (a ‘nibble’, or half a ‘byte’) can be represented by 1 hex digit!
1 0 0 1 1 1 1 0 0 1 1 1 0 0 0 1
9 E 7 1
Hexadecimal NumbersO Notationally, we write hexadecimal
numbers preceded by ‘0x’, which simply denotes that what follows is a hexadecimal number.
O The below value might be written: 0x9E71
1 0 0 1 1 1 1 0 0 1 1 1 0 0 0 1
9 E 7 1
Enumerated Types
O Allows us to create our own type with a finite set of possible values.
O Abstraction makes code easier to understand and work with.
Enumerated TypesExamples of Possible Finite Sets:O {WIN, LOSE, DRAW}O {YES, NO, MAYBE}O {SMALL, MEDIUM, LARGE, XL}O {TALL, VENTI, GRANDE}O {WINDOWS, MAC_OS, LINUX}
intmain(){
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 typedefsenum 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…typedef enum {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
O Structs provide a way of bundling together related values.
O Sets of ‘fields’ – variables each having their own distinct data types.
O Useful when we want to keep track of multiple related properties.
Struct
O Can access fields within a struct:O NameOfStruct.NameOfField
O Can access fields from a pointer to a struct:O Pointer->NameOfFieldO (*struct).NameOfField
Structs
struct pkmn{ char* name; char* type; int hp;};
Structs “.” vs “->”typedef struct 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->daydate.tomorrowp->day
Structs
struct pkmn charmander;charmander.name = GetString();charmander.type = GetString();charmander.hp = 40;
Structtypedefstruct {
char *name;char* type;int hp;
} pkmn;
// no longer need to
// write ‘struct pkmn’!
pkmn mudkip;
File I/O
O We are accustomed to reading from and writing to the terminal.
O More formally, we read from stdin, write to stdout.O We can also read from and write to files!
File I/OO fopen – open a fileO fclose – close a fileO fread – read from a fileO fwrite – write from a fileO fseek - move to a particular point in a file
File I/OO Must always open a file before
reading from or writing to it.O Should always close a file whenever
you open one!
File I/OO fopen returns a FILE*, a pointer to a
‘file handle’. O We use the FILE* to access and close
our file.
Coding time!! :DO In the CS50 Appliance…
O cloud.cs50.net/~jjozwiak