EECE.2160 ECE Application Programming Instructors: Dr. Michael Geiger & Peilong Li Spring 2016 Lecture 26: Structures (cont.)
ECE Application Programming: Lecture 26 Lecture outline Announcements/reminders All remaining regrade deadlines: end of semester (4/29) Program 7 due 4/11 Only 9 programs this term Tentative due dates: 4/20 (P8) and 4/29 (P9) All 9 programs will count toward your grade Lowest score will not be dropped Today’s class Finish structures 4/6/2019 ECE Application Programming: Lecture 26
ECE Application Programming: Lecture 26 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; Access members using Dot operator: student1.middle = ‘J’; Arrow (if pointers): sPtr->GPA = 3.5; Typically passed to functions by address 4/6/2019 ECE Application Programming: Lecture 26
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 *s); 4/6/2019 ECE Application Programming: Lecture 26
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 4/6/2019 ECE Application Programming: Lecture 26
ECE Application Programming: Lecture 26 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); } 4/6/2019 ECE Application Programming: Lecture 26
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; } 4/6/2019 ECE Application Programming: Lecture 26
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; } 4/6/2019 ECE Application Programming: Lecture 26
ECE Application Programming: Lecture 26 Nested structures Variables inside structures can be structures themselves Example: general structure for names typedef struct { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name } Name; 4/6/2019 ECE Application Programming: Lecture 26
Nested structures (cont.) Example (cont.): Could use Name structure any time you need name within a structure Redefining StudentInfo: typedef struct { Name stu_name; // Student name unsigned int ID; // ID # double GPA; // Grade point } StudentInfo; Will need multiple dot operators to access field within nested structure Given StudentInfo s1; s1.stu_name Name structure within s1 s1.stu_name.middle middle initial of name within s1 4/6/2019 ECE Application Programming: Lecture 26
ECE Application Programming: Lecture 26 Next time PE4 (Structures) Reminders: Program 7 due 4/11 Only 9 programs this term Tentative due dates: 4/20 (P8) and 4/29 (P9) All 9 programs will count toward your grade Lowest score will not be dropped 4/6/2019 ECE Application Programming: Lecture 26