Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE Application Programming

Similar presentations


Presentation on theme: "ECE Application Programming"— Presentation transcript:

1 16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Summer 2015 Lecture 1: Course overview Basic C program structure Data: constants and variables

2 ECE Application Programming: Lecture 1
Lecture outline Announcements Program 1 due 5/22 40 points: complete simple C program 10 points: introduce yourself If we’ve met previously, you still have to meet with me this semester to earn these points. Course overview Instructor information Course materials Course policies Resources Course outline Introduction to C programming Program development cycle Development environments Basic program structure 6/12/2018 ECE Application Programming: Lecture 1

3 Course staff & meeting times
Lectures: TTh, 9 AM-12 PM, Ball 412 F (5/29 & 6/12 only) 9 AM-12 PM, Ball 412 Instructor: Dr. Michael Geiger Phone: (x43618 on campus) Office: 118A Perry Hall Office hours: T/Th 12-1 or by appointment Only on campus days we have class (T/Th + F 5/29, 6/12) 6/12/2018 ECE Application Programming: Lecture 1

4 ECE Application Programming: Lecture 1
Course materials Textbook: K.N. King, C Programming: A Modern Approach, 2nd edition, 2008, W.W. Norton. ISBN: Course tools: Need integrated development environment (IDE) that compiles/runs C code Recommended IDEs (all free; links on web) Windows: Microsoft Visual Studio Express (MS website) Mac: Xcode (Mac App Store) Linux: gcc/gdb (text-based; can run through terminal on Mac as well) 6/12/2018 ECE Application Programming: Lecture 1

5 Additional course materials
Course websites: Will contain lecture slides, handouts, assignments Discussion group through piazza.com: Allow common questions to be answered for everyone All course announcements will be posted here Will use as class mailing list—please enroll ASAP 6/12/2018 ECE Application Programming: Lecture 1

6 ECE Application Programming: Lecture 1
Course policies Academic honesty All assignments are to be done individually unless explicitly specified otherwise by the instructor Any copied solutions, whether from another student or an outside source, are subject to penalty You may discuss general topics or help one another with specific errors, but do not share assignment solutions Must acknowledge assistance from classmate in submission 6/12/2018 ECE Application Programming: Lecture 1

7 Programming assignments
Will submit all code via to Dr. Geiger Will not get confirmation unless you explicitly ask Penalty after due date: -(4n-1) points per day i.e., -1 after 1 day, -4 after 2 days, -16 after 3 days … Assignments that are 4+ days late receive 0 See grading policies (last three pages of today’s handout) for more details on: Grading rubric Common deductions Regrade policy Example grading 6/12/2018 ECE Application Programming: Lecture 1

8 Programming assignments: regrades
You are allowed one penalty-free resubmission per assignment Each regrade after the first: 1 day late penalty Must resubmit by regrade deadline, or late penalties will apply Late penalty still applies if original submission late “Original submission”  first file submitted containing significant amount of relevant code In other words, don’t turn in a virtually empty file just to avoid late penalties—it won’t count 6/12/2018 ECE Application Programming: Lecture 1

9 ECE Application Programming: Lecture 1
Grading and exam dates Grading breakdown Programming assignments: 60% Exam 1: 10% Exam 2: 15% Exam 3: 15% Exam dates Exam 1: Friday, May 29 Exam 2: Thursday, June 11 Exam 3: Thursday, June 25 6/12/2018 ECE Application Programming: Lecture 1

10 Tentative course outline
Basic C program structure and development Working with data: data types, variables, operators, expressions Basic console input/output Control flow Functions: basic modular programming, argument passing Pointers, arrays, and strings File & general input/output Bitwise operators Creating new data types: structures Dynamic memory allocation 6/12/2018 ECE Application Programming: Lecture 1

11 Programming exercises
Note on course schedule: several days marked as “PE#” Those classes will contain supervised, in-class programming exercises We’ll write/complete short programs to illustrate previously covered concepts If you have a laptop, bring it May have to do some design ahead of time 6/12/2018 ECE Application Programming: Lecture 1

