Input & Output: Console

Slides:



Advertisements
Similar presentations
Types and Variables. Computer Programming 2 C++ in one page!
Advertisements

1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
© 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.
1 Chapter 2 C++ Syntax and Semantics, and the Program Development Process Dale/Weems/Headington.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Data types and variables
CS150 Introduction to Computer Science 1
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Basic Elements of C++ Chapter 2.
Objectives You should be able to describe: Data Types
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.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
1 C++ Syntax and Semantics, and the Program Development Process.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
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.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
1 A more complex example Write a program that sums a sequence of integers and displays the result. Assume that the first integer read specifies the number.
A Sample Program #include using namespace std; int main(void) { cout
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Bill Tucker Austin Community College COSC 1315
Chapter # 2 Part 2 Programs And data
Variables, Identifiers, Assignments, Input/Output
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Documentation Need to have documentation in all programs
Computing Fundamentals
Basic Elements of C++.
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2: Introduction to C++
2.1 Parts of a C++ Program.
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Chapter 1 c++ structure C++ Input / Output
Variables and Constants
Presentation transcript:

Input & Output: Console Console – term is left over from days of mainframes and refers to the monitor display and keyboard entry Input from the keyboard (console input) cin >> marked_price ; (Note: input goes INTO the memory storage location defined as marked_price) Output to the monitor screen (console output) cout << “Sales tax: “ << tax << endl; (Note: Output goes OUT OF the memory storage location tax to the monitor screen – along with some literal text and an ‘insert end of line’ indicator) << and >> are ‘operators’ indicating a direction of flow (stream insertion operators where ‘stream’ means a sequence of data)

Variables and Literals Fixed value, not subject to change during program execution Used to assign values to variables or output fixed content Conform to some standard type such as float, double, char, string, int Examples: 3.1415, “Hello”, 28, ‘Y’ Variable Value can change during execution of the code Has a name defined by the programmer Has a specific ‘type’ defined by the programmer (i.e. int, float, double, char) Has a storage location in memory that is assigned by the computer at the time the program is executed. The programmer refers to this storage location in his/her program using the name he/she used to define it. All variables are represented in the storage location by a sequence of 1’s and 0’s

Escape Sequences The \ (backslash) character has a special significance. It is used to ‘escape’ or change the meaning of the character that follows it. The combination of the \ and the following character is called an ‘escape sequence’ \n Newline (does same as endl) \t Inserts a tab \\ Causes a \ to be printed \’ Causes a ‘ to be printed \” Causes a “ to be printed Note: Never put a space in the middle of an escape sequence

Example: Using Escape Sequences //This is an example of a program that utilizes the console out object and escape sequences #include <iostream> using namespace std; int main() { cout << "Some Common Escape Sequences"; cout << endl; cout << "Newline\t\t\\n"; cout << "Tab\t\t\\t"; cout << “\n”; cout << "Backslash\t\\\\"; cout << "Single Quote\t\\\'"; cout << "Double Quote\t\\\""; return 0; }

Naming Variables Cannot use any of the C++ reserved (key) words (syntax) Should indicate the role of the variable in the program Should follow a naming convention (style) Variable names should be in all lower case with underscore between words ( Should be descriptive but moderate in length Names can only start with letter or underscore and may contain only letters, numbers and underscores Examples: body_weight_pounds or body_weight_lbs max_number_suitcases test_grade1 final_grade_average

Data Types & Variable Definitions Program variables must be defined not only by a name but also by a data type. The type declaration statement determines how the value of the variable is stored. Below are typical storage sizes and ranges for different types. Integers: short 2 bytes -32,768 to +32,767 unsigned short 2 bytes 0 to 65,535 int 4 bytes -2,147,483,648 to +2, 147,483,647 unsigned int 4 bytes 0 to 4,294,967,295 long 4 bytes -2,147,483,648 to +2, 147,483,647 unsigned long 4 bytes 0 to 4,294,967,295

Typical vs. Guaranteed What storage size is guaranteed on all systems? int is at least as large as short int long int is as least as large as int unsigned short is the same size as short unsigned int is the same size as int unsigned long is the same size as long The sizeof special operator can be used to determine the storage size on any given computing platform.

Floating Point (Real Numbers) Real numbers (floating point numbers) are stored in a format similar to scientific notation. Examples: 2,498,230.668 is 2.498231668 x 106 where 2.498231 is called the mantissa and 6 is the exponent .0034459 is 3.4459 x 10-3 where 3.4459 is the mantissa and -3 is the exponent For computer storage purposes these numbers would be written as 2498231668E6 and 34459E-3 A designated part of the allocated memory space is used for the mantissa and the rest for the exponent. One bit is used for the sign.

Floating Point Data Types Below are the data types with storage sizes typically found on PCs. float 4 bytes 3.4E-38 to 3.4E38 double 8 bytes 1.7E-308 to 1.7E308 long double 8 bytes 1.7E-308 to 1.7E308 Note: Remember the computer actually stores both the mantissa and exponent in binary. Although the exponent being an integer can be stored exactly, the mantissa may not be able to be stored exactly and at some point will be truncated. Ex. .1 or 1/10 decimal = 0.0001100110011... (binary) where … means the pattern repeats.

Char Data Type Characters are internally represented by numbers and thus can be thought of as a type of integer. Most systems use the ASCII (American Standard Code for Information Interchange) convention for the storage of characters. Strings are a sequence of characters stored in consecutive memory locations. We will be talking more about this later.

ASCII characters 0 to 127 Code 0 to 31 (and # 127) are non-printing, mostly obsolete control characters that affect how text is processed. There are 95 printable characters. To print one, press the ALT key (hold it down) and type the decimal number.

A popular variation of an extended ASCII set

Strings A string is a sequence of characters and is stored using the string ‘class’. You must include a preprocessor directive (#include <string>) if you want to use a string type variable. “”s must be around the literal assigned to a string variable. #include <string> string my_first_name, my_last_name; my_first_name = “Trish”; my_last_name = “Sumbera”’; The sequence of char that make up a string is stored in consecutive memory locations and the end is marked with a null (ASCII 0) character. Note: The ASCII code for the char 0 (zero) is 48. We use \0 to represent the null character. Also Note: char literals use single quotes. string literals use double quotes. ‘T’ takes up one byte of memory but “T” requires two bytes. Why?

bool Data Type bool (boolean) data have values of either ‘true’ or ‘false’ Needless to say this value is represented numerically where true is represented by 1 and false by 0

Named Constants Literals that may need to be changed in the future can be given a name. Use ALL_CAPS with words separated by _ (Note: This is a style issue and not part of the C++ language) Value cannot be changed while the program is running Defined using const <data type> <NAME> = <value>; Examples: const float PI = 3.1415; const int MAX_VACATION_HRS = 120; Are stored in a named memory location that is read-only #define preprocessor directive may be seen used in older programs Not stored in memory Text substitution occurs during preprocessor stage No defined data type

Binary Representation of Numbers As a programmer you should know how the computer stores numerical data in memory. EVERYTHING is stored using 0’s and 1’s. Binary representation of numbers is essential to understanding data types and some of the nuances resulting from binary representation. Let’s look at binary vs. decimal and how integers are stored in memory.