CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout << "The reciprocal of 10.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Types and Variables. Computer Programming 2 C++ in one page!
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CMT Programming Software Applications
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/8/08CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
CSE202: Lecture 10AThe Ohio State University1 Numerical Error.
String Escape Sequences
Basic Elements of C++ Chapter 2.
Chapter 2 Data Types, Declarations, and Displays.
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.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
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
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CPS120: Introduction to Computer Science
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
CSC 107 – Programming For Science. Announcements.
Chapter 2 Variables.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
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.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
A Sample Program #include using namespace std; int main(void) { cout
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
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.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Bill Tucker Austin Community College COSC 1315
Chapter 2: Introduction to C++
Introduction to C++ October 2, 2017.
Chapter 2: Introduction to C++
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
Chapter 2 Variables.
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Programming Fundamental-1
Presentation transcript:

CSE1222: Lecture 2The Ohio State University1

mathExample2.cpp // math example #include using namespace std; int main() { cout << "The reciprocal of 10 is " << 1.0/10.0 << endl; cout << "The square root of 10 is " << sqrt(10.0) << endl; cout << "e^(10.0) = " << exp(10.0) << endl; cout << "The reciprocal of 15 is " << 1.0/15.0 << endl; cout << "The square root of 15 is " << sqrt(15.0) << endl; cout << "e^(15.0) = " << exp(15.0) << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University2

mathExample2.cpp > g++ -o mathExample2.exe mathExample2.cpp > mathExample2.exe The reciprocal of 10 is 0.1 The square root of 10 is e^(10.0) = The reciprocal of 15 is The square root of 15 is e^(15.0) = e+06 > CSE1222: Lecture 2The Ohio State University3

Variables (1)  Programs like mathExample2.cpp have little use in practice…  There is no room for change They simply print the same output with the same values (using 10 and 15 only here) onto the screen every time they are executed  A more interesting program might have different behavior under different circumstances, i.e. with different values CSE1222: Lecture 2The Ohio State University4

Variables (2)  For instance, a program that asks for any number and outputs its reciprocal and square root is much more useful than mathExample2.cpp This program needs placeholders for the incoming values These placeholders are called variables CSE1222: Lecture 2The Ohio State University5

mathExample3.cpp // math example #include using namespace std; int main() { double x; // this is a variable declaration statement x = 10.0; cout << "The reciprocal of 10 is " << 1.0/x << endl; cout << "The square root of 10 is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl; x = 15.0; cout << "The reciprocal of 15 is " << 1.0/x << endl; cout << "The square root of 15 is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University6

Declaration Statements  C++ variable declarations are of the form: dataType variableName; dataType: int, float, char, double, unsigned int,... variableName: composed of alphanumeric characters or underscore ‘_’  Rule: Before you can use a variable in your program, you must declare it This allows the computer to set aside the memory that will be needed for that variable CSE1222: Lecture 2The Ohio State University7

Declaration Example #include using namespace std; int main() { int age; float wage; char initial; double height; return 0; // exit program } CSE1222: Lecture 2The Ohio State University8

Variable Names  Memorize these rules!  Composed of the characters: a, b, c,…, z, A, B, C,…, Z, 0, 1, 2,…, 9 and _  Must begin with: a, b, c,…, z, A, B, C,…, Z or _  Capitalized and lower case letters are different I.e., case sensitive  Example variable declarations (where int is the data type): int age;int age1; int Age;int age2; int myAge;int age3B; int Jacks_age;int _age; CSE1222: Lecture 2The Ohio State University9

Variable Names Which of these are valid variable names?  me2  Me2  2L8  N-Shareef  He_who_hesitates_is_lost  HeWhoHesitatesIsLost  Gordon.Gee CSE1222: Lecture 2The Ohio State University10

Variable Assignments  Variables can be assigned values during or after declaration, but never before (why?)  Assignment is done with the equals sign height = 67.34; initial = ‘E’; double totalPay = salary + overtime;  Why will the second line not work here as shown? height = 67.34; initial = E; // will not work as expected double totalPay = salary + overtime; CSE1222: Lecture 2The Ohio State University11

