Programming Fundamentals

Slides:



Advertisements
Similar presentations
Chapter 3. Expressions and Interactivity CSC125 Introduction to C++
Advertisements

1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
Basic Elements of C++ Chapter 2.
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
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.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Week 1 Algorithmization and Programming Languages.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
Introduction to C++ Basic Elements of C++. C++ Programming: From Problem Analysis to Program Design, Fourth Edition2 The Basics of a C++ Program Function:
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Constants, Data Types and Variables
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Basic Elements of C++
Introduction to C++ (Extensions to C)
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 3 Assignment and Interactive Input.
Computing Fundamentals
Basic Elements of C++.
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
A First Book of ANSI C Fourth Edition
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 3 Input output.
Chapter # 2 Part 2 Programs And data
C++ Programming Lecture 3 C++ Basics – Part I
An Introduction to Programming with C++ Fifth Edition
C++ Programming Basics
listing. Where is the variable
C++ for Engineers and Scientists Second Edition
Chapter 1 c++ structure C++ Input / Output
Variables and Constants
Presentation transcript:

Programming Fundamentals

Today’s Lecture Input with cin Floating point variables Type bool Setw manipulator

Input with cin (Example 1)

OUTPUT

Example 2

OUTPUT

Input with cin The statement causes the program to wait for the user to type in a number. The resulting number is placed in the variable ftemp.

Input with cin The keyword cin (pronounced “C in”) is an object, predefined in C++ to correspond to the standard input stream. This stream represents data coming from the keyboard. The >> is the extraction or get from operator. It takes the value from the stream object on its left and places it in the variable on its right.

Variables Defined at Point of Use Note that in this example variables are defined at point of use. You can define variables throughout a program, not just at the beginning.

Cascading << The program first sends the phrase Equivalent in Celsius is: to cout, then it sends the value of ctemp, and finally the newline character ‘\n’. The extraction operator >> can be cascaded with cin in the same way, allowing the user to enter a series of values.

Cascading Example

OUTPUT Two numbers separated by space

OUTPUT Two numbers separated by line

Expressions Any arrangement of variables, constants, and operators that specifies a computation is called an expression. For example, alpha+12 and (alpha-37)*beta/2 are expressions. When the computations specified in the expression are performed, the result is usually a value. Thus if alpha is 7,the first expression shown has the value 19.

Precedence () ^ * , / + , - Example expression: * , / + , - Example expression: 2 * 4 / 4 + (6 + 6 / 3) ans???

Floating Point Types Floating-point variables represent numbers with a decimal place—like 3.1415927, 0.0000625,and –10.2. They have both an integer part, to the left of the decimal point, and a fractional part, to the right.

Kinds of floating-point variables There are three kinds of floating-point variables in C++: type float, type double, and type long double.

Type float Type float stores numbers in the range of about 3.4x10^–38 to 3.4x10^38. It occupies 4 bytes (32 bits) in memory.

Example 1

OUTPUT??

Here’s a sample interaction with the program:

Type double and long double The larger floating point types, double and long double, are similar to float except that they require more memory space and provide a wider range of values and more precision. Type double requires 8 bytes of storage and handles numbers in the range from 1.7x10^–308 to 1.7x10^308. Type long double is compiler-dependent but is often the same as double.

Floating-Point Constants The number 3.14159F is an example of a floating-point constant. The decimal point signals that it is a floating-point constant, and not an integer, and the F specifies that it’s type float, rather than double or long double. The number is written in normal decimal notation. You don’t need a suffix letter with constants of type double; it’s the default. With type long double, use the letter L.

Contdd . . . . You can also write floating-point constants using exponential notation. Exponential notation is a way of writing large numbers without having to write out a lot of zeros. For example,1,000,000,000 can be written as 1.0E9 in exponential notation.

The const Qualifier The keyword const (for constant) precedes the data type of a variable. It specifies that the value of a variable will not change throughout the program. Any attempt to alter the value of a variable defined with this qualifier will elicit an error message from the compiler.

The #define Directive Constants can also be specified using the preprocessor directive #define. Syntax is # define identifier replacement-text For example, the line appearing at the beginning of your program specifies that the identifier PI will be replaced by the text 3.14159 throughout the program.

The using Directive A namespace is a part of the program in which certain names are recognized; outside of the namespace they’re unknown. The directive says that all the program statements that follow are within the std namespace.

The using Directive Various program components such as cout are declared within this namespace. We can use it the other way, For example like this without using directive

Type bool Variables of type bool can have only two possible values: true and false Type bool is most commonly used to hold the results of comparisons. For example: Is var1 less than var2? If so, a bool value is given the value true; If not, it’s given the value false.

The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters wide, where n is the argument to setw(n). The value is right justified within the field.

OUTPUT

Example code

OUTPUT

Field width and setw N a m e : M d i h L q t R g # 9 - S E 9 - S E Without setw manipulator N a m e : M d i h L q t R g # 9 - S E

Variable Type Summary

Unsigned Data Types

Type Conversion

Automatic Conversions

Automatic Conversions When two operands of different types are encountered in the same expression, the lower-type variable is converted to the type of the higher-type variable.

Automatic Conversions In our example, the int value of count is converted to type float and stored in a temporary variable before being multiplied by the float variable avgWeight. The result (still of type float) is then converted to double so that it can be assigned to the double variable totalWeight.

Process of coversion 4 bytes These conversions take place invisibly

Casts In C++ Cast applies to data conversions specified by the programmer, as opposed to the automatic data conversions. Casts are also called type casts. What are casts for?

Casts Sometimes a programmer needs to convert a value from one type to another in a situation where the compiler will not do it automatically or without complaining. We have four specific casting operators: dynamic_cast <new_type> (expression) reinterpret_cast <new_type> (expression) static_cast <new_type> (expression) const_cast <new_type> (expression)

Example

QUESTIONS????