Skip to content

Commit

Permalink
All chapters' source material finished
Browse files Browse the repository at this point in the history
Advanced Arithmetic & arithmetic are "semi-final" -- unless I see major
issues there, they're done. See chapter-status.txt for details on where
we are.
  • Loading branch information
jeremyhansen committed Nov 5, 2013
1 parent 52be944 commit 2cae4b0
Show file tree
Hide file tree
Showing 10 changed files with 550 additions and 296 deletions.
282 changes: 130 additions & 152 deletions chap_advancedarith.tex

Large diffs are not rendered by default.

220 changes: 115 additions & 105 deletions chap_arithmetic.tex
@@ -1,33 +1,33 @@
%Arithmetic

One of the most important things provided by C++ is the ability to do math.
Everything a computer sees is a number. \
Everything a computer sees is a number.
To a computer, its ability to do math and manipulate numbers is as essential to it as breathing is to us.
(My apologies to anything not living that may be reading this).

The operators (\Code{+}, \Code{-}, \Code{*}, \Code{/}) in C++ are slightly different from what you may be used to from your second-grade math class.
Addition is still a plus sign ( \Code{+} ) and subtraction is still a minus sign ( \Code{-} ).
On the other hand, multiplication becomes an asterisk ( \Code{*} ) and division becomes a forward slash ( \Code{/} ).
Think of it as \textit{over} as in ``5 over 9'' is the same as the fraction $5/9$.
Think of it as \textit{over} as in ``5 over 9'' is the same as the fraction $5/9$ or $\frac{5}{9}$.

To do math in C++, you will either want a variable to store the answer, or output the answer to the user.

The following code directly outputs the answer to the user:

\begin{lstlisting}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
cout << 9 + 2;
\end{lstlisting}
\end{lstlisting}\end{minipage}

This code shows how to use a variable to store the answer:

\begin{lstlisting}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int sum = 9 + 2;
\end{lstlisting}
\end{lstlisting}\end{minipage}

Note that when you use a variable to store an answer, the variable must come first in the equation (before the equal sign) and must be the only thing on the left side of the equation.
There are some other things to note.
When you use more complicated equations, you can use parentheses to help.
C++ uses a familiar order of operations (Parentheses, Exponents, Multiply, Divide, Add, and Subtract, or PEMDAS), but without the exponent operation (this topic is covered in Chapter \ref{Advanced Arithmetic}).
C++ uses a familiar order of operations (Parentheses, Exponents, Multiply, Divide, Add, and Subtract, or PEMDAS), but without the exponent operation (this topic is covered in Chapter~\ref{chap_advancedarith}).
However, unlike in normal arithmetic, parentheses do not imply multiplication.
\Code{(4)(3)} does not mean the same as \Code{4~*~3}.
\Code{(4)(3)} results in a syntax error and does not compile.
Expand All @@ -43,44 +43,44 @@
Starting with similarities, \Code{C++} and \Code{++C} both increase value of \Code{C} by one.
The difference lies in when another variable is being set to that incremented value, such as \Code{B = C++}.
\Code{B} will be set to \Code{C} before \Code{C} is incremented.
\Code{B = ++C} will cause \Code{B} to be set to \Code{C+1}, in a similar way to \Code{B = 1 + C}.
\Code{B = ++C} will cause \Code{B} to be set to \Code{C+1}, in a similar way to \Code{B~=~1~+~C}.

\begin{lstlisting}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int A;
A = 4;
A++;
//A contains 5
\end{lstlisting}
\begin{lstlisting}
\end{lstlisting}\end{minipage}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int A;
A = 9;
A--;
//A contains 8
\end{lstlisting}
\begin{lstlisting}
\end{lstlisting}\end{minipage}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int A, B;
B = 7;
A = B++;
//A contains 7, B contains 8
\end{lstlisting}
\begin{lstlisting}
\end{lstlisting}\end{minipage}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int A, B;
B = 7;
A = ++B;
//A contains 8, B contains 8
\end{lstlisting}
\begin{lstlisting}
\end{lstlisting}\end{minipage}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int A, B;
B = 3;
A = B--;
//A contains 3, B contains 2
\end{lstlisting}
\begin{lstlisting}
\end{lstlisting}\end{minipage}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int A, B;
B = 3;
A = --B;
//A contains 2, B contains 2
\end{lstlisting}
\end{lstlisting}\end{minipage}

