1 / 13

EECE.2160 ECE Application Programming

In this lecture, Dr. Michael Geiger covers nested structures and file usage with structures. The lecture also includes an exercise to complete specified functions for the Name and SINew structures.

vwatson
Download Presentation

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 2016 Lecture 28: PE4: Structures

  2. Lecture outline • Announcements/reminders • Program 7 due today • Program 4 & 5 regrade deadline: Friday, 11/18 • Program 8 to be posted; due 11/28 • Only 9 programs this term • Tentative due dates: 11/28 (P8) and 12/7 (P9) • All 9 programs will count toward your grade • Lowest score will not be dropped • Review • Nested structures • Today’s class • PE4 (Structures) ECE Application Programming: Lecture 28

  3. Review: 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 28

  4. Review: File usage with structures • Each structure has its own .h/.c files • .h file (i.e., Name.h, SINew.h) contains structure definition and prototypes of related functions • .c file (i.e., Name.c, SINew.c) contains function definitions • To use structure type and/or functions, include .h file • SINew.h would include Name.h • Allows Name variable inside SINew structures • Main program could include both … • … although that’s redundant—including SINew.h implicitly includes Name.h ECE Application Programming: Lecture 28

  5. 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 ECE Application Programming: Lecture 28

  6. 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(StudentInfo list[], int n): Print the contents of an array list that contains nStudentInfo structures • intfindByLName(StudentInfo 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(StudentInfo 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 28

  7. Name functions voidprintName(Name *n) { printf("%s %c. %s\n", n->first, n->middle, n->last); } voidreadName(Name *n) { printf("Enter name: "); scanf("%s %c. %s", n->first, &n->middle, n->last); } ECE Application Programming: Lecture 27

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

  9. 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); } ECE Application Programming: Lecture 27

  10. printList() voidprintList(SINewlist[], intn) { inti; // Loop index for (i = 0; i < n; i++) { printStudent(&list[i]); printf("\n"); } } ECE Application Programming: Lecture 27

  11. 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; } ECE Application Programming: Lecture 27

  12. findByID() int findByID(SINew list[], int n, unsignedint 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; } ECE Application Programming: Lecture 27

  13. Next time • Finish PE4 • Dynamic memory allocation • Reminders: • Program 7 due today • Program 4 & 5 regrade deadline: Friday, 11/18 • Program 8 to be posted; due 11/28 • Only 9 programs this term • Tentative due dates: 11/28 (P8) and 12/7 (P9) • All 9 programs will count toward your grade • Lowest score will not be dropped ECE Application Programming: Lecture 28

More Related