EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
Advertisements

ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2017 Lecture 27: Nested structures

ECE Application Programming: Lecture 27 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) 2/17/2019 ECE Application Programming: Lecture 27

ECE Application Programming: Lecture 27 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 2/17/2019 ECE Application Programming: Lecture 27

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; 2/17/2019 ECE Application Programming: Lecture 27

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 2/17/2019 ECE Application Programming: Lecture 27

ECE Application Programming: Lecture 27 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); } 2/17/2019 ECE Application Programming: Lecture 27

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; } 2/17/2019 ECE Application Programming: Lecture 27

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; } 2/17/2019 ECE Application Programming: Lecture 27

ECE Application Programming: Lecture 27 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 2/17/2019 ECE Application Programming: Lecture 27

ECE Application Programming: Lecture 27 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 2/17/2019 ECE Application Programming: Lecture 27

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 2/17/2019 ECE Application Programming: Lecture 27

ECE Application Programming: Lecture 27 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 2/17/2019 ECE Application Programming: Lecture 27