Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructors: Dr. Lin Li & Dr. Michael Geiger Spring 2019 Lecture 29 Structures (continued)
2
ECE Application Programming: Lecture 29
Lecture outline Announcements/reminders Program 7 due Wed, 4/24 Remaining programs Program 8 to be due Friday, 5/3 (last day of classes) Program 9 (extra credit) to be due during finals; will not accept late submissions Today’s lecture Program 7 overview Review structures basics Structures as function arguments—examples 6/22/2019 ECE Application Programming: Lecture 29
3
ECE Application Programming: Lecture 29
Program 7 overview New topics covered in this program 2-D arrays Strings Program accepts single-word commands Store command as string Display/modify state of 21 x 51 “pixel” grid Stored as 2-D array of characters (NOT strings) 1 “pixel” = 1 char Axis labels not stored in array (unless you modify starter file) 6/22/2019 ECE Application Programming: Lecture 29
4
ECE Application Programming: Lecture 29
Program 7 commands add: Add box to grid Prompt user for x, y coordinates of lower left corner as well as width/height print: Print current grid state with all boxes reset: Remove all boxes from grid and reset to initial state exit: End the program If user enters any other command, print an error message 6/22/2019 ECE Application Programming: Lecture 29
5
Program 7: initial grid state
Grid uses +, -, |, and space characters 6/22/2019 ECE Application Programming: Lecture 29
6
ECE Application Programming: Lecture 29
Program 7: grid + boxes Boxes shown 11 x 8 at (10, 5) 3 x 3 at (25, 10) 4 x 1 at (36, 6) 4 x 4 at (49, 19) Last box is partially outside grid! Not an error, but definitely condition to account for 6/22/2019 ECE Application Programming: Lecture 29
7
ECE Application Programming: Lecture 29
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 6/22/2019 ECE Application Programming: Lecture 29
8
Working with structures
Can rarely work with entire structure Assignment only—that’s it Work with individual member(s) Must eventually deal with built-in types Different modes of access for Single structure variable dot operator Pointer to structure variable arrow operator Let’s discuss how and why … 6/22/2019 ECE Application Programming: Lecture 29
9
ECE Application Programming: Lecture 29
Structures vs. Arrays Arrays Pro: simple access (location- or index-based) Con: doesn’t support multiple data types Structures Pro: more flexibility in data types Con: complex access to members (name-based) Complexity of access due to memory layout 6/22/2019 ECE Application Programming: Lecture 29
10
ECE Application Programming: Lecture 29
Dot operator Used with single structure variable Must specify specific instance of new data type General form: <var name>.<member name> Examples (to be handwritten): 6/22/2019 ECE Application Programming: Lecture 29
11
ECE Application Programming: Lecture 29
Pointers Why are pointers useful? Primarily as pointer arguments Allow function to modify data declared outside Why else are structure pointers useful? Hint: how are arrays always passed to functions? Saves time and space to pass structs by address Use arrow operator—does work of Pointer dereferencing Member selection (dot operator) 6/22/2019 ECE Application Programming: Lecture 29
12
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; 6/22/2019 ECE Application Programming: Lecture 29
13
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 6/22/2019 ECE Application Programming: Lecture 29
14
ECE Application Programming: Lecture 29
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); } 6/22/2019 ECE Application Programming: Lecture 29
15
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; } 6/22/2019 ECE Application Programming: Lecture 29
16
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; } 6/22/2019 ECE Application Programming: Lecture 29
17
ECE Application Programming: Lecture 29
Final notes Next time Nested structures Reminders: Program 7 due Wed, 4/24 Remaining programs Program 8 to be due Friday, 5/3 (last day of classes) Program 9 (extra credit) to be due during finals; will not accept late submissions 6/22/2019 ECE Application Programming: Lecture 29
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.