Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.

Slides:



Advertisements
Similar presentations
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
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.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Computer Science 1620 Loops.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Computer Science 1620 Programming & Problem Solving.
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Introduction to C Programming
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
COMPUTER SCIENCE I C++ INTRODUCTION
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
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Variables, Data Types and Constants Yared Semu Addis Ababa Institute of Technology Mar 31, 2012.
Input & Output: Console
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
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.
Lecture 3: The parts of a C++ program Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
Chapter 2: Introduction to C++. Outline Basic “Hello World!!” Variables Data Types Illustration.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
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
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
A Sample Program #include using namespace std; int main(void) { cout
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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 Topics The Basics of a C++ Program Data Types
Chapter 3 Assignment and Interactive Input.
Computing Fundamentals
Introduction to C++ Programming
Variables, Identifiers, Assignments, Input/Output
Chapter 3: Input/Output
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Presentation transcript:

Computer Science 1620 Variables and Memory

Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.

Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { return 0; }

Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { return 0; }

Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { cout << ( )/3 << endl; return 0; }

Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { cout << ( )/3 << endl; return 0; } We receive an incorrect answer, due to integer division dropping the decimal part.

Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { cout << ( )/3.0 << endl; return 0; } Now this is ok, as the numerator will be promoted to a floating point number.

Review Examples: suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years #include using namespace std; int main() { return 0; }

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years #include using namespace std; int main() { return 0; } After one year, the fund will be worth 1.08 times what it was initially After two years, the fund will be worth 1.08 times what it was after one year After three years, the fund will be worth 1.08 times what it was after two year

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years #include using namespace std; int main() { cout << "Year 1: $" << * 1.08 << endl; cout << "Year 2: $" << * 1.08 * 1.08 << endl; cout << "Year 3: $" << * 1.08 * 1.08 * 1.08 << endl; return 0; }

The previous program works fine, but what do you notice? #include using namespace std; int main() { cout << "Year 1: $" << * 1.08 << endl; cout << "Year 2: $" << * 1.08 * 1.08 << endl; cout << "Year 3: $" << * 1.08 * 1.08 * 1.08 << endl; return 0; } Repeat Calculations

Variables a memory location that holds a value Advantages: allow us to avoid repeated computation store a result, rather than recompute it allow us to read user input coming soon!! to understand C++ variables requires a little knowledge on how memory works

Program Memory - a Simplified View when you run a program your operating system (Windows, Linux, MacOS) allocates a spot in memory for the program to run a certain portion of memory holds the instructions the machine instructions to be executed by the processor the remaining portion is dedicated to storing data

Variables Program Memory your program's memory is a sequence of contiguous blocks each block has an address it is in this memory that you can store data

Variable a variable is named memory storage that is, a variable is a location in memory that can be referenced by name a variable allows the programmer access to a memory location, to store and retrieve data

Variables – Declaration to store a value using a variable, you must first declare the variable variable declarations take the following syntax: typename type refers to the type of the value to be stored (such as int, double). It defines how much memory the variable is given. name is the name of the variable. This is how you will refer to this value from now on.

#include using namespace std; int main() { int amount; return 0; } Example: amount is the name of the variable. From this point forward, when the program sees the word amount, it knows you are referring to the value stored at that variable. The variable's type is an int. This means that only integers can be stored in this variable. It also means that this variable will get 4 bytes of memory.

#include using namespace std; int main() { double amount; return 0; } Example 2: The variable's type is an double. It means that this variable will get 8 bytes of memory.

#include using namespace std; int main() { string name; return 0; } Example 2: The variable's type is an string. The amount of memory it gets will vary with the size of the stored string. We will have much to say about the string type later on (when we talk about arrays). For now, just treat it like the other data types. To use the string type, you must add this include statement.

Behind the scenes – Variable Declaration #include using namespace std; int main() { int amount; return 0; } Program Memory: when a variable is declared, C++ reserves a spot in memory for that variable. amount

Variable Names not just any name for a variable is permitted must be a valid identifier rules: the first character must be a letter or underscore only letters, digits, and underscore may follow you must not use a keyword a word already used in C++ (like int) some compilers have limits on identifier length

Variable Names what is a valid name, and what is invalid? keva a_1234 cs1620asingledoublelethbridge 007bondmain_return keva alethbridge cs1620a_1234singledouble 007bondmain_return johna alethbridge cs1620a_1234singledouble 007bondmain_return

Assignment variable declarations reserve the memory assignment allows us to store a value in memory uses the = operator syntax: the value of the expression is placed in memory variable nameexpression =

Assignment #include using namespace std; int main() { int amount; amount = 10; return 0; } Program Memory:

Assignment #include using namespace std; int main() { int amount; amount = 10; return 0; } Program Memory: when a variable is declared, C++ reserves a spot in memory for that variable. int amount; amount

Assignment #include using namespace std; int main() { int amount; amount = 10; return 0; } 10 Program Memory: when a variable is assigned, the value of the expression is placed in memory amount = 10; amount

