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
Instructors: Dr. Michael Geiger Spring 2018 Lecture 25 Structure examples; nested structures

2 ECE Application Programming: Lecture 25
Lecture outline Announcements/reminders Program 5 regrades due Monday, 4/9 Program 6 due today Program 7 due Friday, 4/13 Today’s lecture Review: Structures Structure examples Nested structures 9/20/2018 ECE Application Programming: Lecture 25

3 ECE Application Programming: Lecture 25
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 9/20/2018 ECE Application Programming: Lecture 25

4 Example: Using structures
What does the following print? typedef struct { double real; double imag; } Complex; int main() { Complex a = {1, 2}; Complex b = {3.4, 5.6}; Complex c, d, e; printf("A = %.2lf+%.2lfi\n", a.real, a.imag); printf("B = %.2lf+%.2lfi\n", b.real, b.imag); c = a; d.real = a.real + b.real; d.imag = a.imag + b.imag; e.real = a.real - b.real; e.imag = a.imag - b.imag; printf("C = %.2lf+%.2lfi\n", c.real, c.imag); printf("D = %.2lf+%.2lfi\n", d.real, d.imag); printf("E = %.2lf+%.2lfi\n", e.real, e.imag); return 0; } 9/20/2018 ECE Application Programming: Lecture 25

5 ECE Application Programming: Lecture 25
Example solution A = i B = i C = i D = i E = i Note: code in handout has spaces before and after ‘+’ for readability; code on previous slide doesn’t because it wouldn’t fit! 9/20/2018 ECE Application Programming: Lecture 25

6 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; 9/20/2018 ECE Application Programming: Lecture 25

7 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 9/20/2018 ECE Application Programming: Lecture 25

8 ECE Application Programming: Lecture 25
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); } 9/20/2018 ECE Application Programming: Lecture 25

9 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; } 9/20/2018 ECE Application Programming: Lecture 25

10 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; } 9/20/2018 ECE Application Programming: Lecture 25

11 ECE Application Programming: Lecture 25
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 9/20/2018 ECE Application Programming: Lecture 25

12 ECE Application Programming: Lecture 25
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 9/20/2018 ECE Application Programming: Lecture 25

13 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 9/20/2018 ECE Application Programming: Lecture 25

14 ECE Application Programming: Lecture 25
Final notes Next time Exam 2 Review Reminders: Program 5 regrades due Monday, 4/9 Program 6 due today Program 7 due Friday, 4/13 9/20/2018 ECE Application Programming: Lecture 25


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google