12 ECE Application Programming: Lecture 1
Course questions General notes/questions about the course: How many of you have prior programming experience? For those that do, can improve programming style, efficiency, potentially learn new items For those that don’t, course assumes no prior programming experience Fair warning for all of you: material builds on itself throughout course Difficulty increases as course goes on If (when) you get stuck, ask for help!!! 6/12/2018 ECE Application Programming: Lecture 1

13 Course questions (continued)
How many of you are taking this course only because it’s required? Follow-up: how many of you hope you’ll never have to program again once you’re done with the course? Both computer and electrical engineers commonly program in industry—some examples: Automation of tasks Circuit simulation Test procedures Programming skills highly sought by employers 6/12/2018 ECE Application Programming: Lecture 1

14 ECE Application Programming: Lecture 1
Program development ... which is a good approach for your assignments, too! Average student’s approach to programming Read specification (assignment) ... at least some of it, anyway ... Attempt to write complete program Find output error and fix related code Repeat previous step until either Code completely works ... ... or code is such a mess that problem(s) can’t be fixed 6/12/2018 ECE Application Programming: Lecture 1

15 Program development (cont.)
A more structured approach to program development Read specification Identify requirements What results should program produce? How can I test correctness of those results? Plan design that implements requirements Using flowchart, pseudocode, etc. Plan for tests as well Translate design into actual code Test program and fix errors 6/12/2018 ECE Application Programming: Lecture 1

16 ECE Application Programming: Lecture 1
Our first C program #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

17 ECE Application Programming: Lecture 1
Our first C program # indicates pre-processor directive include is the directive stdio.h is the name of the file to "insert" into our program. The <> means it is part of the C development system #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

18 ECE Application Programming: Lecture 1
Our first C program main is the name of the primary (or main) procedure. All ANSI C programs must have a main routine named main The () indicates that main is the name of a procedure. All procedure references must be followed with () #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

19 ECE Application Programming: Lecture 1
Our first C program { } enclose a "block". A block is zero or more C statements. Note that code inside a block is typically indented for readability—knowing what code is inside the current block is quite useful. #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

20 ECE Application Programming: Lecture 1
Our first C program printf() is a "built-in" function (which is actually defined in stdio.h). "Hello World!" is the string to print. More formally, this is called the control string or control specifier. #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } Every statement must end with a ";". Preprocessing directives do not end with a ";" (but must end with a return). 6/12/2018 ECE Application Programming: Lecture 1

21 ECE Application Programming: Lecture 1
Our first C program The \n is an escape character used by the printf function; inserting this character in the control string causes a “newline” to be printed—it’s as if you hit the “Enter” key #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

22 ECE Application Programming: Lecture 1
Our first C program The int tells the compiler our main() program will return an integer to the operating system; the return tells what integer value to return. This keyword could be void, indicating that the program returns nothing to the OS. #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

23 Variations #1 of first program
#include <stdio.h> int main() { printf("Hello"); printf("there"); printf("World!"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

24 Variations #2 of first program
#include <stdio.h> int main() { printf("Hello\n"); printf("there\n"); printf("World!\n"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

25 Variations #3 of first program
#include <stdio.h> int main() { printf("Hello\nthere\nWorld!\n"); return 0; } 6/12/2018 ECE Application Programming: Lecture 1

26 Variations #4 of first program
#include <stdio.h> int main(){printf ("Hello\nthere\nWorld!\n");return 0;} Note while this is syntactically correct, it leaves much to be desired in terms of readability. 6/12/2018 ECE Application Programming: Lecture 1

27 ECE Application Programming: Lecture 2
Code readability Readability wouldn’t matter if: Entire code project written by one person All code was in same file Same person is the only one to use the code Code was used only for a short period of time More typically: Projects are split—multiple programmers and files Code usually reused Multiple users Used/adapted (hopefully) over long period of time You may reuse code ... but forget what you originally wrote! Bottom line: code needs to be readable 6/12/2018 ECE Application Programming: Lecture 2

