Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program.

Slides:



Advertisements
Similar presentations
Class 2 ssh, memory, data types, variables
Advertisements

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. About the book Ivor Horton, Beginning ANSI C++: The Complete.
Types and Variables. Computer Programming 2 C++ in one page!
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
IT-101 Section 001 Lecture #4 Introduction to Information Technology.
Binary Number Systems.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
Cosc175/data1 Data comprised of constants and variables information stored in memory Each memory location has an address Address - number identifying a.
Chapter 2: C Fundamentals Dr. Ameer Ali. Overview C Character set Identifiers and Keywords Data Types Constants Variables and Arrays Declarations Expressions.
Bits & Bytes: How Computers Represent Data
Elements of a C++ program 1. Review Algorithms describe how to solve a problem Structured English (pseudo-code) Programs form that can be translated into.
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.
Input & Output: Console
C Tokens Identifiers Keywords Constants Operators Special symbols.
Numeric Types, Expressions, and Output ROBERT REAVES.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Sales person receive RM200/week plus 9% of their gross sales for that week. Write an algorithms to calculate the sales person’s earning from the input.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Ivor Horton, Beginning ANSI C++: The Complete Language, 3 rd.
Lecture #5 Introduction to C++
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Loops: Repeating One or More Statements for loop while loop.
Lesson 6 Getting Started in C Programming Hu Junfeng 2007/10/10.
 All C programs are made up of functions that perform operations on variables.  In this lecture we examine variables  Variables are the basic building.
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.
Ch Chapter 4 Basic Data Types and Variables 4.1 Basic Data Types In C TABLE 4.1 Introduction to Basic Data Types in C Type SizeDescription char 1.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Variables Symbol representing a place to store information
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
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.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Characters and Strings
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
INTRODUCTION TO ‘C’ PROGRAMMING BY Prof. P. PADMANABHAM M.Tech (AE), M.Tech(CS), Ph.D(CS)-FIETE, FIE Director Academics, Bharat Institute Of Engineering.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
1 Objects Types, Variables, and Constants Chapter 3.
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
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.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
3.1 Denary, Binary and Hexadecimal Number Systems
Tokens in C Keywords Identifiers Constants
Bits & Bytes How Computers Represent Data
EPSII 59:006 Spring 2004.
Variable Symbol represents a place to store information
C++ Data Types Data Type
Lectures on Numerical Methods
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Variables and Constants
Programming Fundamental-1
Presentation transcript:

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Basic Data Type and calculations Data types in C++ Program literals Binary and hexadecimal representation for integers Declare and initialize variables in program How calculations using integers work Floating-point calculations Constant and variables

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Data and data types Integer literals Floating point literals Binary literals  0, 1 Octal laterals  0, …, 7 Decimal digit  0, …, 9 Hexadecimal digit  0, …, 9, A, B, C, D, E, F

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Storage Unit 1.One byte = 8 bits 2.1 Kilo Byte (KB) = 1024 Bytes 3.1 Mega Byte (MB) = 1024 KBs 4.1 Giga Byte (GB) = 1024 MBs 5.1 Tera Byte (TB) = 1024 GBs = 2 10 GBs 6.1 Peta Byte (PB) = 1024 TBs 7.1 Exa Byte (EB) = 1024 PBs = 2 60 Bytes

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Number system Binary system – 0, 1 Octal system – 0,1,2,3,4,5,6,7 –Leading by ‘0’, such as 0256 Hexadecimal system –0,1,,2,3,4,5,6,7,8,9,A,B,C,D,E,F –Leading by ‘0x’ or ‘0X’, such as 0X1D3C Decimal system –0,1,2,3,4,5,6,7,8,9

Number interchange to decimal Binary to decimal –( ) 2 = =(26.375) 10 Octal to decimal –(524.4) 8 =5*64+2*8+4+4*0.125=(340.5) 10 Hexadecimal to decimal –(C2A.D) 16 =12*256+2* *0.0625=( ) 10

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Number interchange (59.75) 10 = ( ) 2 = (3B.C) 16 = (73.6) 8 (25.4) 10 = ( …) 2 = ( …) 8 = ( …) 16

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Binary Operation ( ) + ( ) = ( ) 1’s complement of ( ) → ( ) 2’s complement of ( ) → ( ) = 1’s complement + 1 ( ) – ( ) = ( ) + ( ) = ( )

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Variable Types Integer variables –char (1byte) –short (2 bytes) –int (4 bytes) –long (4 or 8 bytes) Floating variables –float (4 bytes) –double (8 bytes) –long double (8 or 10 bytes)

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Integer Domain in VC++ TypeSize (bytes)Domain char1-128 ~ 127 unsigned char10U ~ 255U short ~ unsigned short20U ~ 65535U int ~ unsigned int40U ~ U long L~ L unsigned long40UL ~ UL

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Numeric Operators Plus, positive  + Minus, negative  - Multiply  * Divide  / Modulus  % Increment  ++ Decrement  -- Assignment operators  =, +=, -=, *=, /=, %=, &=, |=, ^=, >= Programs 2.1, 2.1A, 2.2, 2.3

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

op= Assignment Operator apples *= oranges + 2; apples = apples * (oranges + 2); oranges += 2; sum += i++; // sum +=i; i=i+1; sum += ++i; // i = i+1; sum +=i; total=++count+6; // count++; total=count+6; total=count++ +6; // total=count+6; count++;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. The const keyword Constant –Magic numbers, such as PI, exponential number, unit, etc. –Values or contents that do not or cannot change or vary Program 2.4

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Numeric functions for integers #include “math.h” or #include using namespace std;

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Random numbers #include // standard library #include // timing function // random seed Program 2.5

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating numbers In VC++ “ e003” represent the above number

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating-point data types TypeSize (bytes) Decimal precision Domain float46≒ 1.2x ~ 3.4x10 38 double815≒ 2.2x ~ 1.8x long double 8 (10)15 (19)≒ 2.2x ~ 1.8x (3.3x ~ 1.2x )

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Floating Operation Program 2.6 The float.h file Program 2.7 –Floating point error –Never rely on an exact floating-point representation of a decimal value in your program code Program 2.8 –Floating point output manupilator

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC.

Escape sequences

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Character Literals Program 2.9 App. A ASCII code Extended character set –ASCII code 128 ~ 255 –Wide character 2 or 4 bytes character (wchar_t) Uicode defined international character set

Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Initialization int unlucky = 13; is same as int unlucky(13); char letter(‘A’); is same as char letter = 65;