https://markmeretzky.com/fordham/1600/
,
on your computer.
Read the course
syllabus,
including the links about attendance and plagiarism.
Admire
the
public_html
directory of each student.
Write down your login name and secret password
for our Fedora Linux server
storm.cis.fordham.edu
in a safe place.
If you have already forgotten or lost your password,
you can request a new password with
this form.
Log into
storm
during the week.
ssh jsmith@storm.cis.fordham.edu
jsmith
stands for your Fordham ID.
Terminal
application
and type the following “secure shell” command:
ssh jsmith@storm.cis.fordham.edu
jsmith
stands for your Fordham ID.
Practice downloading files from the web into your home directory. For example,
jsmith@storm:~$ date (Make sure that storm.cis.fordham.edu can hear you and is responding.) jsmith@storm:~$ cal jsmith@storm:~$ cd (Go to your own home directory.) jsmith@storm:~$ pwd (Make sure you arrived there.) jsmith@storm:~$ ls -l (See what's in your own home directory.) jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/dime/dime1.C jsmith@storm:~$ ls -l (Did the new file actually arrive?)
Practice
using
vi
(the “visual editor”)
to modify a file.
For example,
jsmith@storm:~$ cd jsmith@storm:~$ pwd jsmith@storm:~$ ls -l jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/xanadu.txt jsmith@storm:~$ ls -l jsmith@storm:~$ vi xanadu.txt
Practice
using the “compiler” (i.e., translator)
c++
to “compile” (i.e., translate) a C++ program into terms that
storm.cis.fordham.edu
can understand and execute.
(The full name of the compiler is
/usr/bin/c++
.)
jsmith@storm:~$ cd jsmith@storm:~$ pwd jsmith@storm:~$ ls -l jsmith@storm:~$ c++ dime1.C jsmith@storm:~$ ls -l (See if c++ created a new file named "a.out".) jsmith@storm:~$ ./a.out (Execute the program and send its output to the screen.) jsmith@storm:~$ echo $? (If you're interested, see the invisible "exit status" number produced by the program.) jsmith@storm:~$ ./a.out > dime1.txt (Execute the program and deposit its output into a new file "dime1.txt".) jsmith@storm:~$ ls -l (See if you created the file dime1.txt.)
Practice
copying files into your public_html
directory
so you can see them with a web browser.
jsmith@storm:~$ cd jsmith@storm:~$ pwd jsmith@storm:~$ ls -l jsmith@storm:~$ cp dime1.C public_html jsmith@storm:~$ cp dime1.txt public_html jsmith@storm:~$ cd public_html jsmith@storm:~$ pwd jsmith@storm:~$ ls -l (See if the copies are actually there in your public_html directory.)
Then see the files in your
public_html
directory by pointing your web browser at
https://storm.cis.fordham.edu/~jsmith/dime1.C
https://storm.cis.fordham.edu/~jsmith/dime1.txt
Study the C++ programs
dime1.C
through
dime6.C
.
Now make two changes to the C++ program
flag.C
.
using
statement so you don’t have to say std::
10 times.
dime6.C
produced all three of its lines of output with a single statement.
Since there is only a single statement,
there will now be only a single semicolon (;
).
(Of course, the
return
statement will still have its own semicolon.)
Execute your changed
flag.C
and deposit its output into a new file named
flag.txt
.
Copy your changed
flag.C
and its file of output
flag.txt
into your
public_html
directory.
Next week, our web server should be configured so that
you can see these two files by pointing your web browser at
https://storm.cis.fordham.edu/~jsmith/flag.C
https://storm.cis.fordham.edu/~jsmith/flag.txt
Until then, you can see your files
here.
If you have time: I heard three beeps when I executed a C++ program
including the following statement while connected to
storm.cis.fordham.edu
from my Macintosh.
Can you hear the beeps?
Turn up the volume.
std::cout << "\a\a\a\n"; //the ASCII "alarm" character
For the two students who could not log into
storm.cis.fordham.edu
:
storm.cis.fordham.edu
account for
dg51@fordham.edu
,
and emailed him his insane temporary four-word password.
ac239@fordham.edu
.
storm.cis.fordham.edu
is rejecting your password,
you can request another password by filling out
this form.
Study the programs we looked at in class today,
and download and execute some of them.
Try new combinations of data types.
For example, could you store input from
cin
into a
double
variable the way we stored input into an
int
variable
here?
Could you find the maximum (and minimum) values we could store into a
double
variable the way we found the maximum (and minimum) values we could store
into an
int
variable
here?
(Expect the maximum
double
value to be a humongous number around
1.8 × 10308.)
Why do the possible values for an
int
go from -2,147,483,648 to +2,147,483,647?
I tried to write out the answer
here
more neatly than I did on the whiteboard in class on September 5.
Also look at the table of operator precedence and associativity, starting with lines 5 and 6. On September 12, we’ll also look at lines 2, 7, 16. Does C++ have a dismaying number of operators?
Do you get the same unpredictable garbage each time you execute the following code? Run it three or four times. (“nan” means “not a number”; “inf” means “infinity”.)
//Remember to compile with c++ -std=c++20 programname.C #include <format> //for format. Remember the other #include's using namespace std; int i; //unpredictable garbage double d; //unpredictable garbage cout << i << "\n"; cout << d << "\n"; cout << format("{:15}", d) << "\n";
Write a little C++ program that inputs (using
cin
)
one or more values into one or more
int
or
double
variables,
does some arithmetic with these variables in an expression,
and outputs (using
cout
)
the result.
Invent something interesting.
Some suggestions:
lobster.C
Copy your C++ program into your
public_html
directory.
For inspiration, see what the other students have done
here.
cin
and stores the input into a variable,
checks to make sure that the input was successful,
computes a value (the final answer) by means of an expression,
stores that value into another variable,
and outputs the value of that other variable.
Write a program that does
int
arithmetic with
/
%
to compute the quotient and remainder resulting from an
int
division.
This quotient and remainder could be
dollars and cents, hours and minutes, gallons and quarts, etc.)
Also play with
while
and
for
loops.
(I updated the embarrasing, obsolete initialization of the variable in
for2.C
and
beer.C
.)
Can you store the output of
stylesheet1.C
into a file (named
stylesheet1.html
)
and see it in your web browser?
Could you write a
for
loop that computes the sum of all the integers from 1 to 100 inclusive?
(The sum is 5,050.)
product3.C
has three numbers already built into it:
the initial investment ($1,000.00), the number of years (10),
and the annual rate of interest (6 percent).
Make a more flexible version of the program that asks the user three questions,
so that her or she can input their own choice for these three numbers.
Put the resulting program into your
public_html
directory by Sunday night, September 22, 2024.
If you have no idea how to do this,
take a peek at
product4.C
.
Our first example of how to do input was
variable5.C
.
The C++ program
graph3.C
always makes little boxes that are the same size and shape.
That’s because it has two numbers already built into it:
the number of rows of blanks in each box (1),
and the number of columns of blanks in each box (4).
Make a more flexible version of the program that asks the user two
additional questions,
so that her or she can input their own choice for these two numbers.
See the two pictures here.
[Hint: we used a
for
loop in
line.C
to output 36 X
’s in a row.
Now instead of the
cout << "+----";that we currently have in
graph3.C
,
could you output a plus sign, and then write a for
loop to output 4 dashes in a row?
And instead of looping 4 times,
could you loop the number of times that the user asked for?]
Put the resulting program into your
public_html
directory by Thursday afternoon, September 26, 2024.
Also pick a color, any color. The recipe for a color is three numbers, telling how much red, green, and blue light to mix in. The minimum amount for each ingredient is 0, the maximum amount is 255. For example,
255 0 0
red0 255 0
green0 0 255
blue255 215 0
gold192 192 192
silver184 115 51
copper128 128 0
olive0 255 255
aqua
Put the three numbers of your color into
solid.C
,
replacing my blue color 0 0 255
.
Run the program, together with the other program
/usr/bin/pnmtopng
, following the instructions
here.
Then can you see a little 20 × 30 image file of your chosen color
in your web browser?
The eaiest way to begin to think about nested loops
is to think about traveling through a two-dimensional space,
e.g., the rows and columns of pixels that make up a rectangular image.
For a non-spacial example of nested loops, see if you can figure out
how
factor.C
works.
To give you a head start on any C++ program that you write from scratch,
please help yourself to a copy of this little file,
boilerplate.C
.
It will save you some typing.
jsmith@storm:~$ cd jsmith@storm:~$ pwd jsmith@storm:~$ wget https://markmeretzky.com/fordham/1600/src/dime/boilerplate.C jsmith@storm:~$ ls -l (There should be a new file named boilerplate.C) jsmith@storm:~$ mv boilerplate.C bettername.C (Rename the file.) jsmith@storm:~$ ls -l (Did the file get renamed?) jsmith@storm:~$ vi bettername.C (Now write the C++ program.)
morning.C
when I woke up on Friday morning, September 27.
(It turns out that the 12-hour clock needed a three-way
if
instead of a remainder;
sorry about that.)
You’ll have to download a fresh
morning.C
with another
wget
,
and then insert the “Good morning/afternoon/evening”
code again.
Then run the program
at various hours of the day and see what it outputs.
You can also run
and2.C
,
but it will remain silent until next Thursday evening.
Study two examples of nested loops:
the small but subtile example in
factor.C
and the triply nested loops in
graph4.C
.
Review the steps by which I changed
graph3.C
into
graph4.C
in class.
For example,
code fragment (a) is hardwired to always output exactly 4 dashes.
It evolves into
code fragment (d),
which is flexible enough to output the number of dashes that
the user requested.
cout << "+----";
cout << "+"; cout << "----";
cout << "+"; for (int cb {0}; cb < 4; ++cb) { cout << "-"; }
cout << "+"; for (int cb {0}; cb < ncolsb; ++cb) { cout << "-"; }
Could you figure out how to close the right and bottom edges of the graph paper instead of leaving it ragged? For example,
How many rows of boxes (e.g., 10)? 2 How many columns of boxes (e.g., 10)? 4 How many rows of blanks in each box (e.g., 1)? 1 How many columns of blanks in each box (e.g., 3)? 4 +----+----+----+---- (ragged) | | | | +----+----+----+---- | | | |
How many rows of boxes (e.g., 10)? 2 How many columns of boxes (e.g., 10)? 4 How many rows of blanks in each box (e.g., 1)? 1 How many columns of blanks in each box (e.g., 3)? 4 +----+----+----+----+ (closed) | | | | | +----+----+----+----+ | | | | | +----+----+----+----+
You didn’t seem very happy with the four-way
if
in
leap.C
.
So here’s the
Wikipedia article
about our “Gregorian” calendar.
(That’s the calendar we use every day.)
Look at the quote from the United States Naval Observatory
that begins
“Every year that is exactly divisible by four is a leap year,
except for years that are…”.
Please satisfy yourselves that my
if
statements do exactly what this quote says.
Also study the examples of
if
statements inside of
for
loops, starting with
product4.C
(which is merely
product3.C
with an added
if
statment).
By now, you should understand how the
if/else
in
ukraine.C
works (see
ukraine.png
).
Can you make the Ukrainian flag bigger?
Can you figure out how the
if/else if/else
in
france.C
works
(see
france.png
)?
graph5.C
is just like
graph4.C
,
except it closes up the ragged right and bottom edges of the graph paper.
You can run
toolow.C
again:
it now picks a random number in the range 1 to 100 each time you run it.
Observe that if you follow the best strategy
(divide and conquer:
your first guess should be 50, your second should be 25 or 75)
it will never take more than 7 guesses.
That’s because log2 100 is approximately 7.
toolow2.C
is the same program,
but it uses a new variable named
tries
to count how many guesses it took.
In class on October 3, we unsnarled
morning1.C
into
morning3.C
,
and I was pleasantly surprised to see so much interest in this issue.
In the same way, unsnarl
morning2.C
and put the resulting program into your
public_html
directory by Sunday night, October 6, 2024.
Write a C++ to output a flag like the examples we saw
here
(ukraine.png
,
france.png
,
japan.png
,
usa.png
,
etc.)
Use the same nested
for
loops that we saw in
ukraine.C
:
the outer loop counts through the rows,
the inner loop counts through the columns.
It would be really convenient if I could resize your flag just by changing the
values of the two variables
nrows
and
ncols
.
(That’s what we did in class on October 3 to
ukraine.C
and
france.C
.)
To have more than one color,
you will need an
if
statement inside of the nested
for
loops, as in
ukraine.C
and
france.C
.
That’s the whole point of this assignment:
to provide yet another example of writing an
if
statement, or an
if/else
statement
(or even a chain of
else/if
s)
inside of a loop (in this case,
inside of nested loops).
Put your C++ program and the resulting
.png
image file into your
public_html
directory by Thursday afternoon, October 10, 2024.
For example, check out the lovely essay in papayawhip,
peachpuff, pink, and lightcoral at
flaghw2.C
and
flaghw2.png
.
Or the
flag of Ireland
at
ireland.C
and
ireland.png
.
The midterm exam will be on Thursday, October 10, 2024 at 6:00 p.m. Arrays will not be on the midterm, since we haven’t covered them yet.
https://print.fordham.edu/user
says “This site can’t be reached”.
for2.C
,
so make sure you understand how this program works.
for2.C
loop?
Improve
monkey1.C
so that it outputs the correct tense of the verb “to be”:
switch4.C
and the current hour in
and2.C
.)
time_t t {time(nullptr)}; //remember to #include <ctime> tm *p {localtime(&t)}; int currentYear {p->tm_year + 1900}; //the current year
Speaking of three-way
if
statements,
check out
Italy.png
mexico.png
mexico.png
ireland.C
and
ireland.png
.
Too bad he or she said
int row = 0;
instead of
int row {0};
,
and
int col = 0;
instead of
int col {0};
.
Let’s party like it’s 2011!
colombia.png
,
and compare them with the real
flag of Colombia.
I wish we could see the C++ program that created this flag,
but the
author
did not copy the C++ program into his or her
public_html
directory.
Is there an easy way to make the top stripe wider?
Please do the
remedial exercises.
You can hand in the answers on paper,
or put them in one big file (call it remedial.txt
)
in your public_html
directory on
storm.cis.fordham.edu
.
I added two more
nested loop
examples,
triangle.C
and
parallelogram.C
,
to
line.C
and
rectangle.C
.
In
triangle.C
,
why do the lines become longer and longer?
In
parallelogram.C
,
why are there more and more spaces in front of each line?
On October 24, we will decide if we want to have class on October 31 (Hallowe’en).
I repeat the main idea of the October 24th class:
the data in a C++ program (i.e., the lists of numbers and strings)
belong up in a data structure (such as an array or a “map”)
at the top of the program,
not down in the statements (such as the if
statements
or the cout
statements)
at the bottom of the program.
For example, compare
Good | Bad |
---|---|
switch4.C |
switch1.C |
carmodel2.C
| carmodel1.C |
verticalstripes.C |
flaghw2.C |
Use nested loops to output the lyrics to a song
whose verses get longer and longer.
First see how the nested loops in
triangle.C
produce lines of output that get longer and longer.
Then contemplate the
lyrics
to the song
The Twelve Days of Christmas,
and see how the nested loops in
christmashint.C
produce paragraphs of output that get longer and longer.
You can pick a different song
(such as Green Grow the Rushes, O,
There’s a Hole in the Bottom of the Sea,
or
There Was an Old Lady who Swallowed a Fly),
but I would be most interested in seeing a song in a foreign language.
Make sure you understand the
day
/month
/year
logic in the body of the
for
loop in
date.C
.
(You can assume that there are no leap years.)
If the user wanted to go a distance of 1000 days into the future,
the loop would iterate 1000 times.
Let’s take this number 1000 as an example.
Is there a way we could get to our destination with fewer than 1000
iterations?
Do you already know of an easy way to break 1000 days into two smaller numbers,
namely 2 years and 270 additional days?
Then is there an easy way to leap 2 years into the future
(say, from 24 Oct 2024 directly to 24 Oct 2026) in a single bound?
(Hint: something like year += 2;
)
If that were the case, then you would only need to iterate 270 times
to get to the final destination.
So change
date.C
so that no matter what distance the user requests,
even if it’s up in the thousands,
you can get there with at most 364 iterations of the
for
loop.
Extra credit:
can you change the loop so that it advances one month into the future,
instead of just one day into the future,
with each iteration?
christmas.C
.
You should no longer get an error message from the
c++
command.
Sorry.
My attempt to output three Chinese characters that you can see in your
web browser (上下川):
chinese.C
.
If that works, you can try the Chinese
for
loop
(上
下
丌
不
与
丏
丐
丑
丒
专
且
丕
世
丗
丘
丙)
in this
chinese.C
.
I repeat some of the main ideas of the November 7th class.
In
date2.C
,
think of the three variables
year
,
month
,
day
as a team that collectively represent a date.
On November 14th, we will learn to actually package these three variables
into one big variable called a “structure”,
and in the Spring 2025 CISC-2000-E01, we will learn to package
these three variables into one big variable called an “object”
(as in “object oriented programing”).
In two programs,
we used the operators
/
and
%
to get a quotient,
and a remainder of a limited size.
date2.C
,
we divided
distance
by 365 and got a remainder limited to the range 0 to 364 inclusive.
We then used this remainder to limit the
for
loop to at most 364 iterations.
queue.C
(which we did not attempt to read in class),
we had an array of
n
elements,
whose subscripts therefore had to be limited to the range 0 to
n-1
inclusive.
When we had a subscript that was outside of this legal range,
we divided the subscript by
n
and used the remainder instead.
For example,
when we had a subscript of n+1
,
which was a little bit beyond the end of the array,
we divided the subscript by n
and got a remainder of 1,
which was safely within the array.
Study the logic of two programs:
date3.C
wants to go 1000 days into the future from November 7, 2024.
1000 days is 2 years and 270 days.
(That’s what the
/
and
%
tell you.)
If we add 2 years and 270 days directly to November 7, 2024,
we get November 277, 2026.
In some sense this surprising answer is correct,
but it needs a series of minor adjustments.
Here are other ways of writing the same date,
each one slightly more reasonable than the previous one:
for
loop in
date3.C
performs the above 9 adjustments with only 9 iterations,
instead of the 1000 iterations that
date.C
would have taken,
or the 270 iterations that
date2.C
would have taken.
This is called
programming.
Knowing how to program will get you big bucks.
stack.C
,
as an example of a program that changes the values in the array as it runs.
On November 14,
we will divide the
main
function of this program into several separate functions.
If you haven’t yet done the homework where you write a C++ program that outputs the lyrics to a song that gets longer and longer, do it.
Write another C++ program that outputs the lyrics to a song
made of verses and a chorus.
The chorus should be output by a function named chorus
.
Imitate
function1.C
and
function2.C
.
Remember to write a function declaration above the main
function as well as the function definition below the main
function.
Study
minimumint.C
.
Then observe that
sortint.C
has the same logic, repeated over and over again.
(Or if you prefer string
s, you can study
minimumstring.C
.
Then observe that
sortstring.C
has the same logic, repeated over and over again.)
I added a separate example,
swap.C
,
to illustrate how to swap the values of two variables.
This logic is employed in
bubblesortint.C
and
bubblesortstring.C
.
structure2.C
has two columns of data.
Give it a third column,
listing the average 2023 temperature in Fahrenheit in New York City
for each month.
Get the temperatures from the last complete row in
this
table.
You will have to make three changes to
structure2.C
:
double temperature; //2023 New York City average, in fahrenheitto the blueprint for the data type
month
,
after the two existing fields
name
and
length
.
{"January", 31, 43.5}, {"February", 28, 41.1}, //etc.
Run the C++ program
wav.C
, creating the output file
sine.wav
.
(Follow the directions in the
array examples.)
Then change the array of structures
to play a different melody.
For example,
note a[] { {E, 0.5}, //Three {D, 0.5}, //blind {C, 1.0}, //mice, {E, 0.5}, //Three {D, 0.5}, //blind {C, 1.0}, //mice, {G, 0.5}, //See {F, 0.25}, //how {F, 0.25}, //they {E, 1.0} //run, {G, 0.5}, //See {F, 0.25}, //how {F, 0.25}, //they {E, 1.0} //run. };
The array of structures already has two
double
columns, named
pitch
and
length
.
Add a third
double
column named volume
,
giving the loudness of each note.
1.0 will be the maximum volume,
0.0 will be the minimum volume (complete silence),
and 0.1 or 0.05 will be a very soft volume.
To make it work, change the value of the variable y
to
double y {a[i].volume * numeric_limits<int16_t>::max() * sin(x)};
Fordham says, “Please remind your students that the Tutor room in Rose Hill (John Mulcahy Hall 310) is now open 10-3:45 M-F. (Exception - Monday opens at 10:30). We now have a PhD student working the 10-11:15 shift T-F. No reservations needed!”
maze.C
(and its output)
to your friends and relatives during Thanksgiving.
(“This is what we’re doing in our C++ class at Fordham!”)
Let them make their own maze
by modifying the rows and columns of characters in the two-dimensional
array a
in
maze.C
.
To make the output more
vivid,
you can put in the color control codes
here.
Here’s how this program relates
to what we did in class on November 21, 2024.
maze.C
is divided into two functions,
the
main
function and the
f
function.
Note that we created the three variables
a
,
nrows
,
and
nrows
up above the
main
function.
This allows us to mention these three variables in both fuctions.
All the other variables were created inside of a function.
Each of these other variables can therefore be mentioned
only within the function
in which they were created.