28 ECE Application Programming: Lecture 2
Comments C allows you to add comments to your code Single line comments: start with // Multi-line comments: start with /* end with */ Typical uses Multi-line comment at start of program with Author’s name (& other info if appropriate) Date started/modified File name Description of overall file functionality For individual code sections Single/multi-line comment for major section of code performing single function Single line comment for single line of code if that line alone is important 6/12/2018 ECE Application Programming: Lecture 2

29 ECE Application Programming: Lecture 2
Comment example /* ECE Application Programming Instructor: M. Geiger 6/12/2018 hello.c: Intro program to demonstrate basic C program structure and output */ #include <stdio.h> // Main program: prints basic string and exits int main() { printf("Hello World!\n"); // Comment return 0; } 6/12/2018 ECE Application Programming: Lecture 2

30 ECE Application Programming: Lecture 2
Representing data in C Two major questions (for now) What kind of data are we trying to represent? Data types Can the program change the data? Constants vs. variables 6/12/2018 ECE Application Programming: Lecture 2

31 Four Types of Basic Data
Integer int Floating point (single precision) float Double Precision double Character char 6/12/2018 ECE Application Programming: Lecture 2

32 ECE Application Programming: Lecture 2
Integer Constants Any positive or negative number without a decimal point (or other illegal symbol). Legal values: Illegal values: 2,523 (comma) 6.5 (decimal point) $59 (dollar sign) 5. (decimal point) 6/12/2018 ECE Application Programming: Lecture 2

33 Range of Integers (Machine Dependent)
unsigned signed char 0   +127 (8 bits) short int 0   short (16 bits) int 0 to  long long int (32 bits) 6/12/2018 ECE Application Programming: Lecture 2

34 float/double Constants
Any signed or unsigned number with a decimal point Legal values: Legal (exponential notation): 1.624e e e23 1.0e e e e e+7 Illegal: $ , E5 6/12/2018 ECE Application Programming: Lecture 2

35 float/double Constants
Range of float (32 bits) ± E – 38 ± E + 38 Range of double (64 bits) ± E – 308 ± E + 308 6/12/2018 ECE Application Programming: Lecture 2

36 ECE Application Programming: Lecture 2
Character Constants Stored in ASCII or UNICODE Signified by single quotes (’ ’) Valid character constants ’A’ ’B’ ’d’ ’z’ ’1’ ’2’ ’!’ ’+’ ’>’ ’?’ ’ ’ ’#’ Invalid character constants ’GEIGER’ ’\’ ’CR’ ’LF’ ’’’ ’’’’ ’”’ ”Q” 6/12/2018 ECE Application Programming: Lecture 2

37 Character Escape Sequences
Meaning ’\b’ Backspace ’\’’ Single quote ’\n’ Newline ’\”’ Double quote ’\t’ Tab ’\nnn’ Char with octal value nnn ’\\’ Backslash ’\xnn’ Char with hex value nn 6/12/2018 ECE Application Programming: Lecture 2

38 Using #define with constants
Often makes sense to give constant value a symbolic name for readability Don’t want constant wasting memory space Use #define to define a macro Macro: named code fragment; when name is used, the preprocessor replaces name with code Syntax: #define <name> <code> Common use: defining constants By convention, start constant values with capital letters e.g. #define NumberOne 1 At compile time, all uses of “NumberOne” in your program are replaced with “1” and then compiled 6/12/2018 ECE Application Programming: Lecture 2

39 ECE Application Programming: Lecture 2
Variables All variables have four characteristics: A type An address (in memory) A value A name 6/12/2018 ECE Application Programming: Lecture 2

40 ECE Application Programming: Lecture 2
Variables - name must start with a-z, A-Z ( _ allowed, but not recommended) other characters may be a-z, A-Z, 0-9, _ upper case/lower case are not equal (i.e. ECE, ece, Ece, EcE, eCe would be five different variables) max length system dependent (usually at least 32) By convention Start with lowercase letter Descriptive names improve code readability 6/12/2018 ECE Application Programming: Lecture 2

