The most common use of a
for
loop in C++ is to loop through the subscripts of the elements of an array.
See
output3.C
and
sum3.C
.
output1.C
:
store the values in 12 separate int
variables,
and output the values with 12 cout
statements.output2.C
:
store the values in an array of 12 int
s,
and output the values with 12 cout
statements.output3.C
:
store the values in an array of 12 int
s,
and output the values with one cout
statement
in a for
loop.
sizeof
operator in
integer.C
.output4.C
:
store the values in an array of 12 int
s,
and output the values and their subscripts with one cout
statement
in a for
loop.
output4.C
,
number the elements from 1 to 12 instead of from 0 to 11,
because human beings like to start counting at 1, not at 0.
i+1
in place of i
.
names.C
:
loop through
an array of 12 string
s instead of an
an array of 12 int
s.
number1.C
:
loop through an array of
string
s,
in either direction.
if/else if/else
statement in
number1.C
to a
switch/case/default
statement.
See
switch1.C
and
switch2.C
.
switch4.C
:
pick a string
out of an array of string
s
instead of looping through the array.
switch4.C
holds all its data in a “data structure” (an array),
and is therefore simpler than
switch1.C
and
switch2.C
.
month.C
output the name of the current month.
Imitate
switch4.C
.
string
out of an array of
string
s
monkey1.C
.
Compute the desired array subscript with the
%
(remainder) operator.
monkey2.C
.
The same program, but with a long chain of
else/if
statements instead of an array.
%=
operator knocks the
year
down to a much smaller number,
in the range 0 to 11 inclusive.
monkey3.C
.
The same program, but with a
switch
statement instead of an array.
monkey1.C
holds all its data up in a “data structure” (an array).
monkey2.C
and
monkey3.C
,
which kept all their data down in the
“code”
(the
if
and
switch
statements).
monkey1.C
,
output “was/is/will be”.
Use a three-way
if
.
time_t t {time(nullptr)}; //remember to #include <ctime> tm *p {localtime(&t)}; int currentYear {p->tm_year + 1900}; //the current year
plot1.C
:
pick a string
out of an array of string
s.
initialize.C
:
three ways to initialize an array
(or else leave it full of garbage).
cin
.
random.C
:
change the values stored in an array.
stack.C
:
LIFO: last hired, first fired.
queue.C
:
FIFO: first in, first out.
People are waiting on line to be served.
range.C
:
loop through the values in an array using a
range for loop.
colbert.C
:
loop through the
char
s
in a
string
of
char
s
using a range for
loop,
just like the one in
range.C
.
carmodel1.C
holds all of its data in if
statements and cout
statements.carmodel2.C
holds all of its data in a data structure.carmodel2.C
,
change the models and colors
(adding extra ones, rearranging their order),
without changing any part of the program except for the data structure.
dependents1.C
holds all of its data in if
statements and cout
statements.dependents2.C
holds all of its data in a data structure (ann array).date.C
:
pretend there is no such thing as a leap year.
/
and
%
to break the
distance
down into a number of
years
and a number of remaining
days
,
where
days
is in the range 0 to 364 inclusive.
You can then add
years
to
year
,
leaping in a single bound to within one year of the destination date.
Then walk the remaining
days
.
for
loop stride the rest of the way to the destination one
month
at a time, not one day at a time.
triangle1.C
and
triangle2.C
got longer and longer.)
christmashint.C
.
string
s)
for a given value.
find1.C
with a range
for
loop.
string
variables differ from
int
variables in three ways:
#include <string>
""
by default.
string name {""}
;cin >> name;
would input only one word.
getline
function (with parentheses) to input an entire line up to the terminating
newline character.
find2.C
with a plain old
for
loop, to find the index of the value in the array
find3.C
with the C++ standard library
find
algorithm instead of a
for
loop
income.C
:
an array of 50 string
s,
giving the names of the
U.S. States in order of decreasing 2021 median household income
(from the
Wikipedia article).
find2.C
.
Please type the name of a state: New York New York is number 14 of 50 in terms of median household income.
minimumint.C
:
an array of int
s.minimumstring.C
:
an array of string
s.sortint.C
:
an array of int
s.
numeric_limits<int>::max()
in
integer.C
.
sortstring.C
:
an array of string
s.sortint.C
or
sortstring.C
iterates,
by counting how many times the program
executes the comparison
if (a[j] < a[smallest]) {Do this by creating a new variable
int count {0}; //how many times we compare pairs of elementsimmediately before the “
for (int i
”
loop, and say
++count;immediately before the
if
statement, and say
cout << "We performed " << count << " comparisons.\n";immediately before the
return EXIT_SUCCESS;
.
You should discover that the program compared
49 × 50 = 2450 pairs of elements
while sorting the array of 50 elements.
That’s (n − 1)n pairs,
which we will round to n2 pairs.
sortint.C
or
sortstring.C
sort the values into
decreasing
order.
Simply reverse the comparison operator to
if (a[j] > a[smallest]) {and (for your own personal sanity) rename the varariable
smallest
to
largest
.
In
sortint.C
change the biggest possible number
numeric_limits<int>::max()
to the smallest possible number
numeric_limits<int>::min()
.
In
sortstring.C
,
change the “biggest” string
"zzzzzzzzzzzzzzzz"
to the “smallest” string
"AAAAAAAAAAAAAAAA"
.
Then run the program again.
swap.C
:
swap the values of two variables.
temp
in
swap.C
?
Why can’t we swap the values of
x
and
y
like this?
//Swap the values of x and y. x = y; y = x;
bubblesortint.C
:
an array of int
s.bubblesortstring.C
:
an array of string
s.bubblesortint.C
or
bubblesortstring.C
execute the comparison
if (a[j] > a[j + 1]) {You should discover that the program compared only (49 × 50)/2 = 1225 pairs of elements while sorting an array of 50 elements. That’s (n − 1)n/2 pairs, which we will round to n2/2 pairs.
bubblesortint.C
or
bubblesortstring.C
sort the values into decreasing order.
string
s:
language1.C
with a two-dimensional array.
switch4.C
.
language2.C
with nested if
statements
string
s to
language1.C
.
string langname[] { "English", "Spanish", "French", "German" };Output the user instructions by looping through this new array.
language4.C
for the correct way to program this problem in C++.
roman.C
converts an integer to a Roman numeral.
int
s.
france.C
(france.png
)
had three vertical stripes.
flaghw2.C
(flaghw2.png
)
has four vertical stripes.
verticalstripes.C
.
verticalstripes.C
:
france.txt: three stripes
papaya.txt: four stripes
spectrum.txt: seven stripes
(spectrum.png
)date4.C
:
a two-dimensional array of
int
s
to handle leap vs. non-leap years.
parallel1.C
.
Parallel arrays:
a column of
int
s
and a column of
string
s.
structure2.C
.
structure1.C
:
create 12 structures.structure2.C
:
create an array of structures,
and loop through it with a plain old for
loop.
structure2.C
,
observer how the column of names is output left-justified,
and the column of numbers is output right-justified.
structure3.C
:
create an array of structures,
and loop through it with a range for
loop.structure2.C
has two columns.
Add a third column, of type
double
, named
temperature
,
giving the
average temperature
in Fahrenheit,
in New York City, for each month in 2023.
You will have to do three things:
double temperature; //2023 average, in fahrenheitto the blueprint for the data type
month
,
after the two existing fields.
{"January", 31, 43.5}, {"February", 28, 41.1}, //etc.
parallel3.C
:
let the user type the name of a state,
then output the 2021 median income for that state.
parallel4.C
:
an array of 50
struct
ures.
plot2.C
:
how it all fits together.
A one-dimensional array of structures,
and a two-dimensional array of
string
s.
plot1.C
.
ants.C
:
two columns of string
s.
fly.C
:
two columns of string
s.
greengrass.C
:
two columns of string
s.
avenue.C
:
at what cross street is a given address on a Manhattan Avenue?
Uses data from
Wikipedia.
wav.C
and
sine.wav
:
create an audio file.
jsmith@storm:~$ cd jsmith@storm:~$ c++ wav.C jsmith@storm:~$ ./a.out jsmith@storm:~$ mv sine.wav public_htmlMove the file to your
public_html
directory on
storm.cis.fordham.edu
and then point your browser at
http://storm.cis.fordham.edu/~jsmith/sine.wav
.
ants.C