1 / 12

EECE.2160 ECE Application Programming

EECE.2160 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2017 Lecture 27: Nested structures. Lecture outline. Announcements/reminders Program 6 graded; regrades due 11/29 Program 7 posted; due 11/20 Programs submitted 11/21-11/26 “ 1 day late ”

jwarnock
Télécharger la présentation

EECE.2160 ECE Application Programming

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. EECE.2160ECE Application Programming Instructor: Dr. Michael Geiger Fall 2017 Lecture 27: Nested structures

  2. Lecture outline • Announcements/reminders • Program 6 graded; regrades due 11/29 • Program 7 posted; due 11/20 • Programs submitted 11/21-11/26 “1 day late” • Late penalties begin increasing again on 11/27 • Today’s class • Structures and functions example • Nested structures • Start PE4 (Structures) ECE Application Programming: Lecture 27

  3. Review: Structures • User-defined types; example: typedef struct { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; } StudentInfo; • Can define variables of that type • Scalar: StudentInfo student1; • Array: StudentInfo classList[10]; • Pointer: StudentInfo *sPtr = &student1; • Access members using • Dot operator: student1.middle = ‘J’; • Arrow (if pointers): sPtr->GPA = 3.5; • Typically passed to functions by address ECE Application Programming: Lecture 27

  4. Review: Structures and functions • Can pass structures to functions • int f(StudentInfo s); • Structures consume significant memory • Usually much more efficient to simply pass pointer • int g(StudentInfo *p); • Access structure through pointer: -> operator • Handles dereferencing and field access • Example: p->GPA = 3.0; ECE Application Programming: Lecture 27

  5. Example: Structures and functions • Write the following functions that use the StudentInfo structure • Given a pointer to a single StudentInfo variable, print all of the student info to the screen using the following format: Michael J. Geiger ID #12345678 GPA: 1.23 • Given an array of StudentInfo variables and the size of the array, compute and return the average GPA of all students in the list • Prompt the user to enter 3 lines of input (using the format below), read the appropriate values into StudentInfo elements, and return a value of type StudentInfo • Format (user input underlined) Enter name: Michael J. Geiger Enter ID #: 12345678 Enter GPA: 1.23 ECE Application Programming: Lecture 27

  6. Example solution void printStudent(StudentInfo *s) { printf(“%s %c. %s\n”, s->first, s->middle, s->last); printf(“ID #%u\n”, s->ID); printf(“GPA %.2lf\n”, s->GPA); } ECE Application Programming: Lecture 27

  7. Example solution (cont.) double avgGPA(StudentInfo list[], int n) { int i; int sum = 0; for (i = 0; i < n; i++) sum += list[i].GPA; return sum / n; } ECE Application Programming: Lecture 27

  8. Example solution (cont.) StudentInfo readStudent() { StudentInfo s; printf(“Enter name: ”); scanf(“%s %c. %s”, s.first, &s.middle, s.last); printf(“Enter ID #: ”); scanf(“%u”, &s.ID); printf(“Enter GPA: ”); scanf(“%lf”, &s.GPA); return s; } ECE Application Programming: Lecture 27

  9. Nested structures • Structures can contain other structures: typedefstruct { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name } Name; typedefstruct { Name sname; // Student name unsigned int ID; // ID # double GPA; // Grade point } SINew; • Will need multiple dot operators to access field within nested structure • Given SINew s1; • s1.sname  Name structure within s1 • s1.sname.middle  middle initial of name within s1 ECE Application Programming: Lecture 27

  10. PE4 (Structures) • Given header files, main program • Complete specified functions • For the Name structure • void printName(Name *n): Print the name pointed to by n, using format <first> <middle>. <last> • void readName(Name *n): Prompt for and read a first, middle, and last name, and store them in the structure pointed to by n • SINew functions on next slide ECE Application Programming: Lecture 27

  11. Today’s exercise (continued) • Given header files, main program • Complete specified functions • Name functions on previous slide • For the SINewstructure • void printStudent(SINew *s): Print information about the student pointed to by s • void readStudent(SINew *s): Prompt for and read information into the student pointed to by s • void printList(SINew list[], int n): Print the contents of an array list that contains nStudentInfo structures • intfindByLName(SINew list[], int n, char lname[]): Search for the student with last name lname in the array list. Return the index of the structure containing that last name, or -1 if not found • intfindByID(SINew list[], int n, unsigned intsID): Search for the student with ID # sID in the array list. Return the index of the structure containing that last name, or -1 if not found ECE Application Programming: Lecture 27

  12. Next time • Finish PE4 • File I/O • Reminders: • Program 6 graded; regrades due 11/29 • Program 7 posted; due 11/20 • Programs submitted 11/21-11/26 “1 day late” • Late penalties begin increasing again on 11/27 ECE Application Programming: Lecture 27

More Related