41 Variables - legal names
grossPay carpet_Price cArPeT_price a_very_long_variable_name i ______strange___one_____ _ (not recommended) 6/12/2018 ECE Application Programming: Lecture 2

42 Variables - legal names (but not recommended)
l (that's lower case L) O (that's capital O) l1 (that's lower case L, and digit one) O0Oll11 (oh,zero,oh,el,el,one,one) _var (many system variables begin w/ _ ) 6/12/2018 ECE Application Programming: Lecture 2

43 ECE Application Programming: Lecture 2
Variables - declaring var name memory loc main() { float hours, payrate; float grosspay; int j; hours ? 4278 payrate ? 427C 4280 grosspay ? j ? 4284 All variable declarations should be grouped together at the start of the function 6/12/2018 ECE Application Programming: Lecture 2

44 ECE Application Programming: Lecture 2
Variables - assigning varname = expression; Declared variable single variable on left side of = expression any legal expression Expression can be constant, variable, function call, arithmetic operation, etc. Variable type (int, float, etc) and expression result type should match If not, funny things can happen ... 6/12/2018 ECE Application Programming: Lecture 2

45 ECE Application Programming: Lecture 2
Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; hours 40.0 4278 payrate ? 427C 4280 grosspay ? j ? 4284 6/12/2018 ECE Application Programming: Lecture 2

46 ECE Application Programming: Lecture 2
Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; hours 40.0 4278 payrate 20.0 427C 4280 grosspay ? j ? 4284 6/12/2018 ECE Application Programming: Lecture 2

47 ECE Application Programming: Lecture 2
Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate; hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j ? 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). 6/12/2018 ECE Application Programming: Lecture 2

48 ECE Application Programming: Lecture 2
Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate; j = 5; hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j 5 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). 6/12/2018 ECE Application Programming: Lecture 2

49 ECE 160 - Introduction to Computer Engineering I
02/09/2005 Variables (cont.) var name memory loc main() { float hours, payrate; float grosspay; int j; hours = 40.0; payrate = 20.00; grosspay = hours * payrate j = 5; j = j + 1; hours 40.0 4278 payrate 20.0 427C 4280 grosspay 800.00 j 5 6 4284 note: referencing a variable only "reads" it (non-destructive). Assigning to a variable overwrites whatever was there (destructive). 6/12/2018 ECE Application Programming: Lecture 2 (c) 2005, P. H. Viall

50 ECE Application Programming: Lecture 3
Example: Variables What values do w, x, y, and z have at the end of this program? int main() { int w = 5; float x; double y; char z = ‘a’; x = 8.579; y = -0.2; w = x; y = y + 3; z = w – 5; return 0; } 6/12/2018 ECE Application Programming: Lecture 3

51 ECE Application Programming: Lecture 3
Example solution int main() { int w = 5; float x; double y; char z = ‘a’; x = 8.579; y = -0.2; w = x; y = y + 3; z = w – 5; return 0; } w = 5 z = ‘a’ (ASCII value 97) x = 8.579 y = -0.2 w = 8 (value is truncated) y = (-0.2) + 3 = 2.8 z = 8 – 5 = 3 6/12/2018 ECE Application Programming: Lecture 3

52 ECE Application Programming: Lecture 2
Assignment #1 Basic assignment to ensure you can write, run, and submit programs Write a short program that prints (each item on its own line): Your name Your major Your class (i.e. freshman, sophomore, etc.) The name and semester of this course Submit only your source (prog1_simple.c) file via to File name matters! 6/12/2018 ECE Application Programming: Lecture 2

53 ECE Application Programming: Lecture 1
Final notes Next time: Operators Basic input/output with scanf()/printf() PE1: Flowcharts and debugging basics Reminders: Sign up for the course discussion group on Piazza! Program 1 due Friday, 5/22 40 points: complete simple C program 10 points: introduce yourself If we’ve met previously, you still have to meet with me this semester to earn these points. 6/12/2018 ECE Application Programming: Lecture 1


Download ppt "ECE Application Programming"

Similar presentations


Ads by Google