Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0;

Similar presentations


Presentation on theme: "Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0;"— Presentation transcript:

1 Computer Science 1620 Accumulators

2 Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0; cout << fixed << showpoint << setprecision(2); 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; }

3 Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0; cout << fixed << showpoint << setprecision(2); balance = balance * 1.08; cout << "Year 1: $" << balance10 << endl; balance = balance * 1.08; cout << "Year 2: $" << balance20 << endl; balance = balance * 1.08; cout << "Year 3: $" << balance30 << endl; return 0; } Why didn't I use three different variables here?

4 Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0; cout << fixed << showpoint << setprecision(2); double balance1 = balance * 1.08; cout << "Year 1: $" << balance10 << endl; double balance2 = balance * 1.08 cout << "Year 2: $" << balance20 << endl; double balance3 = balance * 1.08; cout << "Year 3: $" << balance30 << endl; return 0; } Why didn't I use three different variables here?

5 Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0; cout << fixed << showpoint << setprecision(2); 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; } I can use the same variable in all three locations because I never need the "old" value again.

6 Accumulation this operation (assigning the value of an expression back to a variable that is involved in an expression) is very comon for each operation, the variable accumulates the results of the expressions sum, product, etc this is so common, that C++ allows a shorthand notation

7 Accumulation ExpressionShorthand x = x + 1x += 1 x = x – 1x -= 1 x = x * 1x *= 1 x = x / 1x /= 1 x = x % 1x %= 1

8 Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0; cout << fixed << showpoint << setprecision(2); 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; }

9 Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0; cout << fixed << showpoint << setprecision(2); balance *= 1.08; cout << "Year 1: $" << balance << endl; balance *= 1.08; cout << "Year 2: $" << balance << endl; balance *= 1.08; cout << "Year 3: $" << balance << endl; return 0; }

10 += as an Expression the operator += is a binary operator left hand side: variable right hand side: expression the entire operation represents an expression balance *= 1.08; this operation has a value

11 += as an Expression what is the value of this expression: Answer: the same as the value of its long-version expression: the value of += is whatever the new value being assigned to memory is balance *= 1.08; balance = balance * 1.08;

12 Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0; cout << fixed << showpoint << setprecision(2); balance *= 1.08; cout << "Year 1: $" << balance << endl; balance *= 1.08; cout << "Year 2: $" << balance << endl; balance *= 1.08; cout << "Year 3: $" << balance << endl; return 0; }

13 Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0; cout << fixed << showpoint << setprecision(2); cout << "Year 1: $" << (balance *= 1.08) << endl; cout << "Year 2: $" << (balance *= 1.08) << endl; cout << "Year 3: $" << (balance *= 1.08) << endl; return 0; }

14 Increment/Decrement a very common operation in programming is to increment/decrement an integer by 1 array traversal counting occurrences loop control it is so common that C++ has a special operator for doing this ++ and -- Syntax: ++ variable ++ variable prefix postfix

15 Example: write a program that reads in an integer, and prints out the next 5 integers in sequence.

16 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; return 0; }

17 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; return 0; }

18 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; return 0; }

19 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; return 0; } Identical code is good can be put into a loop (next topic)

20

21 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; return 0; }

22 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; start = start + 1; cout << "Next number: " << start << endl; return 0; }

23 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; start += 1; cout << "Next number: " << start << endl; start += 1; cout << "Next number: " << start << endl; start += 1; cout << "Next number: " << start << endl; start += 1; cout << "Next number: " << start << endl; start += 1; cout << "Next number: " << start << endl; return 0; }

24 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; start++; cout << "Next number: " << start << endl; start++; cout << "Next number: " << start << endl; start++; cout << "Next number: " << start << endl; start++; cout << "Next number: " << start << endl; start++; cout << "Next number: " << start << endl; return 0; }

25 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; ++start; cout << "Next number: " << start << endl; ++start; cout << "Next number: " << start << endl; ++start; cout << "Next number: " << start << endl; ++start; cout << "Next number: " << start << endl; ++start; cout << "Next number: " << start << endl; return 0; }

26 Increment/Decrement as Expressions the ++ and – operators represent expressions what is the value of the expression? the new value of the variable? ++start;start++; These two statements have a value

27 Answer: Depends if the prefix version is used, then the value of the expression is the new value of the variable if the postfix version is used, then the value of the expression is the old value of the variable cout << ++start << endl; cout << start++ << endl; This prints out the new value of start (after the increment). This prints out the old value of start (before the increment).

28 Example: #include using namespace std; int main() { int num; num = 5; cout << ++num << endl; num = 5; cout << num++ << endl; return 0; }

29 Operators Increment/Decrement Examples int i = 8; int j = i++; int k = ++i; int m = i--; int n = 9 + i++;

30 Operators Increment/Decrement Examples int i = 8;// i = 8 int j = i++;// j = 8, i = 9 int k = ++i;// k = 10, i = 10 int m = i--; // m = 10, i = 9 int n = 9 + i++; // n = 18, i = 10

31 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; ++start; cout << "Next number: " << start << endl; ++start; cout << "Next number: " << start << endl; ++start; cout << "Next number: " << start << endl; ++start; cout << "Next number: " << start << endl; ++start; cout << "Next number: " << start << endl; return 0; }

32 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; cout << "Next number: " << ++start << endl; return 0; }

33

34 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) ten years, b) twenty years c) thirty years Write a program that reads in an integer, and prints out the next 5 integers in sequence. #include using namespace std; int main() { int start; cout << "Number: "; cin >> start; cout << "Next number: " << start++ << endl; return 0; }

35

36 Operator Precedence our precedence so far has been Operator ( ) - (unary) *, /, % +, - = highest precedence lowest precedence Where do ++, --, +=, -=, *=, /=, and %= fit?

37 Operator Precedence our precedence so far has been Operator ( ), ++ (postfix), -- (postfix) - and + (unary), ++(prefix), -- (prefix) *, /, % +, - =, +=, -=, *=, /=, %= highest precedence lowest precedence

38 Unary + and -: + expr (evaluates to expr) - expr (evaluates to the value of expr negated Examples + 2 - 5 (-3) - 2 - 5 (-7) - (2 - 5) (3) Operator ( ), ++ (postfix), -- (postfix) - and + (unary), ++(prefix), -- (prefix) *, /, % +, - =, +=, -=, *=, /=, %=

39 Operator ( ), ++ (postfix), -- (postfix) - and + (unary), ++(prefix), -- (prefix) *, /, % +, - =, +=, -=, *=, /=, %= EXERCISES (some of the expr. do not compile) int i = 2; +-i++; -- i; + + i; --5; ++i--;


Download ppt "Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = 25000.0;"

Similar presentations


Ads by Google