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 & Dr. Peilong Li Spring 2017 Lecture 26: Nested structures

2 ECE Application Programming: Lecture 26
Lecture outline Announcements/reminders Program 7 due Friday, 4/7 Today’s class 2-D arrays and functions Program 7 intro Nested structures Start PE4 (Structures) 7/31/2019 ECE Application Programming: Lecture 26

3 2-D arrays and functions
When passing 2-D array to function, can omit first dimension (rows) but must list columns Example: // Assume n = # of rows int f(int arr[][4], int n); int main() { int x[3][4]; f(x, 3); ... } 7/31/2019 ECE Application Programming: Lecture 22

4 ECE Application Programming: Lecture 26
Program 7 overview Store 21 * 51 grid in 2-D character array Each character is grid line ('-', '|', or '+'), space (' '), or star ('*') Stars represent “boxes” added to grid User inputs x, y coordinates of lower left corner + width/height of box Commands (all strings) “add”: add a box to the grid “print”: print current state of grid + added boxes “reset”: reset grid to original state (no boxes) “exit”: end the program Functions resetGrid(char grid[][NCols]): resets grid to hold only grid lines & spaces addBox(char grid[][NCols], int x, int y, int width, int height): adds new box to grid printGrid(char grid[][NCols]): output current grid state to screen 7/31/2019 ECE Application Programming: Lecture 26

5 Program 7 overview (continued)
A few tricky things about the program: Don’t assume commands match just because first few characters match e.g. if I type “resetting”, your grid shouldn’t be reset Boxes can lie partially inside grid e.g. box with origin (-1, -1), width 5, height 5 Program must check for such cases and only modify coordinates inside grid Failure to check for these cases leads to either Boxes “wrapping around” grid, or Program crashing when you go outside your grid array 7/31/2019 ECE Application Programming: Lecture 26

6 ECE Application Programming: Lecture 26
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 7/31/2019 ECE Application Programming: Lecture 26

7 ECE Application Programming: Lecture 26
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 7/31/2019 ECE Application Programming: Lecture 26

8 ECE Application Programming: Lecture 26
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 7/31/2019 ECE Application Programming: Lecture 26

9 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 7/31/2019 ECE Application Programming: Lecture 26

10 ECE Application Programming: Lecture 26
Next time Finish PE4 Dynamic memory allocation Reminders: Program 7 due Friday, 4/7 7/31/2019 ECE Application Programming: Lecture 26


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google