Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Chapter Day 4. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 4 Return Signed Contracts Questions from last Class?? Problem set 1 Posted.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Expressions, Data Conversion, and Input
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
© 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Doing math In java.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
2-1 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
CprE 185: Intro to Problem Solving (using C)
ARITHMETIC IN C Operators.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Creating Objects & String Class
CprE 185: Intro to Problem Solving (using C)
Escape Sequences What if we wanted to print the quote character?
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
With Assignment Operator
A First Book of ANSI C Fourth Edition
Arithmetic Expressions & Data Conversions
Data Types and Expressions
CprE 185: Intro to Problem Solving (using C)
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Formatting Numbers CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

Administrative Stuff HW3 is out Due on Friday Sep 8pm  Electronic submission on WebCT

Quick review of the last lecture

Expressions An expression is a combination of one or more operators and operands Arithmetic expressions compute numeric results and make use of the arithmetic operators: If either or both operands used by an arithmetic operator are floating point, then the result is a floating point Addition Subtraction Multiplication Division Remainder +-*/%+-*/% © 2004 Pearson Addison-Wesley. All rights reserved

Division and Remainder If both operands to the division operator ( / ) are integers, the result is an integer (the fractional part is discarded) The remainder operator (%) returns the remainder after dividing the second operand into the first 14 / 3 equals 8 / 12 equals % 3 equals 8 % 12 equals 2 8 © 2004 Pearson Addison-Wesley. All rights reserved

Operator Precedence Operators can be combined into complex expressions result = total + count / max - offset; Operators have a well-defined precedence which determines the order in which they are evaluated Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order © 2004 Pearson Addison-Wesley. All rights reserved

Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123 © 2004 Pearson Addison-Wesley. All rights reserved

Expression Trees The evaluation of a particular expression can be shown using an expression tree The operators lower in the tree have higher precedence for that expression a + (b – c) / d a + / -d bc © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Revisited The assignment operator has a lower precedence than the arithmetic operators First the expression on the right hand side of the = operator is evaluated Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1432 © 2004 Pearson Addison-Wesley. All rights reserved

Effect of sum = sum + item; Figure 2.4. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Effect of scanf("%lf", &miles); Figure 2.5. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree for area = PI * radius * radius; Figure 2.8. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Step-by-Step Expression Evaluation Figure 2.9. Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1); Figure Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y Figure Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Assignment Revisited The right and left hand sides of an assignment statement can contain the same variable First, one is added to the original value of count Then the result is stored back into count (overwriting the original value) count = count + 1; © 2004 Pearson Addison-Wesley. All rights reserved

Increment and Decrement The increment and decrement operators use only one operand The increment operator ( ++ ) adds one to its operand The decrement operator ( -- ) subtracts one from its operand The statement count++; is functionally equivalent to count = count + 1; © 2004 Pearson Addison-Wesley. All rights reserved

Increment and Decrement The increment and decrement operators can be applied in postfix form: count++ or prefix form: ++count When used as part of a larger expression, the two forms can have different effects Because of their subtleties, the increment and decrement operators should be used with care © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Operators Often we perform an operation on a variable, and then store the result back into that variable C provides assignment operators to simplify that process For example, the statement num += count; is equivalent to num = num + count; © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Operators There are many assignment operators in Java, including the following: Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y © 2004 Pearson Addison-Wesley. All rights reserved

Assignment Operators The right hand side of an assignment operator can be a complex expression The entire right-hand expression is evaluated first, then the result is combined with the original variable Therefore result /= (total-MIN) % num; is equivalent to result = result / ((total-MIN) % num); © 2004 Pearson Addison-Wesley. All rights reserved

Escape Sequences Some C escape sequences: Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash © 2004 Pearson Addison-Wesley. All rights reserved

printf() function printf(“format string”, variable1, variable2, …); printf(“For int use %d”, myInteger); printff(“For float use %f”, myFloat); printf(“For double use %lf”, myDouble); printf(“For float or double %g”, myF_or_D);

scanf() function scanf(“format string”, &variable1, &variable2, …); scanf(“%d”, &myInteger); scanf(“%f”, &myFloat); scanf(“%lf”, &myDouble); scanf(“%d%f”, &myInteger, &myFloat);

Common Bugs Using & in a printf function call. printf(“For int use %d”, &myInteger); // wrong Using the wrong string in printf printf(“This is a float %d”, myFloat); // use %f not %d Not using & in a scanf() function call. scanf(“%d”, myInteger); // Wrong Using the wrong string in scanf() scanf(“%d”, &myFloat); // wrong; use %f instead of %d

Why do we need to specify the format of the keyboard input before we can read it?

printf() arguments %[flags][width][.precision][length]specifier

Printing octal, hexadecimal and binary

Questions?

THE END