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 Fall 2018 Lecture 23 Character arrays and strings

2 ECE Application Programming: Lecture 23
Lecture outline Announcements/reminders Program 5 due 11/7 Exam 2 in class Monday, 11/5 Will cover lectures (except lecture 16) Lec. 25: Exam 2 Preview (Fri. 11/2) Today’s lecture Program 5 overview Review: Arrays and functions Character arrays and strings 6/30/2019 ECE Application Programming: Lecture 23

3 ECE Application Programming: Lecture 23
P5 overview Integral approximation through trapezoidal method Program split into three files zyBooks IDE allows you to view one at a time prog5_integral.c: main() function only prog5_functions.h: function prototypes Do not need to modify unless you’re adding function(s) prog5_functions.c: function definitions Complete what’s already there 6/30/2019 ECE Application Programming: Lecture 23

4 P5 overview (continued)
General program structure Read input values (endpoints, # trapezoids) Call integrate() from main() to perform integral Repeat program? User should answer Y/N (case insensitive) main() mostly responsible for input/output 1 large loop for most of main() repeats if ‘Y’/’y’ Smaller loops for each input prompt (like P4) Error conditions Formatting errors for each numeric input Low endpoint must be < high endpoint # trapezoids must be at least 1 6/30/2019 ECE Application Programming: Lecture 23

5 ECE Application Programming: Lecture 23
P5: functions integrate(min, max, trapezoids) Performs actual integral approximation f(x) Function to be integrated Always fixed as: sin(x) + x2 / 10 badInput() Used to clear line if formatting error occurs If written correctly, can generally handle errors as: if (formatting error) badInput(); 6/30/2019 ECE Application Programming: Lecture 23

6 ECE Application Programming: Lecture 23
P5: trapezoidal method Range [a, b] split into n trapezoids Trapezoid width = ∆𝑥= 𝑏 −𝑎 𝑛 Trapezoid area = 0.5 × 𝑏 × ℎ1+ℎ2 = 0.5 × ∆𝑥 × 𝑦 𝑘−1 + 𝑦 𝑘 = 0.5 × ∆𝑥 × 𝑓(𝑥 𝑘−1 )+𝑓( 𝑥 𝑘 ) 6/30/2019 ECE Application Programming: Lecture 23

7 P5: trapezoidal method (continued)
Math here is just simplification of basic area calculation 𝐴𝑟𝑒𝑎=0.5 × ∆𝑥 × 𝑦 0 + 𝑦 × ∆𝑥 × 𝑦 1 + 𝑦 2 +…+0.5 × ∆𝑥 × 𝑦 𝑛−1 + 𝑦 𝑛 =0.5 × ∆𝑥 × 𝑦 0 + 𝑦 1 + 𝑦 1 + 𝑦 2 +…+ 𝑦 𝑛−1 + 𝑦 𝑛 =0.5 × ∆𝑥 × 𝑦 0 +2 𝑦 1 +2 𝑦 2 +…+ 2𝑦 𝑛−1 + 𝑦 𝑛 =𝟎.𝟓× ∆𝒙 × 𝒚 𝟎 +𝟐 𝒌=𝟏 𝒏−𝟏 𝒚 𝒌 + 𝒚 𝒏 ≈ 𝑎 𝑏 𝑓 𝑥 𝑑𝑥 6/30/2019 ECE Application Programming: Lecture 23

8 P5: implementation warning
Go through x values  generate y values (trapezoid heights) How should you code that process? For loop can iterate over range, but be careful Wrong idea: loop over x values, i.e. for (x = a; x < b; x += deltaX) {…} What’s the potential problem? What data type should x, a, b, & deltaX be? All variables of type double  approximations Rounding errors will affect # loop iterations Is there a whole number you can use with loop? # iterations can (and should) be based on n Iterations might not exactly = n (could be n – 1, n + 1, ?) 6/30/2019 ECE Application Programming: Lecture 23

9 Review: arrays & pointers
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); Size of array does not need to be specified in brackets (and will be ignored by compiler) Calling functions with array arguments: simply specify name of array For example, given int x[20]; pass that array to f(): f(x, 20); Array name is pointer to first element: x  &x[0] 6/30/2019 ECE Application Programming: Lecture 23

10 ECE Application Programming: Lecture 23
Strings in C Strings in C: null-terminated arrays of characters “Hello”{‘H’, ‘e’, ‘l’, ‘l’, ‘o’, 0} Null character = 0 = ‘\0’ Can declare array to hold string Need space to hold null: char hello[5] would be too small Can use string constants to directly initialize char hello[] = “Hello”; Equivalent to: char hello[6]; hello[0] = ‘H’; hello[1] = ‘e’; hello[2] = ‘l’; hello[3] = ‘l’; hello[4] = ‘o’; hello[5] = OR-- hello[5] = ‘\0’; 6/30/2019 ECE Application Programming: Lecture 23

11 Strings and I/O functions
Can pass string as array or pointer: char * printf(), scanf() take char * as first argument Given string char hello[] from previous slide: Print directly: printf(hello); Print w/formatting using %s: printf(“%s\n”, hello); Print individual character: printf(“%c\n”, hello[1]); Overwrite with new string: scanf(“%s”, hello); Ampersand is unnecessary  array name is already address scanf() will read up to first whitespace character 6/30/2019 ECE Application Programming: Lecture 23

12 ECE Application Programming: Lecture 23
Final notes Next time More on strings Reminders: Program 5 due 11/7 Exam 2 in class Monday, 11/5 Will cover lectures (except lecture 16) Lec. 25: Exam 2 Preview (Fri. 11/2) 6/30/2019 ECE Application Programming: Lecture 23


Download ppt "EECE.2160 ECE Application Programming"

Similar presentations


Ads by Google