Note that the right hand side takes an expression #include using namespace std; int main() { int amount; amount = ; return 0; } 29 Program Memory: amount hence, you can include something other than a literal for now, assume that the type of the variable and the type of the value should be the same

Assignment also applies to doubles and strings as well #include using namespace std; int main() { double product; product = 15.0 * 14.0; string name; name = “Letitia"; return 0; }

Retrieval variable declarations reserve the memory assignment allows us to store a value in memory how do we retrieve the value? the variable name itself can be used as an expression when a variable is used as an expression, the value of that expression is whatever is being stored at that variable

#include using namespace std; int main() { int amount; amount = 10; cout << amount << endl; return 0; } Program Memory: Program: Output:

#include using namespace std; int main() { int amount; amount = 10; cout << amount << endl; return 0; } Program Memory: Program: Output: int amount; amount

#include using namespace std; int main() { int amount; amount = 10; cout << amount << endl; return 0; } 10 Program Memory: Program: Output: amount = 10; amount

#include using namespace std; int main() { int amount; amount = 10; cout << amount << endl; return 0; } 10 Program Memory: Program: Output: cout << amount << endl; amount 10 amount in this case is an expression the value of that expression is the value stored in amount's memory

Where else can a variable be used as an expression? anywhere an expression is allowed in an arithmetic expression in an assignment statement double amount; amount = 10.0; cout << amount << endl; string name; name = ”Portia"; string name2; name2 = name;

Note: Assignment is an operator has lower precedence than arithmetic operator hence, variable can be used in its own assignment int amount; amount = 10; amount = amount + 20; Arithmetic Expression evaluated first Assignment evaluated after.

Another Note: variable declaration and assignment can be combined into one statement can also be written: int amount; amount = 10; int amount = 10;

Returning to our previous example: calculate the yearly balance on an investment making 8% interest per year can we improve our original solution with a variable?

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years #include using namespace std; int main() { cout << "Year 1: $" << * 1.08 << endl; cout << "Year 2: $" << * 1.08 * 1.08 << endl; cout << "Year 3: $" << * 1.08 * 1.08 * 1.08 << endl; return 0; }

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years #include using namespace std; int main() { }

Suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to calculate how much the fund will be worth after a) one year, b) two years c) three years #include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; }

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output:

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance double balance

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance balance =

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance balance * 1.08;

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance balance = balance * 1.08;

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance cout << "Year 1: $" << balance << endl; Year 1: $27000

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance Year 1: $27000 balance * 1.08;

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance Year 1: $27000 balance = balance * 1.08;

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance Year 1: $27000 cout << "Year 2: $" << balance << endl; Year 2: $29160

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance Year 1: $27000 Year 2: $29160 balance * 1.08;

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance Year 1: $27000 Year 2: $29160 balance = balance * 1.08;

#include using namespace std; int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } Program Memory: Program: Output: balance Year 1: $27000 Year 2: $29160 cout << "Year 3: $" << balance << endl; Year 3: $

Compare the two programs: int main() { double balance = ; balance = balance * 1.08; cout << "Year 1: $" << balance << endl; balance = balance * 1.08; cout << "Year 2: $" << balance << endl; balance = balance * 1.08; cout << "Year 3: $" << balance << endl; return 0; } int main() { cout << "Year 1: $" << * 1.08 << endl; cout << "Year 2: $" << * 1.08 * 1.08 << endl; cout << "Year 3: $" << * 1.08 * 1.08 * 1.08 << endl; return 0; } 6 multiplications 3 multiplications

Assignment as Expression assignment is a binary operator similar to +, -, *, /, % right to left associative assignment is actually an expression that is, the assignment statement has a value the value of an assignment is: the value being placed in the variable's memory int x; x = 10; This statement has a value.

Assignment as an Expression this means we can use assignment wherever we use an expression 1) In a cout statement 2) In an arithmetic expression cout << (x = 10) << endl; int y = (x=10) + 4; These uses are quite uncommon

Assignment as an Expression 3) In another assignment statement this use is actually quite common that is, if you are setting two or more variables to the same value, you can use one assignment as an expression for the other assignment int x, y; x = y = 25; int a,b,c,d,e,f; a = b = c = d = e = f = 0;

Assignment as an Expression 3) In another assignment statement The order in which assignment operator is evaluated: RIGHT to LEFT! int x, y; x = y = 25; 25

Assignment as an Expression note the previous examples: assignment has very low precedence second lowest (tied with others) when using assignment as an expression, you should usually use parentheses int y = (x=10) + 4; cout << (x = 10) << endl;

Assignment as Expression assignment is an example of an expression that does more than simply represent a value it (may) change the state of a variable some other expressions have this effect as well therefore, we require an updated definition: an expression is an entity in C++ that (1) always has a value and (2) sometimes changes the state of the program

Write a program that converts 20 degrees farenheit to celsius. Formula:

Write a program that converts 20 degrees Farenheit to Celsius. #include using namespace std; int main() { return 0; }

Write a program that converts 20 degrees Farenheit to Celsius. #include using namespace std; int main() { double farenheit = 20.0; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; }

