Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructors: Dr. Lin Li & Dr. Michael Geiger Spring 2019 Lecture 29 Structures (continued)
2
ECE Application Programming: Lecture 30
Lecture outline Announcements/reminders Program 7 due Wed, 4/24 Remaining programs Program 8 to be due Friday, 5/3 (last day of classes) Program 9 (extra credit) to be due during finals; will not accept late submissions Today’s lecture Structures as function arguments—examples Nested structures 7/9/2019 ECE Application Programming: Lecture 30
3
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; 7/9/2019 ECE Application Programming: Lecture 30
4
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 # 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 #: Enter GPA: 1.23 7/9/2019 ECE Application Programming: Lecture 30
5
ECE Application Programming: Lecture 30
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); } 7/9/2019 ECE Application Programming: Lecture 30
6
Example solution (cont.)
double avgGPA(StudentInfo list[], int n) { int i; double sum = 0; for (i = 0; i < n; i++) sum += list[i].GPA; return sum / n; } 7/9/2019 ECE Application Programming: Lecture 30
7
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; } 7/9/2019 ECE Application Programming: Lecture 30
8
ECE Application Programming: Lecture 30
Nested structures Structures can contain other structures: typedef struct { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name } Name; Name sname; // Student name unsigned int ID; // ID # double GPA; // Grade point } SINew; 7/9/2019 ECE Application Programming: Lecture 30
9
Nested structure accesses
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 Structure pointer typically only to top-level structure Given SINew *sp = &s1; sp->sname Name structure within s1 sp->sname.middle middle initial of name within s1 7/9/2019 ECE Application Programming: Lecture 30
10
ECE Application Programming: Lecture 30
PE3 (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 7/9/2019 ECE Application Programming: Lecture 30
11
Today’s exercise (continued)
Given header files, main program Complete specified functions Name functions on previous slide For the SINew structure 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 n StudentInfo structures int findByLName(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 int findByID(SINew list[], int n, unsigned int sID): 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 7/9/2019 ECE Application Programming: Lecture 30
12
ECE Application Programming: Lecture 30
Final notes Next time File I/O (Lecture 32) Reminders: Program 7 due Wed, 4/24 Remaining programs Program 8 to be due Friday, 5/3 (last day of classes) Program 9 (extra credit) to be due during finals; will not accept late submissions 7/9/2019 ECE Application Programming: Lecture 30
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.