computer science 1620 variables and memory. review examples: write a program that calculates and...

97
Computer Science 1620 Variables and Memory

Post on 19-Dec-2015

219 views

Category:

Documents


0 download

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 <iostream> using namespace std;

int main() {

return 0;}

Write a program that calculates and displays the average of the numbers 45, 69, and 106.

#include <iostream> using namespace std;

int main() {

return 0;}

Write a program that calculates and displays the average of the numbers 45, 69, and 106.

#include <iostream> using namespace std;

int main() { cout << (45 + 69 + 106)/3 << endl;

return 0;}

Write a program that calculates and displays the average of the numbers 45, 69, and 106.

#include <iostream> using namespace std;

int main() { cout << (45 + 69 + 106)/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 <iostream> using namespace std;

int main() { cout << (45 + 69 + 106)/3.0 << endl;

return 0;}

Now this is ok, as the numeratorwill be promoted to a floating pointnumber.

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 <iostream> 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 <iostream> 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 <iostream> using namespace std;

int main() {

cout << "Year 1: $" << 25000 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 1.08 * 1.08 * 1.08 << endl;

return 0;}

The previous program works fine, but what do you notice?

#include <iostream> using namespace std;

int main() {

cout << "Year 1: $" << 25000 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 1.08 * 1.08 * 1.08 << endl;

return 0;} Repeat Calculations

Variablesa memory location that holds a valueAdvantages:

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 Viewwhen 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

1000

1004

1008

1012

1016

1020

1024

1028

1032

1036

Variablea 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 variablevariable declarations take the following

syntax:

type name

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 <iostream> 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 <iostream> 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 <iostream>#include <string> 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 aboutthe 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 <iostream> 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 Namesnot just any name for a variable is permittedmust be a valid identifier rules:

the first character must be a letter or underscoreonly letters, digits, and underscore may followyou 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?

kev a1234 1234a _1234

cs1620a single double lethbridge

007bond main _ return

kev a1234 1234a lethbridge

cs1620a _1234 single double

007bond main _ return

john a1234 1234a lethbridge

cs1620a _1234 single double

007bond main _ return

Assignmentvariable declarations reserve the memoryassignment allows us to store a value in

memoryuses the = operatorsyntax:

the value of the expression is placed in memory

variable name expression=

Assignment

#include <iostream> using namespace std;

int main() { int amount;

amount = 10;

return 0;

}

Program Memory:

Assignment

#include <iostream> 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 <iostream> 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 <iostream> using namespace std;

int main() { int amount; amount = 15 + 14;

return 0;}

29

Program Memory:

amounthence, you can include something other than a literalfor 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 <iostream> using namespace std;

int main() { double product; product = 15.0 * 14.0;

string name; name = “Letitia"; return 0;}

Retrievalvariable declarations reserve the memoryassignment allows us to store a value in

memoryhow 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 <iostream> using namespace std;

int main() { int amount;

amount = 10;

cout << amount << endl;

return 0;

}

Program Memory:Program:

Output:

#include <iostream> using namespace std;

int main() { int amount;

amount = 10;

cout << amount << endl;

return 0;

}

Program Memory:Program:

Output:

int amount;

amount

#include <iostream> using namespace std;

int main() { int amount;

amount = 10;

cout << amount << endl;

return 0;

}

10

Program Memory:Program:

Output:

amount = 10;

amount

#include <iostream> 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 + 24.0 << 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 firstAssignment 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 yearcan 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 <iostream> using namespace std;

int main() { cout << "Year 1: $" << 25000 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 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 <iostream> 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 <iostream> using namespace std;

int main() {

double balance = 25000.0;

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 <iostream> using namespace std;

int main() {

double balance = 25000.0;

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 <iostream> using namespace std;

int main() {

double balance = 25000.0;

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 <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

25000.0

Program Memory:Program:

Output:

balance

balance = 25000.0

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

25000.0

Program Memory:Program:

Output:

balance

balance * 1.08;

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

27000.0

Program Memory:Program:

Output:

balance

balance = balance * 1.08;

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

27000.0

Program Memory:Program:

Output:

balance

cout << "Year 1: $" << balance << endl;

Year 1: $27000

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

27000.0

Program Memory:Program:

Output:

balance

Year 1: $27000

balance * 1.08;

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

29160.0

Program Memory:Program:

Output:

balance

Year 1: $27000

balance = balance * 1.08;

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

29160.0

Program Memory:Program:

Output:

balance

Year 1: $27000

cout << "Year 2: $" << balance << endl;

Year 2: $29160

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

29160.0

Program Memory:Program:

Output:

balance

Year 1: $27000Year 2: $29160

balance * 1.08;

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

31492.8

Program Memory:Program:

Output:

balance

Year 1: $27000Year 2: $29160

balance = balance * 1.08;

#include <iostream> using namespace std;

int main() {

double balance = 25000.0;

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;}

31492.8

Program Memory:Program:

Output:

balance

Year 1: $27000Year 2: $29160

cout << "Year 3: $" << balance << endl;

Year 3: $31492.8

Compare the two programs:

int main() {

double balance = 25000.0;

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: $" << 25000 * 1.08 << endl; cout << "Year 2: $" << 25000 * 1.08 * 1.08 << endl; cout << "Year 3: $" << 25000 * 1.08 * 1.08 * 1.08 << endl; return 0;}

6 multiplications

3 multiplications

Assignment as Expressionassignment 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 expression1) In a cout statement

2) In an arithmetic expression

cout << (x = 10) << endl;

int y = (x=10) + 4;

These usesare quite uncommon

Assignment as an Expression3) 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 Expression3) 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 Expressionnote the previous examples:

assignment has very low precedencesecond 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 Expressionassignment is an example of an expression

that does more than simply represent a value

it (may) change the state of a variablesome 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: )32(9

5−= FC

Write a program that converts 20 degrees Farenheit to Celsius.

#include <iostream> using namespace std;

int main() {

return 0;}

)32(9

5−= FC

Write a program that converts 20 degrees Farenheit to Celsius.

#include <iostream> 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;}

)32(9

5−= FC

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 theredirect operator is opposite forthat of cout

The cin operator requires a variable. It is to this variablethat the inputted data is stored.

Write a program that converts any temperature farenheit to celsius.

#include <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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

-6.66667

Program Memory:Program:

Output:

farenheit

double celsius = (5.0 / 9.0)*(farenheit – 32);

Enter a temperature in Farenheit: 20

celsius

#include <iostream> 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

-6.66667

Program Memory:Program:

Output:

farenheit

cout << farenheit << " F = " << celsius << " C" << endl;

Enter a temperature in Farenheit: 20

celsius

20 F = -6.66667 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 <iostream> 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

streamwhen 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 <iostream> 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 6Please enter another number:

#include <iostream> 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 6Please enter another number:

• This cin statement retrieves the next number on the data stream, which is the 6 that was typed in previously.

#include <iostream> 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 6Please enter another number: 7 * 6 = 42