week 6
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/1.jpg)
Week 6Jordan Jozwiak
After CS50…Before CS50…
![Page 2: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/2.jpg)
Quiz 0O Thoughts?
O StatisticsO Mean: 73O Median: 75O Std Dev: 15
![Page 3: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/3.jpg)
This WeekO HexadecimalO Enumerated TypesO StructsO File I/O
![Page 4: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/4.jpg)
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
![Page 5: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/5.jpg)
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 19 E 7 1
![Page 6: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/6.jpg)
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 19 E 7 1
![Page 7: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/7.jpg)
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.
![Page 8: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/8.jpg)
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;
}
![Page 9: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/9.jpg)
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);...
}
![Page 10: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/10.jpg)
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!
![Page 11: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/11.jpg)
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.
![Page 12: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/12.jpg)
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
![Page 13: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/13.jpg)
Structsstruct pkmn{ char* name; char* type; int hp;};
![Page 14: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/14.jpg)
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
![Page 15: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/15.jpg)
Structsstruct pkmn charmander;charmander.name = GetString();charmander.type = GetString();charmander.hp = 40;
![Page 16: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/16.jpg)
Structtypedefstruct {
char *name;char* type;int hp;
} pkmn;
// no longer need to
// write ‘struct pkmn’!
pkmn mudkip;
![Page 17: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/17.jpg)
File I/OO 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!
![Page 18: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/18.jpg)
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
![Page 19: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/19.jpg)
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!
![Page 20: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/20.jpg)
File I/OO fopen returns a FILE*, a pointer to a
‘file handle’. O We use the FILE* to access and close
our file.
![Page 21: Week 6](https://reader036.vdocument.in/reader036/viewer/2022081514/568161ea550346895dd21a05/html5/thumbnails/21.jpg)
Coding time!! :DO In the CS50 Appliance…
O cloud.cs50.net/~jjozwiak