EECE.2160 ECE Application Programming

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
Advertisements

Computer Programming w/ Eng. Applications
printf() Documentation info:
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2013 Lecture 6: printf() formatting.
Basic Input/Output and Variables Ethan Cerami New York
C Programming Lecture 3. The Three Stages of Compiling a Program b The preprocessor is invoked The source code is modified b The compiler itself is invoked.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
ECE Application Programming Instructors: Dr. Michael Geiger & Nasibeh Nasiri Fall 2015 Lecture 2: Basic C program structure Data in C: Data types,
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Department of Electronic & Electrical Engineering Lecture 3 IO reading and writing variables scanf printf format strings "%d %c %f" Expressions operators.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
ECE Application Programming
Chapter 2 - Introduction to C Programming
ECE Application Programming
2.5 Another Java Application: Adding Integers
ECE Application Programming
Chapter 2 - Introduction to C Programming
OUTPUT STATEMENTS GC 201.
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Building Java Programs
Chapter 2 - Introduction to C Programming
Conversion Check your class notes and given examples at class.
Building Java Programs Chapter 2
CS150 Introduction to Computer Science 1
EECE.2160 ECE Application Programming
Introduction to Java Applications
ECE Application Programming
Building Java Programs
Arithmetic Operations
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Chapter 2 - Introduction to C 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
Building Java Programs Chapter 2
EECE.2160 ECE Application Programming
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
Introduction to C 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:

EECE.2160 ECE Application Programming Instructors: Dr. Michael Geiger Fall 2018 Lecture 4: Operators; printf() basics

Announcements/reminders Program 1 due today 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program DON’T FORGET BLACKBOARD “ASSIGNMENT” Program 2 to be posted; due Friday, 9/21 Won’t cover scanf() until Friday, 9/14 Suggestions Start working on program design now Test equations/output by assigning values to variables Once you understand scanf(), replace assignments with scanf() calls 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 Lecture outline Review Variables Today’s lecture Operators Basic variable output with printf() 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 Review: Variables Four basic data types int, float, double, char Variables Have name, type, value, memory location Variable declarations: examples int x; float a, b; double m = 2.35; Assignments: examples with variables above a = 7.5; x = a + 2; x = 9, not 9.5 m = m – 1; m = 1.35 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 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; } 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 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 (ASCII value 3 = "end of text" character) 5/26/2019 ECE Application Programming: Lecture 4

Arithmetic Operations Operator Addition + Subtraction - Multiplication * Division / Modulus Division (Remainder) % 5/26/2019 ECE Application Programming: Lecture 4

Results of arithmetic operations 3+7 10 - 3.0 15.0 (using non-integer makes result double precision) 12.62 + 9.8 22.42 .08*12.3 0.984 12.0/ 2.0 6.0 10/5 2 10/3 3 (not 3.333…) 10 % 3 1 12 % 5 2 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 Operators (cont.) Previous operators are binary Deal with two values C also supports some unary operators For now, we’ll simply deal with unary negation e.g., if x = 3, the statement -x; produces the value -3 Important note: The statement above does not change the value of x 5/26/2019 ECE Application Programming: Lecture 4

