1 / 21

Week 6

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.

marrim
Download Presentation

Week 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 6 After CS50… Before CS50… Jordan Jozwiak

  2. Quiz 0 • Thoughts? • Statistics • Mean: 73 • Median: 75 • StdDev: 15

  3. This Week • Hexadecimal • Enumerated Types • Structs • File I/O

  4. 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!

  5. 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!

  6. 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

  7. 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.

  8. 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; • }

  9. 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); • ... • }

  10. 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!

  11. 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.

  12. Struct • Can access fields within a struct: • NameOfStruct.NameOfField • Can access fields from a pointer to a struct: • Pointer->NameOfField • (*struct).NameOfField

  13. Structs structpkmn { char* name; char* type; int hp; };

  14. 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

  15. Structs structpkmncharmander; charmander.name = GetString(); charmander.type = GetString(); charmander.hp = 40;

  16. Struct // no longer need to // write ‘structpkmn’! pkmnmudkip; typedef struct { char *name; char* type; int hp; } pkmn;

  17. 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!

  18. 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

  19. File I/O • Must always open a file before reading from or writing to it. • Should always close a file whenever you open one!

  20. File I/O • fopen returns a FILE*, a pointer to a ‘file handle’. • We use the FILE* to access and close our file.

  21. Coding time!! :D • In the CS50 Appliance… • cloud.cs50.net/~jjozwiak

More Related