1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Introduction to C Programming
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Chapter 2 Basic Elements of Fortan
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Data types and variables
Introduction to C Programming
Lecture 2: Topics Bits and Bytes Primitive Types Casting Strings Boolean expressions.
Basic Input/Output and Variables Ethan Cerami New York
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Data Conversion, and Input
Objectives You should be able to describe: Data Types
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
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.
Numeric precision in SAS. Two aspects of numeric data in SAS The first is how numeric data are stored (how a number is represented in the computer). –
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Input, Output, and Processing
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Chapter-4 Managing input and Output operation.  Reading, processing and writing of data are three essential functions of a computer program.  Most programs.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
Mathematical Calculations in Java Mrs. C. Furman.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
+ Note On the Use of Different Data Types Use the data type that conserves memory and still accomplishes the desired purpose. For example, depending on.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
Topics Designing a Program Input, Processing, and Output
Arithmetic Expressions Function Calls Output
ECE Application Programming
Object Oriented Programming
Structure of a C Program
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lecture 3 Expressions Richard Gesick.
INPUT & OUTPUT scanf & printf.
CS1100 Computational Engineering
A First Book of ANSI C Fourth Edition
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Conversion Check your class notes and given examples at class.
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Introduction to C Programming
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning below) –distance –weight, etc.

2 Float Example (Program 2.2) /* Float Example Program */ #include int main () { float var1, var2, var3, sum; var1 = 87.25; var2 = 92.50; var3 = 96.75; sum = var1 + var2 + var3; printf ("Sum: %.2f", sum); } %f: indicates floating values %.2f displays a floating point value with 2 decimal points. Output: Sum:

3 Example: Finding an Average Suppose you want to determine a student’s average. int num = 4; float avg = /num; Problem #1: Operator Precedence –By rules of operator precedence, 100/4 is evaluated first. Hence, avg is set to: 302. –To solve this problem, use (): float avg = ( )/num;

4 Finding an Average Problem #2: –90, 92, 95, 100 and 4 are all integers. Hence, this is integer division. –Integer division can result in data truncation (or loss of data.) –Hence, avg is set to: 94.0, but the students real average is –There are actually three ways to solve this problem.

5 Rules of Promotion Promotion: when mixing ints and floats, everything is promoted to floats. In our average example, there are three ways to force promotion, and get the right answer of 94.25: 1. change num to a float: float num = 4.0; float avg = ( )/num;

6 Rules of Promotion 2. Use a Cast Operator int num = 4; float avg = ( )/(float)num; In this case, num is explicitly cast to a float. And, because we have one float, everything else is promoted. Note that you can also use the (int) cast to cast a float to an integer. 3. Divide by 4.0 float avg = ( ) / 4.0;

7 Finding an Average Problem #2: –90, 92, 95, 100 and 4 are all integers. Hence, this is integer division. –Integer division can result in data truncation (or loss of data.) –Hence, avg is set to: 94.0, but the students real average is –There are actually three ways to solve this problem.

8 NEVER USE floats HERE: In a condition for equality comparison –Floats may be represented differently from what you think by the computer E.g. 1.9 to you may be –1.9 will not necessarily equal 1.9! In critical calculations for the same reason –E.g..1 added 10 times often will not add up to 1 –Use long ints instead and keep track of where your decimal point is (e.g. $1.75 should be stored as 175)

9 Comparing floats If you need to compare floats, determine some threshold and test against that E.g. if ( (fDollarsReceived – fDollarsExpected) <=.00001) ) { printf( “OK, close enough to equal “ “for me!\n” ); } Note you have to check for the reverse too

10 Comparing floats cont’d Complete working example float fDollarDiff = 0 ; fDollarDiff = fDollarsReceived – fDollarsExpected ; if ( <= fDollarDiff && fDollarDiff <= ) { printf( “OK, close enough to equal “ “for me!\n” ); }

11 Good case for a constant By the way that is a prime candidate for #define! –E.g #define f_ACCEPTABLE_THRESHOLD.00001

12 printf conversion specifications The format control string (the first argument to printf) contains: –conversion specifiers (%d, %i, %f) –field widths –precisions –other info we will not look at

13 Field width The exact size of the field in which data is to be printed is specified by a field width. If the field width is larger than the data being printed, the data will normally be right- justified within that field. Use a “-” sign before the number in field width to left-justify If the field width is smaller than the data being printed, the field width is increased to print the data.

14 Field width cont’d An integer representing the field width is inserted between the percent sign (%) and the conversion specifier –For example, %4d int iYourAnswer = 1 ; printf( “The answer %4d is correct!\n”, iYourAnswer ); Will print the following The answer 1 is correct!

15 Precision You can also specify the precision with which data is to be printed. Precision has different meaning -- for different types. –integer: minimum number of digits to be printed –float: number of digits to appear after the decimal point

16 Precision cont’d To use precision, place a decimal point (.) followed by an integer representing the precision between the percent sign(%) and the conversion specifier (ie %.2f)

switch Multiple-Selection Structure Used when testing a variable or expression for EQUALITY (ie no >, =, <= tests) separately for each of the constant integral values it may assume. Preferred over if else in situations where you are testing the same expressions for equality with many different values. Allows you to perform different actions for each test.

switch Multiple-Selection Structure switch (expression){ case value1: action(s); break; case value2: action(s); break; … case default: actions(s); break; } keyword switch could use more than one case; if the same actions are required expression can be a variable or a more complicated expression actions within a single case do not need brackets the default case will be executed in the event that no other case is

beware of “fall through” If you forget to use the break keyword between cases, unexpected things may happen. Once a case tests true, all the statements following that case, will be executed until the next break. Experienced programmers may use this on purpose. For this class we will not.