Operators and variables Operators can be used either with constants or variables Examples: int main() { int w, x, y, z; w = 3 + 2; // w = 5 x = -w; // x = -5 y = x – 7; // y = -12 z = w * y; // z = -60 return 0; } 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 Operators (cont.) More complex statements are allowed e.g. x = 1 + 2 - 3; Parentheses help you prioritize parts of statement Makes difference with order of operations x = 1 + 2 * 3; is different than x = (1 + 2) * 3; 5/26/2019 ECE Application Programming: Lecture 4

Example: Arithmetic operations Evaluate each of the following expressions, including the type (int or double) in your answer 19/3 3/19 19%3 3%19 5 + 7/2 5.0 + 7/2 5 + 7.0/2 5 * 3 % 3 / 6 + 14 + 10 / 2 5 * (3 % 3) / 6 + 14.0 + 10/3 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 Example solution 19/3 = 6 (integer division) 3/19 = 0 (integer division) 19%3 = 1 3%19 = 3 5 + 7/2 = 5 + 3 = 8 5.0 + 7/2 = 5.0 + 3 = 8.0 5 + 7.0/2 = 5 + 3.5 = 8.5 5/26/2019 ECE Application Programming: Lecture 4

Example solution (cont.) For each of the following, underlined part(s) evaluated first at each step 5 * 3 % 3 / 6 + 14 + 10 / 2 = 15 % 3 / 6 + 14 + 5 = 0 / 6 + 14 + 5 = 0 + 14 + 5 = 19 5 * (3 % 3) / 6 + 14.0 + 10/3 = 5 * 0 / 6 + 14.0 + 10/3 = 0 / 6 + 14.0 + 3 = 0 + 14.0 + 3 = 17.0 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 I/O basics Need ability to Print variables (or results calculated using them) Read values from input Output: printf() Already seen basics Input: scanf() 5/26/2019 ECE Application Programming: Lecture 4

Basic printf() formatting To print variables/constants, insert %<type> (format specifier) in your format string %c: single character %d or %i: signed decimal integer %f: float; %lf: double Prints 6 digits after decimal point by default To control # digits, use precision "%.4lf" prints with 4 digits (4th digit rounds) "%.0lf" prints with 0 digits (round to nearest integer) When printed, format specifier is replaced by value of corresponding expression If x is 3, printf("x + x = %d", x + x) prints: x + x = 6 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 printf() example float a=67.49,b=9.999925; printf("hello %f there %f\n",a,b); printf("%f%f%f%f\n",a,a,b,b); printf("a=%.2f, b=%.1f",a,b); printf("Cool huh?\n"); Printed: hello 67.490000 there 9.999925 67.49000067.4900009.9999259.999925 a=67.49, b=10.0Cool huh? 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 printf() details Detailed slides on printf() follow Skip these if you don’t want to go overboard with the full details of how the function works 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 02/07/2005 printf() Documentation info: int printf(const char *format [,argument] ...) Type of value returned Name of function ( ) indicate printf is a function First arg type and formal name (required, since no brackets) next argument type and name (in this case it may be any simple type) [ ] indicate optional arguments … indicates previous argument repeated zero or more times 5/26/2019 ECE Application Programming: Lecture 4 (c) 2005, P. H. Viall

ECE Application Programming: Lecture 4 printf() int printf(const char *format [,argument] ...) Type of value returned (int in this case) All functions return at most one value. The type void is used to indicate a function returns no value There is no requirement to use the value returned. The printf() function returns the number of characters printed (including spaces); returns negative value if error occurs. 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 printf() int printf(const char *format [,argument] ...) Name of function; printf( ) in this case A function name is ALWAYS followed by a set of (), even if the function takes no arguments 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 printf() int printf(const char *format [,argument] ...) Type (const char *) and name (format) of first argument For the moment, const char * can be thought of as a series of characters enclosed in double quotes The name format may be thought of as a code indicating how the arguments are to be interpreted, and how the output should look. 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 printf() int printf(const char *format [,argument] ...) zero of more optional arguments, each preceded by a comma zero because of the … optional because of the [ ] 5/26/2019 ECE Application Programming: Lecture 4

ECE Application Programming: Lecture 4 Final notes Next time scanf() Reminders: Program 1 due today 10 points: register for access to the course textbook 10 points: introduce yourself to your instructor 30 points: complete simple C program DON’T FORGET BLACKBOARD “ASSIGNMENT” Program 2 to be posted; due Friday, 9/21 Won’t cover scanf() until Friday, 9/14 Suggestions Start working on program design now Test equations/output by assigning values to variables Once you understand scanf(), replace assignments with scanf() calls 5/26/2019 ECE Application Programming: Lecture 4