What do you notice about the previous program? what happens if the temperature is 15 degrees? the program would have to be edited, then re- compiled that is, all of our programs so far, once compiled, produce exactly the same output

cin console input allows the user to enter data into your program this data can be stored and manipulated in the same manner as the other data in your program Syntax: cin >> variable Notice that the direction of the redirect operator is opposite for that of cout The cin operator requires a variable. It is to this variable that the inputted data is stored.

Write a program that converts any temperature farenheit to celsius. #include using namespace std; int main() { double farenheit = 20.0; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; }

Write a program that converts any temperature farenheit to celsius. #include using namespace std; int main() { double farenheit = 20.0; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; }

Write a program that converts any temperature farenheit to celsius. #include using namespace std; int main() { double farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; }

Write a program that converts any temperature farenheit to celsius. #include using namespace std; int main() { double farenheit; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; }

When you use the cin operator: the program simply stops and waits for some data (and the enter key to be pressed) it is usually a good idea to display instructions for the user to input some data (using cout) otherwise, the impression may be that the program is not responding this is known as prompting the user

Write a program that converts any temperature farenheit to celsius. #include using namespace std; int main() { double farenheit; cout << "Please enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; }

#include using namespace std; int main() { double farenheit; cout << "Enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; } Program Memory: Program: Output:

#include using namespace std; int main() { double farenheit; cout << "Enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; } Program Memory: Program: Output: farenheit double farenheit;

#include using namespace std; int main() { double farenheit; cout << "Enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; } Program Memory: Program: Output: farenheit cout << "Enter a temperature in Farenheit:"; Enter a temperature in Farenheit:

#include using namespace std; int main() { double farenheit; cout << "Enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; } Program Memory: Program: Output: farenheit Enter a temperature in Farenheit: 20 cin >> farenheit;

#include using namespace std; int main() { double farenheit; cout << "Enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; } 20 Program Memory: Program: Output: farenheit cin >> farenheit; Enter a temperature in Farenheit: 20

#include using namespace std; int main() { double farenheit; cout << "Enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; } 20 Program Memory: Program: Output: farenheit double celsius Enter a temperature in Farenheit: 20 celsius

#include using namespace std; int main() { double farenheit; cout << "Enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; } Program Memory: Program: Output: farenheit double celsius = (5.0 / 9.0)*(farenheit – 32); Enter a temperature in Farenheit: 20 celsius

#include using namespace std; int main() { double farenheit; cout << "Enter a temperature in Farenheit:"; cin >> farenheit; double celsius = (5.0 / 9.0)*(farenheit – 32); cout << farenheit << " F = " << celsius << " C" << endl; return 0; } Program Memory: Program: Output: farenheit cout << farenheit << " F = " << celsius << " C" << endl; Enter a temperature in Farenheit: 20 celsius 20 F = C

Multiple Inputs just as cout can accept multiple expressions at once, cin can accept multiple variables at once: Example: int left; int right; cin >> left >> right;

Notes about multiple inputs: the variables are filled from left to right the input values are separated by whitespace whitespace is ignored by cin (by default) cin >> left >> right; left receives the first value, right receives the second value.

Example: Write a program that accepts two numbers, and computes and displays their product: #include using namespace std; int main() { int num1; int num2; cout << "Please enter two numbers:"; cin >> num1 >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; }

cin – Low level details the cin operator is an example of a data stream when a user enters input, the input waits on the data stream until the cin operator accepts it this can sometimes produce some unexpected results

Example: Rewrite the multiplication program to only take one number at a time: #include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; }

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } Program Memory: Program: Output:

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } Program Memory: Program: Output: int num1; num1

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } Program Memory: Program: Output: int num2; num1 num2

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } Program Memory: Program: Output: cout << "Please enter a number:"; num1 num2 Please enter a number:

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } Program Memory: Program: Output: cin >> num1; num1 num2 Please enter a number:

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } Program Memory: Program: Output: cin >> num1; num1 num2 Please enter a number: 7 6 The user has entered two numbers into the data stream. cin only reads one of them. the other number is still sitting on the data stream, waiting to be read.

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } 7 Program Memory: Program: Output: cin >> num1; num1 num2 Please enter a number: 7 6 The user has entered two numbers into the data stream. cin only reads one of them. the other number is still sitting on the data stream, waiting to be read.

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } 7 Program Memory: Program: Output: cout << "Please enter another number:"; num1 num2 Please enter a number: 7 6 Please enter another number:

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } 7 6 Program Memory: Program: Output: cin >> num2; num1 num2 Please enter a number: 7 6 Please enter another number: This cin statement retrieves the next number on the data stream, which is the 6 that was typed in previously.

#include using namespace std; int main() { int num1; int num2; cout << "Please enter a number:"; cin >> num1; cout << "Please enter another number:"; cin >> num2; cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; return 0; } 7 6 Program Memory: Program: Output: cout << num1 << " * " << num2 << " = " << (num1 * num2) << endl; num1 num2 Please enter a number: 7 6 Please enter another number:7 * 6 = 42