Download presentation
Presentation is loading. Please wait.
1
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Fall 2018 Lecture 25 Exam 2 Preview
2
ECE Application Programming: Exam 2 Preview
Lecture outline Announcements/reminders Exam 2 in class Monday, 11/5 Will be allowed one 8.5” x 11” note sheet Will cover lectures (except lecture 16) Please attend the section for which you are registered Program 5 due 11/7 Program 4 regrades due 11/9 Today’s lecture: Exam 2 Preview 7/2/2019 ECE Application Programming: Exam 2 Preview
3
ECE Application Programming: Exam 2 Preview
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 50 minutes Covers material starting after Exam 1, through lecture 24 (lectures 14-24, except 16) Same general format as Exam 1 3 sections (each with multiple parts, so ~10 questions) + 1 extra credit question For loops (in all sections) Functions (code reading/writing) Arrays (code reading/writing, MC) Strings (MC) 7/2/2019 ECE Application Programming: Exam 2 Preview
4
ECE Application Programming: Exam 2 Preview
Review: for loops for (<init var>; <test>; <change var>) <statements> Operators to directly change variable x++, x-- post-increment/decrement ++x, --x pre-increment/decrement +=, -=, *=, /= augmented assignment Don’t worry about pre-/post-increment differences for test 7/2/2019 ECE Application Programming: Exam 2 Preview
5
ECE Application Programming: Exam 2 Preview
Review: functions Key points to understand 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—allows function to change >1 value Return value Expression following return keyword Value copied back to where function is called Can be assigned to variable, used in another expression, etc. 7/2/2019 ECE Application Programming: Exam 2 Preview
6
ECE Application Programming: Exam 2 Preview
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 7/2/2019 ECE Application Programming: Exam 2 Preview
7
Review: pass by value; return value
int f(int x, int y) { x = x + 2; // Change x only // inside function return (x + y) / 2; } int main() { int a = 10; int b = 20; int c = f(a, b); // Given f() … // above, c = 16 printf("%d", f(5, 15)); // Prints 11 … // (return // val. of f) 7/2/2019 ECE Application Programming: Exam 2 Preview
8
Review: pass by address
int f2(int *p1, int *p2) { *p1 = *p1 + 2; *p2 = *p2 – 3; return *p1 + *p2; } int main() { int a = 10; int b = 20; int c = f2(&a, &b); // a = a + 2 = 12 // b = b – 3 = 17 // c = a + b = 29 7/2/2019 ECE Application Programming: Exam 2 Preview
9
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); 7/2/2019 ECE Application Programming: Exam 2 Preview
10
ECE Application Programming: Exam 2 Preview
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 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/2/2019 ECE Application Programming: Exam 2 Preview
11
ECE Application Programming: Exam 2 Preview
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’ 7/2/2019 ECE Application Programming: Exam 2 Preview
12
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, non-zero value if s1 != s2 7/2/2019 ECE Application Programming: Exam 2 Preview
13
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 7/2/2019 ECE Application Programming: Exam 2 Preview
14
ECE Application Programming: Exam 2 Preview
Final notes Next time: Exam 2 Please be on time Please attend the section for which you are registered Reminders: Program 5 due 11/7 Program 4 regrades due 11/9 7/2/2019 ECE Application Programming: Exam 2 Preview
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.