ECE Application Programming

Slides:



Advertisements
Similar presentations
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
Advertisements

C Programming Tutorial – Part I CS Introduction to Operating Systems.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 31: Structures (cont.) Dynamic memory allocation.
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
C Programming Tutorial – Part I
ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

16.216 ECE Application Programming Instructor: Dr. Michael Geiger Summer 2012 Lecture 11 Structures Exam 3 Preview

ECE Application Programming: Lecture 11 Lecture outline Announcements/reminders Program 8 due Friday, 8/17 at noon Starter file now available Exam 3 Thursday, 8/16 Today’s lecture: Structures Exam 3 Preview 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Review: File I/O Open file: FILE *fopen(filename, file_access) Close file: fclose(file_handle) Formatted I/O: fprintf(file_handle, format_specifier, 0+ variables) fscanf(file_handle, format_specifier, 0+ variables) Unformatted I/O: size_t fwrite(pointer, element size, # elements, file_handle) size_t fread(pointer, element size, # elements, file_handle) Check for EOF using either fscanf() result or feof(FILE *) 7/22/2018 ECE Application Programming: Lecture 11

Review: Unformatted I/O (cont.) Character I/O int fputc(int c, FILE *stream); int putchar(int c); int fgetc(FILE *stream); int getchar(); int ungetc(int c, FILE *stream); Line I/O int fputs(const char *s, FILE *stream); int puts(const char *s); char *fgets(char *s, int n, FILE *stream); char *gets(char *s); 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 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 7/22/2018 ECE Application Programming: Lecture 11

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 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 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Using structure types Once defined, can declare variables using that type Scalar: StudentInfo student1; Array: StudentInfo classList[10]; Pointer: StudentInfo *sPtr; 7/22/2018 ECE Application Programming: Lecture 11

Using structure variables Initialization very similar to array initialization: StudentInfo student1 = { “John”, ‘Q’, “Smith”, 12345678, 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; 7/22/2018 ECE Application Programming: Lecture 11

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; } 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Example solution A = 1.00+2.00i B = 3.40+5.60i C = 1.00+2.00i D = 4.40+7.60i E = -2.40+-3.60i 7/22/2018 ECE Application Programming: Lecture 11

Structure assignments, pointers As seen previously: once structure defined, can assign variables of that type to one another Will work both for scalars and arrays Can also have pointers to structures Special notation to access structure elements through pointer: <ptr>-><element name> Example: StudentInfo *p = &student1; p->GPA = 3.5; 7/22/2018 ECE Application Programming: Lecture 11

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); 7/22/2018 ECE Application Programming: Lecture 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 #12345678 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 #: 12345678 Enter GPA: 1.23 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 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); } 7/22/2018 ECE Application Programming: Lecture 11

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; } 7/22/2018 ECE Application Programming: Lecture 11

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; } 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Exam 3 notes Allowed one 8.5” x 11” two-sided note sheet No other notes or electronic devices Exam lasts 3 hours (but is written for ~50 min) Covers all lectures after Exam 2 Format similar to Exams 1/2 1 multiple choice problem (File I/O, unformatted I/O, & structures) 1 code reading problem 1 code writing problem 7/22/2018 ECE Application Programming: Lecture 11

Review: arrays & pointer arithmetic 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, -3.233}; Must be sure to access inside bounds Array name is a pointer Arrays are always passed by address to functions Can use pointer to access array Can use arithmetic to move pointer through array p + 1  points to next element (after where p currently points) p++  move pointer and point to next element p--  move pointer and point to previous element 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 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 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] 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 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); Must leave enough room for terminating ‘\0’ 7/22/2018 ECE Application Programming: Lecture 11

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 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 7/22/2018 ECE Application Programming: Lecture 11

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 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Review: File I/O Open file: FILE *fopen(filename, file_access) Close file: fclose(file_handle) Formatted I/O: fprintf(file_handle, format_specifier, 0+ variables) fscanf(file_handle, format_specifier, 0+ variables) Unformatted I/O: size_t fwrite(pointer, element size, # elements, file_handle) size_t fread(pointer, element size, # elements, file_handle) Check for EOF using either fscanf() result or feof(FILE *) 7/22/2018 ECE Application Programming: Lecture 11

Review: Unformatted I/O (cont.) Character I/O int fputc(int c, FILE *stream); int putchar(int c); int fgetc(FILE *stream); int getchar(); int ungetc(int c, FILE *stream); Line I/O int fputs(const char *s, FILE *stream); int puts(const char *s); char *fgets(char *s, int n, FILE *stream); char *gets(char *s); 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 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; Access members using Dot operator: student1.middle = ‘J’; Arrow (if pointers): sPtr->GPA = 3.5; 7/22/2018 ECE Application Programming: Lecture 11

ECE Application Programming: Lecture 11 Next time Exam 3 7/22/2018 ECE Application Programming: Lecture 11