Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructors: Dr. Michael Geiger Summer 2017 Lecture 10 Structures Exam 2 Preview
2
ECE Application Programming: Lecture 10
Lecture outline Announcements/reminders Ch. 7 activities due Thursday Program 5 to be posted; due Wednesday 6/20 Old spec and starter files (which will become template files) on course schedule page Exam 2: Monday, 6/18 Will be allowed one 8.5” x 11” note sheet Covers material from Lec. 6-9 (not structures) Today’s lecture Structures Exam 2 Preview 6/17/2019 ECE Application Programming: Lecture 10
3
ECE Application Programming: Lecture 10
Review: strings Represented as character arrays Can be initialized using string constants char hello[] = "Hello"; Can access individual elements hello[3] = 'l'; Can print directly or with formatting Print directly: printf(hello); Print w/formatting using %s: printf("%s\n", hello); Reading strings: scanf("%s", str); Reads all characters up to (but not including) first space, tab, or newline Must leave enough room for terminating '\0' 6/17/2019 ECE Application Programming: Lecture 10
4
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 strncpy() not guaranteed to add null terminator 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 6/17/2019 ECE Application Programming: Lecture 10
5
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 strncat() guaranteed to add null terminator 6/17/2019 ECE Application Programming: Lecture 10
6
ECE Application Programming: Lecture 10
Structures Arrays: groups of data with same type Structures: groups of data with (potentially) different types Example: record to store information about student: First name (char []) Middle initial (char) Last name (char []) ID # (unsigned int) GPA (double) Any data type—scalar, array, pointer (even other structures) allowed 6/17/2019 ECE Application Programming: Lecture 10
7
Declaring structure types
Can define structure as a type using typedef Could omit typedef, but would need “struct” before type name Syntax: typedef struct { <list of members> } <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 6/17/2019 ECE Application Programming: Lecture 10
8
ECE Application Programming: Lecture 10
Using structure types Once defined, can declare variables using that type Scalar: StudentInfo student1; Array: StudentInfo classList[10]; Pointer: StudentInfo *sPtr; 6/17/2019 ECE Application Programming: Lecture 10
9
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; 6/17/2019 ECE Application Programming: Lecture 10
10
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; } 6/17/2019 ECE Application Programming: Lecture 10
11
ECE Application Programming: Lecture 10
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! 6/17/2019 ECE Application Programming: Lecture 10
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 *s); 6/17/2019 ECE Application Programming: Lecture 10
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/17/2019 ECE Application Programming: Lecture 10
14
ECE Application Programming: Lecture 10
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/17/2019 ECE Application Programming: Lecture 10
15
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; } 6/17/2019 ECE Application Programming: Lecture 10
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/17/2019 ECE Application Programming: Lecture 10
17
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 6/17/2019 ECE Application Programming: Lecture 25
18
ECE Application Programming: Lecture 10
Exam 2 notes Allowed one 8.5” x 11” double-sided note sheet No other notes No electronic devices (calculator, phone, etc.) Exam will last 2 hours and 20 minutes Covers material starting after Exam 1, through lecture 9 (lectures 6-9) Structures are not on Exam 2 Same general format as Exam 1 3 questions (each with multiple parts, so actually ~10 questions) + 1 extra credit question Functions (code reading/writing, MC?) Arrays (code reading/writing, MC?) Strings (code reading?, MC) 6/17/2019 ECE Application Programming: Lecture 10
19
ECE Application Programming: Lecture 10
Review: functions Used to break programs into smaller pieces Useful when code sequences repeated Functions have: An optional return value A name Optional arguments Must be prototyped or written completely prior to use Arguments can be: Passed by value: copy of argument is sent to function Arguments cannot be modified outside function Passed by address: address of argument Use pointers or address operator (&) Arguments can be modified outside function—used to “return” multiple values 6/17/2019 ECE Application Programming: Lecture 10
20
ECE Application Programming: Lecture 10
Review: pointers Pointer: address of a variable Can get address of existing object using & Can get value of existing pointer using * Can assign pointers of same type to each other Pointer declaration: <base type>* <pointer name> Base type determines how reference is interpreted Use pointers as function arguments pass by address 6/17/2019 ECE Application Programming: Lecture 10
21
Review: arrays & pointers
Arrays: groups of data with same type x[10] has 10 elements, x[0] through x[9] Can also define with initial values e.g. double list[] = {1.2, 0.75, }; Must be sure to access inside bounds Array name is a pointer Arrays are always passed by address to functions Should pass size of array as additional argument e.g. void f(int arr[], int n); 6/17/2019 ECE Application Programming: Lecture 10
22
ECE Application Programming: Lecture 10
Review: 2D arrays Declared similarly to 1D arrays Example (see below): int x[3][4]; Index elements similarly to 1-D arrays Initialize: int y[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; Typically used with nested for loops Can pass to functions—must specify # columns e.g. void f2(int arr[ ][4], int nRows); Col. 0 Col. 1 Col. 2 Col. 3 Row 0 x[0][0] x[0][1] x[0][2] x[0][3] Row 1 x[1][0] x[1][1] x[1][2] x[1][3] Row 2 x[2][0] x[2][1] x[2][2] x[2][3] 6/17/2019 ECE Application Programming: Lecture 10
23
ECE Application Programming: Lecture 10
Review: strings Represented as character arrays Can be initialized using string constants char hello[] = “Hello”; Can access individual elements hello[3] = ‘l’; Can print directly or with formatting Print directly: printf(hello); Print w/formatting using %s: printf(“%s\n”, hello); Reading strings: scanf(“%s”, str); Reads all characters up to (but not including) first space, tab, or newline Must leave enough room for terminating ‘\0’ 6/17/2019 ECE Application Programming: Lecture 10
24
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 strncpy() not guaranteed to add null terminator 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, 1 if s1 > s2, -1 if s1 < s2 6/17/2019 ECE Application Programming: Lecture 10
25
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 strncat() guaranteed to add null terminator 6/17/2019 ECE Application Programming: Lecture 10
26
ECE Application Programming: Lecture 10
Final notes Next time Exam 2—please be on time Reminders: Ch. 7 activities due Thursday Program 5 to be posted; due Wednesday 6/20 Old spec and starter files (which will become template files) on course schedule page Exam 2: Monday, 6/18 Will be allowed one 8.5” x 11” note sheet Covers material from Lec. 6-9 (not structures) 6/17/2019 ECE Application Programming: Lecture 10
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.