nottoprogram.C
:
how not to program.
while.C
:
while
loop has same
(
parentheses)
and
{
curly
braces}
braces as the
if
statement.
Prefix increment operator
++
.
<
operator1 John 2 Joe 4 Joey 8 Jacob 16 Jake 32 Johann 64 Jimmy 128 James 256 Judd 512 Jedediah 1024 Johnny
#include <iomanip> //for setw (means "set width") cout << setw(4) << "\n"; //right justify
--
(two minus signs, no space between them)
jsmith@storm:~$ time ./a.out
for
loops:
for1.C
.
Put all three vital statistics on the same line:
start, end, stride.
for2.C
:
make the induction variable
i
local to the
for
loop.
hide.C
whose output is
this.
Use the assigment operator
+=
which we saw in
assignment.C
to add 5 to the induction variable.
beer.C
:
A Hundred Bottles of Beer
on the Wall.
control-c
.
stylesheet1.C
and
stylesheet1.html
.
Direct the output into an output file named
stylesheet1.html
jsmith@storm:~$ cd jsmith@storm:~$ pwd jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/loop/stylesheet1.C jsmith@storm:~$ ls -l (There should be a new file named stylesheet1.C) jsmith@storm:~$ c++ stylesheet1.C jsmith@storm:~$ ./a.out > stylesheet1.html jsmith@storm:~$ ls -l (There should be a new file named stylesheet1.html) jsmith@storm:~$ cp stylesheet1.html public_htmlThen point your web browser at
https://storm.cis.fordham.edu/~jsmith/stylesheet1.html
jsmith
is your Fordham login name.
chinese.C
and
chinese.html
:
loop through a range of 16 consecutive Chinese characters,
using
Unicode
character code numbers.
infinite.C
:
an infinite loop.
control-c
.
pierogies.C
:
needs only one variable,
h
.
setw(2)
to make the columns line up neatly.
You will have to include the header file
<iomanip>
in order to use
setw
.
thruway.C
:
needs only one variable,
a
.
Typical
sign.
sum1.C
:
do it all with one big expression.
sum2.C
:
do it with a series of small expressions
sum3.C
:
write the small expression only once, inside a
for
loop.
sum3.C
to add up all the integers from 1 to 1,000,000 inclusive.
The sum should be
500,000,500,000—about five hundred billion—well
beyond the maximum capacity of a variable of data type
int
.
You will therefore have to change the data type of the variable
sum
from plain old
int
to the
long int
that we saw
here.
(For the expression
(n + 1) * n / 2
you will also have to change the data type of the variable
n
from
int
to
long int
.)
What will happen if you don’t?
(Note: do not change
int main
to
long int main
.)
product1.C
:
do it all with one big expression.
pow
product2.C
:
do it with a series of small expressions.
product3.C
:
write the small expression only once,
inside a
for
loop.
product3.C
ask the user three questions.
In response to these questions,
let him or her input
double
variable named
principal
int
variable named
nyears
double
variable named
rate
,
as shown below
double rate {0.0}; cout << "What is the annual interest rate in percent (e.g., 6)? "; cin >> rate; if (!cin) { //Ouput an error message to cerr and return EXIT_FAILURE } //Also make sure the rate is a reasonable (e.g., positive) number. //Each year, multiply the principal by the following factor. //For example, if rate is 6, then factor will be 1.06 double factor {1.00 + .01 * rate};
organ.C
:
Make a one-octave set of organ pipes.
converge.C
:
converge on the value of
dowhile.C
:
do-while