1 / 31

CS1010 Discussion Group 11

CS1010 Discussion Group 11. Week 12 – Structured Structures. HELLO!. S lides are at http ://www.comp.nus.edu.sg/~yanhwa /. Lab 6. Last lab!. Ex 1. Practical Exam 2. See the example clearly. SDU388  SDU0388 Leading zeros are not printed out in the actual car plate! A few ways:

rbradwell
Télécharger la présentation

CS1010 Discussion Group 11

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. CS1010 Discussion Group 11 Week 12 – Structured Structures

  2. HELLO! Slides are at http://www.comp.nus.edu.sg/~yanhwa/

  3. Lab 6 Last lab!

  4. Ex 1 Practical Exam 2 • See the example clearly. SDU388  SDU0388 • Leading zeros are not printed out in the actual car plate! • A few ways: • Hard code and check the length and change the formula accordingly • 9 4 5 4 3 2  9 4 4 3 2  9 4 3 2  9 4 2 • Zero padding of 388 to 0388 (a bit complicated in C) • Use a weights array { 9 4 5 4 3 2 } then add a positive offset based on the difference in length (MAX_LEN – len)

  5. Ex 1 Practical Exam 2 • Check sample runs! When printing Invalid Car Type, don’t print about registration number anymore. • Don’t think too much. TT

  6. Ex 2 Practical Exam 2

  7. Ex 2 Practical Exam 2

  8. Structures Lecture Summary • typedefis a keyword used in C language to assign alternative names to existing types. • You are assigning “result_t” name to this user-defined struct type and you end with a semi-colon • Primitive types are int, char, long, float, double etc • You are defining your own “type”! Type is not a variable typedefstruct{ intstuNum; float score; char grade; } result_t;

  9. Structures Lecture Summary • Define your own types before function prototypes but after pre-processor directives • Then you can use it like as if it is yet another type (same like int, float, double etc) result1 and result2 are variables of type “result_t” typedefstruct{ intstuNum; float score; char grade; } result_t; result_t result1, result2;

  10. Structures Lecture Summary • Syntax to initialise is like array initialisation even for a “structure inside a structure” • Then access members of a structure using the dot (.) operator typedefstruct { int day, month, year; } date_t; typedefstruct { intcardNum; date_t birthday; } card_t; card_tcard1= {888888, {31, 12, 2020}}; card_t card2 = { 666666, {30, 6} }; card2.birthday.year = 2021;

  11. Structures Lecture Summary • Since a function can return a type, it can return a struct! • returned struct of type “result_t” from func is copied over to result variable which is also of type “result_t” • This way, you can return 2 or more values from a function using structs and don’t need to use pointers result_tfunc( ... ) { ... } result_t result; result = func( ... );

  12. Structures vs Arrays Lecture Summary • Structure name refers to the entire structure. Array name “decays” to a constant pointer to the address of the first element of the array • Unlike arrays we can do assignment with structures • result1 = result2 will really copy the data over by value • Now if you change result1.stuNum, It will not affect result2. result2.stuNum = result1.stuNum; result2.score = result1.score; result2.grade = result1.grade;

  13. Structures vs Arrays Lecture Summary • Problem occurs when struct contain pointers. This is a “shallow copy” or “member-wise copy” • The pointers are copied over but not the contents of the pointers. Both structs will now contain pointers to the same location. Any changes made via one pointer will be seen by the other pointer. (To copy over content of pointers will be called a “deep copy”) • For structs that contain arrays, the array contents are copied over to the other array in the other struct, element by element • Arrays cannot be “assigned” and the complier can only copy over the elements

  14. Structures - More Lecture Summary typedefstruct{ char name[12]; int age; char gender; } player_t; • Structures with strings (char arrays) • Same as before! It is a normal char array player_t player3; printf("Enter name, age and gender: "); scanf("%s %d %c", player3.name, &player3.age, &player3.gender);

  15. Structures - More Lecture Summary • When structures are passed into a function, • It is copied over just like an assignment result1 = result2 • Original struct variable will not be modified. You need to return the struct • Different from passing array into a function. • Array name is like a constant pointer to the first element while the structure name is the entire structure itself! • To modify the original struct, pass in the address of the struct to the function

  16. -> Structures -> Lecture Summary • Assume sptr is a pointer to some structure • The dot operator has higher precedence than the pointer deference operator • The following will try to access the thing pointed to by sptr.gpa, which does not work, because sptr is not a struct: • *sptr.gpa = 4.0; // Same as *(sptr.gpa) = 4.0; • The parenthesis operator can be used to override the precedence, but that is awkward: (*sptr).gpa = 4.0; • Instead a new operator is introduced, the "pointer to structure member reference operator", Arrow Operator • sptr->gpa = 4.0; // Same as (*sptr).gpa = 4.0;

  17. Tutorial 1 What to expect? Compilation error. Variable one cannot be assigned to two due to incompatible types, even though both one_t and two_t are defined with the same member types and names. But type “one_t” is just not type “two_t” no matter how it tries!!!

  18. Tutorial 2 More than one answer! The target is s.a. Since scanf() requires the address of the target, the argument for the scanf() function is hence &(s.a) Since dot operator has a higher precedence than the address operator, the parentheses can be omitted, to become &s.a

  19. Tutorial 3 Use structures to return 2 or more outputs from a function instead of pointers.

  20. Tutorial 3

  21. Tutorial 3 This result variable is a temp variable!

  22. Tutorial 4 More than one answer! the target is (*p).a The alternative way of writing is p->a The target is NOT *p.a because dot has a higher precedence than *, hence *p.a is equivalent to *(p.a). Since scanf() requires the address of the target, the argument for the scanf() function is hence &((*p).a) or &(p->a)

  23. Tutorial 5 How to read in data into the array of tile_t?

  24. Tutorial 5 scan_tiles = What is inside the for loop?

  25. Tutorial 5 Process the struct of type tile_t

  26. Tutorial 5 Find max and min cost at the same time

  27. Tutorial 6 Array of another struct in struct

  28. Tutorial 6 Array of another struct in struct

  29. Tutorial 6 Iterating through the struct array in the struct

  30. Tutorial 7 How to create the timeline? How to calculate the longest lesson? How to detect a free period? How to find a period with the most concurrent lessons?

  31. Tutorial 7 How to create the endpoints array? How to find the free periods? How to not include the gap in between zero and the first lesson, the gap between last lesson and the end of the array

More Related