Variable Assignments  The equals sign is an operator like + or -  Variable assignment is NOT commutative!  Rule: The variable must always be on the left side of an equals sign  The following is NOT valid: 67 = x; CSE1222: Lecture 2The Ohio State University12

mathExample4.cpp // math example #include using namespace std; int main() { double x; cout << "Enter x: "; // Note: no new line cin >> x; // Note: operator ">>", not operator "<<" cout << "The reciprocal of " << x << " is " << 1.0/x << endl; cout << "The square root of " << x << " is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University13

Input Using cin (1)  cin (Console INput) can be used to obtain user input  Rule: Unlike cout, use >> with cin, and not <<  When the program is run, cin will wait indefinitely for user input  cin will input a single value into a variable when it detects a new line from the input  Remember that before inputting values into variables, the variables MUST have already been declared! CSE1222: Lecture 2The Ohio State University14 … double x; cout << "Enter x: "; // Note: no new line cin >> x; // Note: operator ">>", not operator "<<" …

> mathExample4.exe Enter x: 10.0 The reciprocal of 10 is 0.1 The square root of 10 is e^(10) = > CSE1222: Lecture 2The Ohio State University15 … int main() { double x; cout << "Enter x: ";// Note: no new line cin >> x; // Note: operator ">>", not operator "<<" cout << "The reciprocal of " << x << " is " << 1.0/x << endl; cout << "The square root of " << x << " is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl; …

mathExample4.cpp // math example #include using namespace std; int main() { double x; cout << "Enter x: "; // Note: no new line cin >> x; // Note: operator ">>", not operator "<<" cout << "The reciprocal of " << x << " is " << 1.0/x << endl; cout << "The square root of " << x << " is " << sqrt(x) << endl; cout << "e^(" << x << ") = " << exp(x) << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University16 Try inputs:

xyz1.cpp // multiple declarations example #include using namespace std; int main() { double x; double y; double z; cout << "Enter x, y and z: "; cin >> x; cin >> y; cin >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University17

> xyz1.exe Enter x, y and z: x = 1 y = 2 z = 3 > CSE1222: Lecture 2The Ohio State University18 … cout << "Enter x, y and z: "; cin >> x; cin >> y; cin >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; …

> xyz1.exe Enter x, y and z: x = 1 y = 2 z = 3 > CSE1222: Lecture 2The Ohio State University19 … cout << "Enter x, y and z: "; cin >> x; cin >> y; cin >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; …

Multiple Declarations  In the previous example we had: double x; double y; double z;  This can be done in one statement like this: double x, y, z;  This is very useful when creating several variables of the same type CSE1222: Lecture 2The Ohio State University20

xyz2.cpp // multiple declarations example #include using namespace std; int main() { double x, y, z; // multiple declarations cout << "Enter x, y and z: "; cin >> x; cin >> y; cin >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University21

xyz3.cpp // multiple declarations example #include using namespace std; int main() { double x, y, z; cout << "Enter x, y and z: "; cin >> x >> y >> z; // read x, then y, then z cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University22

Multiple Inputs Using cin  cin can be used to obtain multiple inputs  cin knows when to delimit I.e., start looking for the next input upon reaching a “whitespace”  Whitespaces are: tabs, spaces, new lines CSE1222: Lecture 2The Ohio State University23

> xyz1.exe Enter x, y and z: x = 1 y = 2 z = 3 > CSE1222: Lecture 2The Ohio State University24 … cout << "Enter x, y and z: "; cin >> x >> y >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; …

> xyz1.exe Enter x, y and z: x = 1 y = 2 z = 3 > CSE1222: Lecture 2The Ohio State University25 … cout << "Enter x, y and z: "; cin >> x >> y >> z; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "z = " << z << endl; …

Breaking up Multiple Inputs  Sometimes it makes more sense to break up multiple inputs into single inputs, even if they are correlated: int x, y, z; cout << “Enter x: “; cin >> x; cout << “Enter y: “; cin >> y; cout << “Enter z: “; cin >> z; CSE1222: Lecture 2The Ohio State University26

cin and cout Which of the following C++ statements have syntax errors and what are the errors? Assume that all variables have been declared and initialized  cout >> “Answer = ” >> x + y >> endl;  cin << x;  cout << “Yes, ” << “ or ” << “ no ” << “ or ” << “ maybe.” << endl;  cin >> yes >> no >> maybe;  cout << “x + y = ” (x + y) << endl; CSE1222: Lecture 2The Ohio State University27

Variables (3)  Variables are used to hold data The data is held in your computer’s main memory A program can read-from and write-to variables ○ I.e., their values can vary  Every variable consists of two parts: 1. The name of a variable is tied to a location in memory 2. Its data type (discussed next...) CSE1222: Lecture 2The Ohio State University28

Data Types  Integer  Real numbers  Characters  Strings, i.e. text  Boolean, i.e. true or false CSE1222: Lecture 2The Ohio State University29

Data Type: Integers (1)  An integer value is any number that has no decimal point (whole number) are all valid integers  1,244 is not a valid integer in C++ Commas are not allowed to express an integer  $12 is not valid either $ is not a valid part of an integer CSE1222: Lecture 2The Ohio State University30

Data Type: Integers (2)  What are the largest and smallest integers that a computer can support? Depends on the computer on which the program is compiled Most of today’s computers use 32-bits to represent an integer, so 2 32 values can be represented (how many is that??? … a lot)  Integers can be signed or unsigned What is the max value of an unsigned 32 bit integer? What is the max value of a signed 32 bit integer? CSE1222: Lecture 2The Ohio State University31

intExample1.cpp // example using type int #include using namespace std; int main() { int x, y; cout << "Enter x and y: "; cin >> x >> y; // Read in x and then y cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "x+y = " << x + y << endl; cout << "x/y = " << x / y << endl; cout << "Done." << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University32 Try inputs:

Integer Division  C++ Definition: A binary operator: a/b, where a and b are integers The integer division operator uses truncation The integer division operator always evaluates to an integer  What is (15 / 2) using the above definition? (15 / 2 ) is 7 Huh?? - Why???!  Remember that integers have no fractional parts!  What would the following output? cout << (15 / 16); There is no rounding in integer division The output would be 0  The fractional part of an integer division is truncated so the result can be an integer CSE1222: Lecture 2The Ohio State University33

Division  Modulus (Mod) operator (%) A binary integer operator Computes the remainder of dividing two integers  Decimal division Make at least one operand a floating- point number: cout << (15.0 / 16.0); CSE1222: Lecture 2The Ohio State University34

Data Types: Floating Point Numbers (1)  Floating-point numbers Have a decimal point Can be signed or unsigned  There are three basic types: float double long double The differences between these are their supported range and precision CSE1222: Lecture 2The Ohio State University35

Data Types: Floating Point Numbers (2)  To represent a floating point number: float uses 32 bits (4 bytes) double uses 64 bits (8 bytes) long double uses 128 bits (16 bytes)  The tradeoff is storage vs. precision and range  What exactly is the precision and range, and how are floating point numbers represented in binary format? IEEE 754 Standard CSE1222: Lecture 2The Ohio State University36

Data Types: Floating Point Numbers (3)  Let’s always use “ double ” to represent floating point numbers CSE1222: Lecture 2The Ohio State University37

Data Types: Floating Point Numbers (4)  Floating-point numbers can be written in exponential notation: or e or -3.45e-3  Here, e is short for “times ten to the power of”, just like in scientific notation CSE1222: Lecture 2The Ohio State University38

rationalExample1.cpp // example using type double #include using namespace std; int main() { double x, y; cout << "Enter x and y: "; cin >> x >> y; cout << "x = " << x << endl; cout << "y = " << y << endl; cout << "x+y = " << x + y << endl; cout << “x*y = " << x * y << endl; cout << “x/y = " << x / y << endl; cout << "Done." << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University39 Try inputs: e100 1e200

Data Type: Characters  Characters: All letters of the alphabet (upper and lower case, i.e. case sensitive) The symbolic representation of digits 0 – 9 All various symbols such as: + * & ^ % $ |, !  A character value or character literal Written in single quotes Consists of exactly one character E.g., 'A' or '8' or ':' or ' ' (blank space) CSE1222: Lecture 2The Ohio State University40

Data Type: Characters  NOTE: '8' and 8 are different '8' is the symbolic representation of the character literal, ‘8’; 8 is the integer 8, which can be used for arithmetic CSE1222: Lecture 2The Ohio State University41

Data Type: Characters  Characters are usually stored with 8 bits, i.e. 1 byte So, there are 2 8 (or 256) different characters Every number within the range of [0, 255] is mapped onto some character  A character is simply a numerical representation known as ASCII encoding CSE1222: Lecture 2The Ohio State University42

ASCII Code CSE1222: Lecture 2The Ohio State University43 CodeChar 32Space 33! 34" 35# 36$ 37% 38& 39' 40( 41) …… CodeChar …… CodeChar 65A 66B 67C 68D 69E 70F 71G 72H 73I 74J …… CodeChar 97a 98b 99c 100d 101e 102f 103g 104h 105i 106j ……

ASCII Table CSE1222: Lecture 2The Ohio State University44

charExample1.cpp // example using type char #include using namespace std; int main() { char c1, c2, c3; cout << "Enter first initial: "; cin >> c1; cout << "Enter second initial: "; cin >> c2; c3 = 'X'; cout << "Created by: "; cout << c1 << c2 << c3 << endl; return 0; // exit program } CSE1222: Lecture 2The Ohio State University45

> charExample1.exe Enter first initial: R Enter second initial: W {What is the output?} CSE1222: Lecture 2The Ohio State University46 … char c1, c2, c3; cout << "Enter first initial: "; cin >> c1; cout << "Enter second initial: "; cin >> c2; c3 = 'X'; cout << "Created by: "; cout << c1 << c2 << c3 << endl; …

Characters Strings  A character string is a sequence or array of zero or more characters  Examples: "Hello“ "Hello World!" ○ Note: Blank space is part of the string "He who hesitates is lost.\nHaste makes waste.\n“ ○ Note: \n is the new line character "" ○ The empty string, i.e. zero characters in the double quotes CSE1222: Lecture 2The Ohio State University47

Character Strings  NOTE: 'A' and "A" are different 'A' is the symbolic representation of the character literal, ‘A’; "A" is a string containing a single character  NOTE: '8' and "8" and 8 are different '8' is the symbolic representation of the character literal, ‘8’; "8" is a string containing a single character; 8 is the integer 8, which can be used for arithmetic CSE1222: Lecture 2The Ohio State University48

Data Type: Boolean  The Boolean data type is used for just two values: true and false Like characters, they only consume 1 byte of storage  Interesting note In C++, any number other than 0 is always interpreted as the Boolean value true E.g., the number 16 is considered to be a true value CSE1222: Lecture 2The Ohio State University49

Arithmetic Operations (1)  The basic operations of +, -, *, /, % These are binary operators; Simple arithmetic expressions of the form: operand operator operand 5 % 3 Defined separately for integers and real numbers  Remember % - An integer operator Modulus division only used for integers arguments Returns the remainder of dividing two integers This will come in handy in the future CSE1222: Lecture 2The Ohio State University50

Arithmetic Operations (2) // Examples of arithmetic operations #include using namespace std; int main() { cout << "5 % 3 = " << (5 % 3) << endl; cout << "4 - 3 = " << (4 - 3) << endl; cout << "5.0 / 2.0 = " << (5.0 / 2.0) << endl; cout << "5 / 2 = " << (5 / 2) << endl; // Is there any real difference in the last two statements? return 0; } CSE1222: Lecture 2The Ohio State University51

>arithmetic.exe 5 % 3 = = / 2.0 = / 2 = 2 > CSE1222: Lecture 2The Ohio State University52 … cout << "5 % 3 = " << (5 % 3) << endl; cout << "4 - 3 = " << (4 - 3) << endl; cout << "5.0 / 2.0 = " << (5.0 / 2.0) << endl; cout << "5 / 2 = " << (5 / 2) << endl; …