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 24: Structures (cont.)

2 ECE Application Programming: Lecture 24
Lecture outline Announcements/reminders Program 6 due today Exam 2 in class 3/29 Will be allowed one double-sided 8.5” x 11” note sheet Covers material starting after Exam 1, through end of week (lectures 13-17, 19-24) Change for late/regrade submissions & other grading issues: please directly contact TAs and CC your instructor Li Zhou odd-numbered programs Zhendong Wang even-numbered programs Today’s class Finish structures 4/12/2019 ECE Application Programming: Lecture 24

3 Review: String functions
In <string.h> library: Copying strings: char *strcpy(char *dest, const char *source); char *strncpy(char *dest, const char *source, size_t num); Return dest Does not append ‘\0’ unless length of source < num Comparing strings: int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t num); Character-by-character comparison of character values Returns 0 if s1 == s2, >0 if s1 > s2, <0 if s1 < s2 4/12/2019 ECE Application Programming: Lecture 24

4 Review: String functions (cont.)
Find # of characters in a string size_t strlen(const char *s1); Returns # characters before ‘\0’ Not necessarily size of array “Add” strings together—string concatenation char *strcat(char *dest, const char *source); char *strncat(char *dest, const char *source, size_t num); Returns dest 4/12/2019 ECE Application Programming: Lecture 24

5 ECE Application Programming: Lecture 24
Review: Structures User-defined data types; collections of data with different types Can define structure as a type using typedef Could omit typedef, but would need “struct” before type name Syntax: typedef struct { <list of variables> } <typeName>; Example: typedef struct { char first[50]; char middle; char last[50]; unsigned int ID; double GPA; } StudentInfo; typedef usually at program start (with #include, #define) <typeName> usually starts with capital letter 4/12/2019 ECE Application Programming: Lecture 24

6 ECE Application Programming: Lecture 24
Using structure types Once defined, can declare variables using that type Scalar: StudentInfo student1; Array: StudentInfo classList[10]; Pointer: StudentInfo *sPtr; 4/12/2019 ECE Application Programming: Lecture 24

7 Using structure variables
Initialization very similar to array initialization: StudentInfo student1 = { “John”, ‘Q’, “Smith”, , 3.75 }; Accessing structure elements: . operator Syntax: <var name>.<element name> Examples: printf(“%s %c %s”, student1.first, student1.middle, student1.last); student1.GPA = 3.5; 4/12/2019 ECE Application Programming: Lecture 24

8 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; } 4/12/2019 ECE Application Programming: Lecture 24

9 ECE Application Programming: Lecture 24
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! 4/12/2019 ECE Application Programming: Lecture 24

10 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 *s); 4/12/2019 ECE Application Programming: Lecture 24

11 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 4/12/2019 ECE Application Programming: Lecture 24

12 ECE Application Programming: Lecture 24
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); } 4/12/2019 ECE Application Programming: Lecture 24

13 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; } 4/12/2019 ECE Application Programming: Lecture 24

14 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; } 4/12/2019 ECE Application Programming: Lecture 24

15 ECE Application Programming: Lecture 24
Nested structures Variables inside structures can be structures themselves Example: general structure for names typedef struct { char first[50]; // First name char middle; // Middle initial char last[50]; // Last name } Name; 4/12/2019 ECE Application Programming: Lecture 24

16 Nested structures (cont.)
Example (cont.): Could use Name structure any time you need name within a structure Redefining StudentInfo: typedef struct { Name stu_name; // Student name unsigned int ID; // ID # double GPA; // Grade point } StudentInfo; Will need multiple dot operators to access field within nested structure Given StudentInfo s1; s1.stu_name  Name structure within s1 s1.stu_name.middle  middle initial of name within s1 4/12/2019 ECE Application Programming: Lecture 24

17 ECE Application Programming: Lecture 24
Next time Exam 1 Preview Reminders: Program 6 due today Exam 2 in class 3/29 Will be allowed one double-sided 8.5” x 11” note sheet Covers material starting after Exam 1, through end of week (lectures 13-17, 19-24) Change for late/regrade submissions & other grading issues: please directly contact TAs and CC your instructor Li Zhou odd-numbered programs Zhendong Wang even-numbered programs 4/12/2019 ECE Application Programming: Lecture 24


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google