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 Fall 2018 Lecture 29 Structure examples; nested structures

2 ECE Application Programming: Lecture 28
Lecture outline Announcements/reminders Program 6 due today Late penalties capped at -1 between Tuesday, 11/20 and Monday, 11/26 Penalties for that assignment begin increasing again Tuesday, 11/27 Program 5 regrades due Monday, 11/26 Program 7 to be posted; due Tuesday, 12/4 Today’s lecture Review: Structures Structure examples Nested structures overview 5/14/2019 ECE Application Programming: Lecture 28

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

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

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

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

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

8 ECE Application Programming: Lecture 28
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); } 5/14/2019 ECE Application Programming: Lecture 28

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

10 Example solution (cont.)
StudentInfo readStudent() { StudentInfo s; char ch; printf(“Enter name: ”); scanf(“%s %c%c %s”, s.first, &s.middle, &ch, s.last); printf(“Enter ID #: ”); scanf(“%u”, &s.ID); printf(“Enter GPA: ”); scanf(“%lf”, &s.GPA); return s; } 5/14/2019 ECE Application Programming: Lecture 28

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

12 ECE Application Programming: Lecture 28
Final notes Next time (Monday, 11/26) PE3: Structures Reminders: Program 6 due today Late penalties capped at -1 between Tuesday, 11/20 and Monday, 11/26 Penalties for that assignment begin increasing again Tuesday, 11/27 Program 5 regrades due Monday, 11/26 Program 7 to be posted; due Tuesday, 12/4 5/14/2019 ECE Application Programming: Lecture 28


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google