Download presentation
Presentation is loading. Please wait.
1
Computer Science 1620 Variables and Memory
2
Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
3
Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { return 0; }
4
Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { return 0; }
5
Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { cout << (45 + 69 + 106)/3 << endl; return 0; }
6
Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include 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.
7
Write a program that calculates and displays the average of the numbers 45, 69, and 106. #include using namespace std; int main() { cout << (45 + 69 + 106)/3.0 << endl; return 0; } Now this is ok, as the numerator will be promoted to a floating point number.
8
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
9
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; }
10
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
11
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: $" << 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; }
12
The previous program works fine, but what do you notice? #include 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
13
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
14
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
15
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
16
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
17
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.
18
#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.
19
#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.
20
#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.
21
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
22
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
23
Variable Names what is a valid name, and what is invalid? keva12341234a_1234 cs1620asingledoublelethbridge 007bondmain_return keva12341234alethbridge cs1620a_1234singledouble 007bondmain_return johna12341234alethbridge cs1620a_1234singledouble 007bondmain_return
24
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 =
25
Assignment #include using namespace std; int main() { int amount; amount = 10; return 0; } Program Memory:
26
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
27
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
28
Note that the right hand side takes an expression #include using namespace std; int main() { int amount; amount = 15 + 14; 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
29
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; }
30
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
31
#include using namespace std; int main() { int amount; amount = 10; cout << amount << endl; return 0; } Program Memory: Program: Output:
32
#include using namespace std; int main() { int amount; amount = 10; cout << amount << endl; return 0; } Program Memory: Program: Output: int amount; amount
33
#include using namespace std; int main() { int amount; amount = 10; cout << amount << endl; return 0; } 10 Program Memory: Program: Output: amount = 10; amount
34
#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
35
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;
36
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.
37
Another Note: variable declaration and assignment can be combined into one statement can also be written: int amount; amount = 10; int amount = 10;
38
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?
39
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: $" << 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; }
40
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() { }
41
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 = 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; }
42
#include 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:
43
#include 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
44
#include 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
45
#include 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;
46
#include 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;
47
#include 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
48
#include 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;
49
#include 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;
50
#include 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
51
#include 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 Year 2: $29160 balance * 1.08;
52
#include 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: $27000 Year 2: $29160 balance = balance * 1.08;
53
#include 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: $27000 Year 2: $29160 cout << "Year 3: $" << balance << endl; Year 3: $31492.8
54
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
55
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.
56
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
57
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;
58
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
59
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;
60
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
61
Write a program that converts 20 degrees farenheit to celsius. Formula:
62
Write a program that converts 20 degrees Farenheit to Celsius. #include using namespace std; int main() { return 0; }
63
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; }
64
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
65
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.
66
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; }
67
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; }
68
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; }
69
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; }
70
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
71
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; }
72
#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:
73
#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;
74
#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:
75
#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;
76
#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
77
#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
78
#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 -6.66667 Program Memory: Program: Output: farenheit double celsius = (5.0 / 9.0)*(farenheit – 32); Enter a temperature in Farenheit: 20 celsius
79
#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 -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
80
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;
81
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.
82
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; }
84
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
85
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; }
88
#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:
89
#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
90
#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
91
#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:
92
#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:
93
#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.
94
#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.
95
#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:
96
#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.
97
#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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.