Compound assignment operators can decrease the amount you type and can make your code more readable.
These are the operators \Code{+=}, \Code{-=}, \Code{*=}, and \Code{/=}.
Expand All @@ -105,156 +105,166 @@

Here is some sample code using the concepts we presented in this chapter:

\begin{lstlisting}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
#include <iostream>

using namespace std;

int main()
{
int a = 5, b = 10, c = 15, d = 20;
int a = 5, b = 10, c = 15, d = 20;

cout << "a + b = " << a + b << endl;
cout << "d - c = " << d - c << endl;
cout << "a * b = " << a * b << endl;
cout << "d / a = " << d / a << endl;
cout << "a + b = " << a + b << endl;
cout << "d - c = " << d - c << endl;
cout << "a * b = " << a * b << endl;
cout << "d / a = " << d / a << endl;
}
\end{lstlisting}
\end{lstlisting}\end{minipage}

The output of this code is:

\begin{lstlisting}
\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
a + b = 15
d - c = 5
a * b = 50
d / a = 4
\end{lstlisting}
\end{lstlisting}\end{minipage}


\LevelD{Review Questions}
\begin{enumerate}
\item Write a statement declaring two integer variables \Code{a} and \Code{b} and initialize them to 6 and 3, respectively.
\item Without changing the last line, fix the following code so there will be an output of 12.
\begin{lstlisting}
int a = 4, b= 2;
a = a + 2 * b;
cout << a;
\end{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int a = 4, b= 2;
a = a + 2 * b;
cout << a;
\end{lstlisting}\end{minipage}

\item What is the output of the following code?
\begin{lstlisting}
int a=2, b=5, c=6;
a++;
b = b*a;
c = (c-a) + 3;
cout << a << endl;
cout << b << endl;
cout << c << endl;
\end{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int a=2, b=5, c=6;
a++;
b = b*a;
c = (c-a) + 3;
cout << a << endl;
cout << b << endl;
cout << c << endl;
\end{lstlisting}\end{minipage}

\item What is the output of the following code?
\begin{lstlisting}
int a, b, c;
a = 2;
b = 8;
c = 1;
c = b - b;
c = a + a;
c = b * 8;
b = b + b;
c = a + c;
b = a + b;
a = a * c;
b = a - c;
c = b + a;
cout << a << endl;
cout << b << endl;
cout << c << endl;
\end{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int a, b, c;
a = 2;
b = 8;
c = 1;
c = b - b;
c = a + a;
c = b * 8;
b = b + b;
c = a + c;
b = a + b;
a = a * c;
b = a - c;
c = b + a;
cout << a << endl;
cout << b << endl;
cout << c << endl;
\end{lstlisting}\end{minipage}
\end{enumerate}

\LevelD{Homework Exercises}
\begin{enumerate}
\item What is the output of the following code?
\begin{lstlisting}
int a=4, b=2, c, d;
a = b + 3;
b++;
c = (b + 4) * 2;
c = c + 2;
d = a + b - 3;
a++;
a = a + 2 - b;
b = b * 2;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
cout << "d=" << d << endl;
\end{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int a = 4, b = 2, c, d;
a = b + 3;
b++;
c = (b + 4) * 2;
c = c + 2;
d = a + b - 3;
a++;
a = a + 2 - b;
b = b * 2;
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
cout << "d=" << d << endl;
\end{lstlisting}\end{minipage}

\item What is the output of the following code?
\begin{lstlisting}
int m=3, n=2, x, y;
x = m + 5;
m--;
y = (m+4) / 3;
n = n + 2;
m = m + n / 2;
m++;
x = x * 2 - 3;
y = y * 2;
n = n + y * 3
cout << "m=" << m << endl;
cout << "n=" << n << endl;
cout << "x=" << x << endl;
cout << "y=" << y << endl;
\end{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int m=3, n=2, x, y;
x = m + 5;
m--;
y = (m+4) / 3;
n = n + 2;
m = m + n / 2;
m++;
x = x * 2 - 3;
y = y * 2;
n = n + y * 3
cout << "m=" << m << endl;
cout << "n=" << n << endl;
cout << "x=" << x << endl;
cout << "y=" << y << endl;
\end{lstlisting}\end{minipage}
\end{enumerate}

\LevelD{Review Answers}
\begin{enumerate}
\item \Code{int a = 6, b = 3;}

\item
\begin{lstlisting}
int a = 4, b = 2;
a = (a + 2) * b;
cout << a;
\end{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
int a = 4, b = 2;
a = (a + 2) * b;
cout << a;
\end{lstlisting}\end{minipage}

\item
\begin{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
3
15
5
\end{lstlisting}
\end{lstlisting}\end{minipage}

\item
\begin{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
132
66
198
\end{lstlisting}
\end{lstlisting}\end{minipage}

\end{enumerate}

\LevelD{Homework Answers}
\begin{enumerate}
\item
\begin{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
a=5
b=6
c=16
d=5
\end{lstlisting}
\end{lstlisting}\end{minipage}

\item
\begin{lstlisting}

\noindent\begin{minipage}{\linewidth}\begin{lstlisting}
m=5
n=16
x=13
y=4
\end{lstlisting}
\end{lstlisting}\end{minipage}
\end{enumerate}

\LevelD{Further Reading}
Expand Down
34 changes: 34 additions & 0 deletions chap_constants.tex
@@ -0,0 +1,34 @@
We call a variable whose value we cannot change a constant.
After you declare a constant, you are unable to change it, no matter what.
There are two types of constants: literal and declared constants (\Code{const}).

\LevelD{Literals}

A literal is a value outide of a variable such as $5$, $9$, $103$, and $-21$. Each of those is an \Code{int}, but a literal constant can be of any data type. The point is, these are values that the C++ compiler already recognizes, and can’t be changed. In other words, you can’t convince the compiler to give the literal 3 the value of 4, because 3 is constant. The table below contains a few examples.

13.8903
94.2321
-389283220.342423
float
‘x’
‘R’
%
char
Be aware, that C++ interprets the difference between a char and a single-character variable name by the enclosure of (‘) single quotation marks.
true
false
bool
Notice that a bool only has two literal values, true or false.


Declared Constant

So what’s the difference between declaring a normal variable and a constant? When we declare a constant, we simply place the keyword const before the data type in the declaration. This indicates that whatever declaration follows the const will be a constant and cannot be changed. Since it’s a constant, you will also need to initialize the value at the same time you declare the variable. Here is an example:

const float pi = 3.14;
float radius = 5, area;

area = radius * radius * pi;
cout << area;

//program outputs 78.5 to the screen
2 changes: 1 addition & 1 deletion chap_preproc.tex
Expand Up @@ -30,7 +30,7 @@
\Code{<math>} & Mathematical functions & \Code{pow()}, \Code{sqrt()}, \Code{cos()}, \Code{tan()}, \Code{sin()}: see Chapter~\ref{chap_advancedarith} \\ \hline
\Code{<iomanip>} & Input/output manipulation & \Code{get\_money()}, \Code{get\_time()}, \Code{put\_time()} \\ \hline
\Code{<ctime>} & Time-related functions & \Code{clock()}, \Code{time()}, \Code{ctime()} \\ \hline
\Code{<string>} & The \Code{string} class & See Chapter~\ref{chap_string} \\ \hline
\Code{<string>} & The \Code{string} class & See Chapter~\ref{chap_strings} \\ \hline
\Code{<fstream>} & File input and output streams & See Chapter~\ref{chap_file_io} \\ \hline

\end{tabular}
Expand Down

0 comments on commit 2cae4b0

Please sign in to comment.