Presentation is loading. Please wait.

Presentation is loading. Please wait.

EECE.2160 ECE Application Programming

Similar presentations


Presentation on theme: "EECE.2160 ECE Application Programming"— Presentation transcript:

1 EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2017 Lecture 28: PE4: Structures

2 ECE Application Programming: Lecture 28
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 Review: Nested structures PE4: Structures 4/4/2019 ECE Application Programming: Lecture 28

3 Review: 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; 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 4/4/2019 ECE Application Programming: Lecture 28

4 ECE Application Programming: Lecture 28
Today’s exercise 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 4/4/2019 ECE Application Programming: Lecture 28

5 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 4/4/2019 ECE Application Programming: Lecture 28

6 ECE Application Programming: Lecture 28
Name functions void printName(Name *n) { printf("%s %c. %s\n", n->first, n->middle, n->last); } void readName(Name *n) { printf("Enter name: "); scanf("%s %c. %s", n->first, &n->middle, n->last); 4/4/2019 ECE Application Programming: Lecture 28

7 Single SINew functions
void printStudent(SINew *s) { printName(&s->sname); printf("ID #%.8u\n", s->ID); printf("GPA: %.2lf\n", s->GPA); } 4/4/2019 ECE Application Programming: Lecture 28

8 Single SINew functions (cont.)
void readStudent(SINew *s) { readName(&s->sname); printf("Enter ID #: "); scanf("%u", &s->ID); printf("Enter GPA: "); scanf("%lf", &s->GPA); } 4/4/2019 ECE Application Programming: Lecture 28

9 ECE Application Programming: Lecture 28
printList() void printList(SINew list[], int n) { int i; // Loop index for (i = 0; i < n; i++) { printStudent(&list[i]); printf("\n"); } } 4/4/2019 ECE Application Programming: Lecture 28

10 ECE Application Programming: Lecture 28
findByLName() int findByLName(SINew list[], int n, char lname[]) { int i; // Loop index // Search for student with matching name // in list for (i = 0; i < n; i++) { if (strcmp(lname, list[i].sname.last) == 0) return i; } // If end of loop reached, student wasn’t // found return -1; 4/4/2019 ECE Application Programming: Lecture 28

11 ECE Application Programming: Lecture 28
findByID() int findByID(SINew list[], int n, unsigned int sID) { int i; // Loop index // Search for student with matching ID in list for (i = 0; i < n; i++) { if (sID == list[i].ID) return i; } // If end of loop reached, student wasn’t // found return -1; 4/4/2019 ECE Application Programming: Lecture 28

12 ECE Application Programming: Lecture 28
Next time 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 4/4/2019 ECE Application Programming: Lecture 28


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google