Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using...

39
Computer Science 1620 Accumulators
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using...

Computer Science 1620

Accumulators

Recall the solution to our financial program:#include <iostream>#include <cmath>#include <iomanip> 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;}

Recall the solution to our financial program:#include <iostream>#include <cmath>#include <iomanip> 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'tI use three differentvariables here?

Recall the solution to our financial program:#include <iostream>#include <cmath>#include <iomanip> 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'tI use three differentvariables here?

Recall the solution to our financial program:#include <iostream>#include <cmath>#include <iomanip> 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.

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

Accumulation

Expression Shorthand

x = x + 1 x += 1

x = x – 1 x -= 1

x = x * 1 x *= 1

x = x / 1 x /= 1

x = x % 1 x %= 1

Recall the solution to our financial program:#include <iostream>#include <cmath>#include <iomanip> 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;}

Recall the solution to our financial program:#include <iostream>#include <cmath>#include <iomanip> 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;}

+= 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

+= 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;

Recall the solution to our financial program:#include <iostream>#include <cmath>#include <iomanip> 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;}

Recall the solution to our financial program:

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

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

Example:write a program that reads in an integer,

and prints out the next 5 integers in sequence.

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

}

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

}

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

}

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 <iostream> 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)

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

}

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

}

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

}

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

}

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

}

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

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).

Example:#include <iostream> using namespace std;

int main() {

int num; num = 5; cout << ++num << endl; num = 5; cout << num++ << endl; return 0;

}

Operators

Increment/Decrement Examples

int i = 8;int j = i++; int k = ++i; int m = i--; int n = 9 + i++;

Operators

Increment/Decrement Examples

int i = 8; // i = 8int j = i++; // j = 8, i = 9int k = ++i; // k = 10, i = 10int m = i--; // m = 10, i = 9int n = 9 + i++; // n = 18, i = 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) 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 <iostream> 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;

}

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

int main() {

int start; cout << "Number: "; cin >> start;

cout << "Next number: " << ++start << endl;

cout << "Next number: " << ++start << endl;

cout << "Next number: " << ++start << endl;

cout << "Next number: " << ++start << endl;

cout << "Next number: " << ++start << 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) 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 <iostream> using namespace std;

int main() {

int start; cout << "Number: "; cin >> start;

cout << "Next number: " << start++ << endl;

cout << "Next number: " << start++ << endl;

cout << "Next number: " << start++ << endl;

cout << "Next number: " << start++ << endl;

cout << "Next number: " << start++ << endl;

return 0;

}

Operator Precedenceour precedence so far has been

Operator

( )

- (unary)

*, /, %

+, -

=

highest precedence

lowest precedence

Where do ++, --, +=, -=, *=, /=, and %= fit?

Operator Precedenceour precedence so far has been

Operator

( ), ++ (postfix), -- (postfix)

- and + (unary), ++(prefix), --(prefix)

*, /, %

+, -

=, +=, -=, *=, /=, %=

highest precedence

lowest precedence

Unary + and -:+ expr (evaluates to expr) - expr (evaluates to the value of expr

negatedExamples

+ 2 - 5 (-3) - 2 - 5 (-7) - (2 - 5) (3)

Operator

( ), ++ (postfix), -- (postfix)

- and + (unary), ++(prefix), --(prefix)

*, /, %

+, -

=, +=, -=, *=, /=, %=

Operator

( ), ++ (postfix), -- (postfix)

- and + (unary), ++(prefix), --(prefix)

*, /, %

+, -

=, +=, -=, *=, /=, %=

EXERCISES(some of the expr. do not compile)

int i = 2;

+-i++;-- i;+ + i;--5;- - 5;++i--;