The textbook for this course is
online.
Part I reviews the language C and covers Chapters 1–2.
Part II covers Chapters 3–6.
Part III covers Chapters 7–10.
/home1/m/mm64/public_html/book/src
.wget
or with the text-only web browser
lynx.
cd to the directory to which you want to download the file pwd lynx -source http://i5.nyu.edu/~mm64/book/src/prog/prog.c > prog.C ls -l prog.C
:g/^[ ]*/s///You should then insert your own white space.
M-x query-replace-regexp <enter>^[ ]*<enter><enter>On a typical PC keyboard, M-x is typed as esc-x. Alternatively, if you are using X11 or xemacs, query-replace-regexp is available from the Search menu with the mouse. At this point, hit <space> or N to confirm or deny each instance, or simply ! to confirm them all at once.
ordinal2.C
in the
in-class examples.
Admire our
class photo;
next week, we’ll make it touch-sensitive.
Read the
syllabus and grading policy.
Look at the
textbook.
See how much homework there was
last semester.
You can use any version of C and C++ on any platform for this course. If you’re a Microsoft Windows person and you don’t already have C and C++ compilers, the easiest way to get started is with Bloodshed.
If you want to use C and C++ with a free Unix account on the Solaris server
i5.nyu.edu
,
get your loginname
here.
Your loginname
is the same as your NYU NetID—two or three lowercase letters
followed by several digits.
Please email me at
mark.meretzky@nyu.edu
if you are in the class but your name is not on the list.
Get your secret password
here.
Your secret password is the same as the secret password for your NYU NetID.
Compile your program with
/bin/g++ myprog.C ls -l a.out -rwx------ 1 abc1234 users 6932 Jun 3 15:23 a.out ./a.out
Brief notes on various platforms:
i5.nyu.edu
,
or on a Macintosh OS X Terminal application.
gcc
and
gcc-c++
if you don’t already have them.
Ubuntu 12.04 desktop Linux:
to get the
term.c
program
(Chapter 1,
pp. 85–89) to compile and link,
you must install these additional packages:
libncurses5-dev (5.9-4)
and
libtinfo-dev (5.9-4)
.
Look at the table of operator precedence and associativity
on p. 6 of
Chapter 1;
multiplication and division are on line 13.
Try all the programs we looked at in class.
Can you find the header file
iostream
on your machine?
What are the values of
INT_MIN
and
INT_MAX
on your machine?
What happens if you add 1 to a variable that already holds
INT_MAX
?
What happens if you subtract 1 from a variable that already holds
INT_MIN
?
What happens if you leave a variable uninitialized or divide by zero?
What are the quotient and remainder when you divide a negative number
by a positive number?
How long does a
for
loop take to loop through all 4,294,967,296 integer values?
(Don’t write the commas: they’re only for human beings.)
On the NYU Solaris machine
i5.nyu.edu
,
it took 44 seconds.
Don’t hand in any of the above—just do them.
Hand in a program that inputs and outputs one or more integers,
and does something interesting in between.
For example, when does the turkey come out of the oven?
What is
Bill Gates’s
net worth today?
Hand in the graph paper homework in the
in-class examples
immediately after
graph3.C
where it says
“Homework”.
Read ahead in the
in-class examples
so you can see what we’re doing next week.
For each program that you hand in, please hand in the C++ program itself and the output. On paper. And I’d be delighted to look at anything you want to show me in C or C++, even stuff that has nothing to do with the course. Surprise me!
rose.C
in the
in-class examples.
Hand in the flag homework, but do a country whose flag is more interesting than France’s. A color printout is not necessary; black and white is fine. Hand in the program and the picture, but don’t hand in the 60,000 lines of numbers.
Also hand in the date homework.
Hand in a C++ program consisting of at least two
.C
or
.cpp
files.
Create variables and functions in one of the files;
use the variables and functions in another of the files.
Also create a
.h
file that declares the names of the variables and functions,
the data types of the variables,
and the data types of the arguments and return values of the functions.
I showed you two ways of writing bubblesort:
with
subscripts,
and rewritten with pointers on pp. 47–48 of
Chapter 1.
Replace the variable i
in this
moving average
program with a pointer named p
.
If you want,
you can also replace j
with another pointer.
Run (but do not hand in) the program in Chapter 1, pp. 85–89, but don’t hand it in. It consists of three files:
term.h
(p. 86; acceptable to C and C++)
and
term.c
(in the language C)main.C
(p. 87; in the language C++)Read ahead about objects in Chapter 2.
version1.C
(pp. 105–108),
version2.C
(pp. 108–111),
and
version3.C
(pp. 111–117),
but don’t hand them in.
Do Homework 2.1.3a (p. 117),
but don’t hand it in.
Hand in Homeworks 2.4a (p. 126) and 2.4b (p. 129).
Read ahead.
term.h
(p. 86 in Chapter 1;
acceptable to C and C++)
and
term.c
(in the language C)terminal.h
and
terminal.C
(in the language C++; pp. 159–161 in
Chapter 2)main.C
(in the language C++; pp. 158–159 in
Chapter 2)Run (but do not hand in) the game in pp. 192–200 consisting of the following nine files.
See how I simplified a flag homework.
scps.nyu.edu/evaluate
.
If you want to learn to program in C++,
go to the second version of the program
here
and change the function
a
into an object.
The static local variable
arr
will be a data member;
the global variables
width
,
height
,
xmax
,
ymax
,
xmin
,
and
ymin
will be static data members.
Think of p. 177 in
Chapter 2:
“a set of variables used by a series of function calls”.
Do Homework 2.16a (p. 263), but don’t hand it in. You can skip paragraphs (1) and (9) of that homework.
Hand in one copy of the rabbit game incorporating Homeworks 2.14.2a (p. 244), 2.16b (p. 264), 2.17a (p. 269), and 2.17b (p. 269). Please hand in only the files you had to modify; circle the code you changed.
Hand in Homework 2.16c (p. 264).
ordinal2.C
in the
in-class examples.
Admire our
class photo;
next week, we’ll make it touch sensitive.
Read the
syllabus and grading policy.
Look at the
textbook.
See how much homework there was
last semester.
You can get the wireless password each week from room 233.
You can use any version of C and C++ on any platform for this course. If you’re a Microsoft Windows person and you don’t already have C and C++ compilers, the easiest way to get started is with Bloodshed.
If you want to use C and C++ with a free Unix account on the Solaris server
i5.nyu.edu
,
get your loginname
here.
Your loginname
is the same as your NYU NetID—two or three lowercase letters
followed by several digits.
Please email me at
mark.meretzky@nyu.edu
if you are in the class but your name is not on the list.
Get your secret password
here.
Your secret password is the same as the secret password for your NYU NetID.
Compile your program with
/bin/g++ myprog.C ls -l a.out -rwx------ 1 abc1234 users 8384 Sep 24 15:22 a.out ./a.out
Brief notes on various platforms:
Look at the table of operator precedence and associativity
on p. 6 of
Chapter 1;
multiplication and division are on line 13.
Try all the programs we looked at in class.
Can you find the header file
iostream
on your machine?
What are the values of
INT_MIN
and
INT_MAX
on your machine?
What happens if you add 1 to a variable that already holds
INT_MAX
?
What happens if you subtract 1 from a variable that already holds
INT_MIN
?
What happens if you leave a variable uninitialized or divide by zero?
What are the quotient and remainder when you divide a negative number
by a positive number?
How long does a
for
loop take to loop through all 4,294,967,296 integer values?
(Don’t write the commas: they’re only for human beings.)
On the NYU Solaris machine
i5.nyu.edu
,
it took 44 seconds.
Don’t hand in any of the above—just do them.
Hand in a program that inputs and outputs one or more integers,
and does something interesting in between.
For example, when does the turkey come out of the oven?
What is
Bill Gates’s
net worth today?
Hand in the graph paper homework in the
in-class examples
immediately after
graph3.C
where it says
“Homework”.
Try to get the
flag
to work, but don’t hand it in.
For each program that you hand in, please hand in the C++ program itself and the output.
automatic.C
in the
in-class examples.
Hand in the
date.C
and
plot.C
homeworks in the
in-class examples.
(The plot.C homework is in the section on
“Structures and Arrays Thereof”.)
Of course, you’ll have to write more than two programs per week
if you want to learn C, let alone C++.
I’d be happy to read anything you want to show me.
I love reading programs.
How big are your
long int
s?
Do you have the data type
long double
?
Can you get
wchar_t
to work on your machine?
Microsoft Windows people:
does
this
work?
Don’t let the wrong words slip
while kissing persuasive lips.
.C
files,
pointers
(Chapter 1,
pp. 44–55),
zero (p. 67),
and
pass-by-value vs. pass-by-reference (p. 69).
here’s one answer to the
graph
homework.
Hand in a C++ program (together with its output)
that is divided into two or more
.C
files.
At least one of the
.C
files should use variables and/or functions that were created in another
.C
file.
These variables and/or functions should be declared in a
.h
file that is included by the
.C
files.
term.h
(p. 86; acceptable to C and C++)
and
term.c
(in the language C)main.C
(p. 87; in the language C++)
Unix people:
if tyhe executable program says
“Sorry, I don”t know anything
about your "xterm-256color" terminal”
put the word
vt100
into your
TERM
environment variable.
echo $TERM export TERM=vt100 echo $TERMWrite an interesting C++ program. Gallia est omnis divisa in partes tres.
Run
version1.C
,
version2.C
,
and
version3.C
,
but don’t hand them in.
Do Homework 2.1.3a (p. 117),
but don’t hand it in.
Hand in a program that creates an interesting class,
creates a few objects of the class,
and calls their member functions.
term.h
(p. 86 in Chapter 1;
acceptable to C and C++)
and
term.c
(in the language C)terminal.h
and
terminal.C
(pp. 159–161 in
Chapter 2)main.C
(pp. 158–159 in
Chapter 2)Hand in Homeworks 2.4a (p. 126), 2.4b (p. 129), and 2.5a.
Here is code we wrote in class on November 5th before 6:00 p.m. It is not a homework.
#include <iostream> #include <cstdlib> #include <string> using namespace std; struct mystruct { string s; double d; }; mystruct a[] = { {"moe", 10}, {"larry", 20}, {"curly", 30} }; const size_t n = sizeof a / sizeof a[0]; int main() { for (const mystruct *p = a; p < a + n; ++p) { cout << p->s << "\n"; } cout << "\nPlease type one of the above names:\n"; string choice; cin >> choice; for (const mystruct *p = a; p < a + n; ++p) { if (p->s == choice) { cout << "found " << p->d << "\n"; return EXIT_SUCCESS; } } cerr << choice << " not found\n"; return EXIT_FAILURE; }
Run (but do not hand in) the game in pp. 192–200 consisting of the following nine files.
term.h
or
term.c
.
You get no credit if you hand in more than one copy of the game.
You fail the course if you hand in code that did not compile and run,
unless you write on it
“didn’t compile”
and/or
“didn’t run.”
factor.C
in the
in-class examples.
Admire our
class photo;
on June 4th, we’ll make it touch-sensitive.
If you want to use C and C++ on a free Unix account on the Solaris server
i5.nyu.edu
,
get your loginname
here.
Your loginname
is the same as your NYU NetID—two or three lowercase letters
followed by several digits.
Get your secret password
here.
Your secret password is the same as the secret password for your NYU NetID.
Compile your program with
/opt/gcc453/bin/g++ myprog.C ls -l a.out ./a.out
Read the syllabus and grading policy. Look at the textbook. See how much homework there was last semester.
You can use any version of C and C++ on any platform for this course. If you’re a Microsoft Windows person and you don’t already have C and C++ compilers, the easiest way to get started is with Bloodshed.
Try all the programs we looked at in class.
Come prepared to answer questions such as the following.
What are the values of
INT_MIN
and
INT_MAX
on your machine?
What happens if you add 1 to a variable that already holds
INT_MAX
?
What happens if you subtract 1 from a variable that already holds
INT_MIN
?
What happens if you leave a variable uninitialized or divide by zero?
What are the quotient and remainder when you divide a negative number
by a positive number?
How long does it take to loop through all four billion integer values?
(On the NYU Solaris machine
i5.nyu.edu
,
it took 44 seconds.)
Look at the table of operator precedence and associativity
on p. 6 of
Chapter 1;
multiplication and division are on line 13.
Hand in a program that inputs and outputs one or more integers.
For example, when does the turkey come out of the oven?
What is
Bill Gates’s
net worth today?
Hand in the graph paper homework immediately after
graph3.C
where it says
“Homework”
in the
in-class examples.
Try to get the
flag
to work, but don’t hand it in.
For each program that you hand in, please hand in the C++ program itself and the output.
No class May 28 (Memorial Day).array2.C
in the
in-class examples.
Click on the tip of each nose in the
class photo.
Homework excerpts.
Make an interesting
flag
with some
if
statements.
Hand in the program and the picture,
but not the 60,000 lines of numeric output.
static
in the
in-class examples.
Hand in the
date.C
homework,
plus one more program that does something interesting with arrays.
Run the programs that are divided into two or more source files,
but don’t hand them in.
The Microsoft Windows input example now works correctly.
Here’s an example of arrays and pointers.
#include <iostream> #include <cstdlib> using namespace std; int main() { //An array containing two pointers. //Each pointer points to the start of an array of chars. const char *a[] = { "hello", "goodbye" }; //Prints &a[0], i.e., the address of the first element of a. //The first element of a is a pointer to char. cout << a << "\n"; //Prints the characters hello, because a[0] is a pointer to char. cout << a[0] << "\n"; //Prints the character h, because a[0][0] is a char. cout << a[0][0] << "\n"; //An array containing two smaller arrays. //Each smaller array contains 8 chars. const char b[][8] = { "hello", "goodbye" }; //Prints &b[0], i.e., the address of the first element of b. //The first element of b is an array of 8 chars. cout << b << "\n"; //Prints the characters hello, because b[0] is the same as &b[0], //which is a pointer to a char. cout << b[0] << "\n"; //Prints the character h, because b[0][0] is a char. cout << b[0][0] << "\n"; return EXIT_SUCCESS; }
0xffbff6a8 hello h 0xffbff698 hello h
term.h
(p. 86; acceptable to C and C++)
and
term.c
(in the language C)main.C
(p. 87; in the language C++)
Study
version1.C
,version2.C
,version3.C
weather_report
class containing
temperature
and
wind speed
data members,
and a
wind_chill
member function that returns
the
wind
chill?
Or something to do with your height and ideal weight?
On Mars or Jupiter?
Or your interest rate?
typedef value_type
on pp. 153–154.
Run (but do not hand in)
the program that tests class
terminal
.
It consists of these five files:
term.h
(p. 86 in Chapter 1;
acceptable to C and C++)
and
term.c
(in the language C)terminal.h
and
terminal.C
(pp. 159–161 in
Chapter 2)main.C
(pp. 158–159 in
Chapter 2)term.h
and
term.c
.
See
Chapter 1,
pp. 85–89.
These two files are in C; the others are in C++.terminal.h
and
terminal.C
.
See
Chapter 2,
pp. 157–163.rabbit.h
and
rabbit.C
.
See
Chapter 2,
pp. 192–200.wolf.h
and
wolf.C
.
Don’t forget to write the constructor for class
wolf
.main.C
term.h
,
term.c
,
terminal.h
,
terminal.C
.
and
main.C
.
Read about friend functions (pp. 200–210). Hand in Homework 2.11b (friends, p. 210).
If you want to use C and C++ on a free Unix account on the Solaris server
i5.nyu.edu
,
get your loginname
here.
[As of
Fri Sep 21 11:17:38 EDT 2012
the i5.nyu.edu accounts have been created.]
(Please email me at
mark.meretzky@nyu.edu
if you are in the class but your name is not on the list.)
Your loginname
is the same as your NYU NetID—two or three lowercase letters
followed by several digits.
Get your secret password
here.
Your secret password is the same as the secret password for your NYU NetID.
Compile your program with
/opt/gcc453/bin/g++ myprog.C ls -l a.out ./a.out
Read the syllabus and grading policy. Look at the textbook. See how much homework there was last semester.
You can use any version of C and C++ on any platform for this course. If you’re a Microsoft Windows person and you don’t already have C and C++ compilers, the easiest way to get started is with Bloodshed.
Make sure you can run the C++ programs we looked at in class, e.g., the program consisiting of the three files
You don’t have to hand this in.Make a class with overloaded operators. Write a program that demonstrates them. Hand in the program and the output on paper on October 3. The following are some sketchy suggestions.
color yellow = red + green; vector northeast = north + east; element helium = hydrogen + 1; //There are 4 stops on the #6 subway //between Union Square and Grand Central Terminal. int n = grandCentral - unionSquare; station bleekerStreet = unionSquare - 1; //one stop below Union Square if (myCurrentStation < bleekerStreet) { cout << "I am way downtown.\n"; }
Run (but do not hand in)
the program in
Chapter 1,
pp. 85–89, but don’t hand it in.
It consists of the following three files.
Email me at
mark.meretzky@nyu.edu
if you get intro trouble;
see the above links to the various platforms.
term.h
(p. 86; acceptable to C and C++)
and
term.c
(in the language C)main.C
(p. 87; in the language C++)No class September 26.
Run (but do not hand in)
the program that tests class
terminal
.
The program consists of these five files.
You have already downloaded the first two.
term.h
(p. 86 in Chapter 1)
and
term.c
(in the language C)terminal.h
and
terminal.C
(pp. 159–161 in
Chapter 2)main.C
(pp. 158–159 in
Chapter 2)polar
and
cartesian
in pp. 362–366,
or the
scale
in pp. 371–375.
malloc
in C or
new
in C++.
The overhead will probably consist of a couple of numbers (probably
of data type
size_t
)
and/or a couple of pointers (probably void *
).
How is the overhead different when you allocate block that holds an array of
objects that have destructors?
Also write a C++ program that needs an array that grows because the program appends elements to the end of the array as it runs. Assume that there is no way to know in advance how many additional elements will be appended. Now there is no such thing in C or C++ as an array that grows: the number of elements in an array is fixed. But there are two ways to get the same effect:
malloc
and
realloc
; orvector
that grows.Program it both ways. Which one is simpler?
malloc/realloc
vs.
vector
.
Run the game consisting of the following 9 files, but don’t hand it in.
The do Homework 4.4a (p. 466).
Hand in one copy of only the files you had to modify for this homework.
For example, do not hand in
term.h
,
because you did not have to modify it.
Circle or highlight the code you modified.
Want to see a problem in object-oriented design?
We have to write code in one class
(class visionary
)
that appears to need access to the private data members of another class
(the
x
and
y
data members of class
wabbit
).
How can we handle this?
If you’re interested in the solution, do Homeworks
5.11a (a friend of class wabbit
, p. 576),
5.11b, 5.11c, and 5.11d.
The object-oriented design course INFO1-CE9267: OOAD_Syllabus.pdf, OOAD.pdf.
mark.meretzky@nyu.edu
if you are enrolled in the class but your name is not on
this list.
Read the
syllabus and grading policy.
Look at the
textbook.
See how much homework there was
last semester.
Admire last semester’s
class photo;
next week, we’ll have our own.
You can use any version of C and C++ on any platform for this course.
On Microsoft Windows,
the easiest way to get started is with Bloodshed (see below).
On Mac OS X,
use Xcode.
On the Unix host at NYU, use the GNU compilers
/opt/gcc450/bin/gcc
and
/opt/gcc450/bin/g++
(withe the
-std=c++0x
option if you want to experiment with
C++0x;
see
lambda
functions).
Put
/opt/gcc450/lib
into the environment variable
LD_LIBRARY_PATH
.
export LD_LIBRARY_PATH=/opt/gcc450/libOn Linux, install the packages for
gcc
and
gcc-c++
if you don’t already have them.
Run the game consisting of the following nine files, but don’t hand it in.
term.h
and
term.c
See
Chapter 1,
pp. 85–89.
These two files are in C; the others are in C++.terminal.h
and
terminal.C
.
See
Chapter 2,
pp. 157–163.rabbit.h
and
rabbit.C
.
See
Chapter 2,
pp. 192–200.wolf.h
and
wolf.C
main.C
Then update class
terminal
by repeating the Homeworks you did in C++ Part I:
2.14.2a
(static member functions for class
terminal
,
Chapter 2,
p. 244)
and 2.17b
(constant data members for class
terminal
,
Chapter 2,
p. 269),
but don’t hand them in.
Remember the 300 × 200 pixel flag we did last semster? Invent a structure to hold ech pixel:
struct pixel { unsigned char red; unsigned char green; unsigned char blue; };Make a class named
picture
.
Each
picture
should contain the following non-static, private data member:
pixel a[200][300]; //200 rows, 300 columnsThe constructor for class
picture
should take one argument,
a
“pointer to pixel
”
that points to the first element of a 300 × 200 array of pixels.
Class
picture
should also have two static data members named
black
and
white
.
These static data members should be all-black and all-white
picture
s.
(See
Chapter 2, p. 239,
Homework 2.14.1b, ¶ (4),
where class
point
acquired a static data member of type
point
.)
Now for the operator overloading. I’d like to be able to compare pictures for equality and inequality:
picture p1(argument for constructor); picture p2(argument for constructor); if (p1 == p2) {I’d like to be able to halve the color intensity of a picture. The following example would halve the red, green, and blue value of every pixel in
p
.
picture p(argument for constructor); p /= 2;I’d also like to say
picture p1(argument for constructor); picture p2 = p1 / 3;(You will have a justifiable feeling of pride when your
operator/
does its work by calling your
operator/-
.)
And superimpositions:
picture p1(argument for constructor); picture p2(argument for constructor); picture p3 = (p1 + p2) / 2;Warning: an
unsigned char
can hold only values in the range 0 to 255 inclusive (depending on your
machine).
When you add two
unsigned char
s
together, the sum will often be too big to fit in an
unsigned char
.
We saw the solution in the “timebomb”
example in
Chapter 3,
pp. 295–299.
The sum of two
picture
objects should be a
superimposition
object,
which will be just like a
picture
object except that the
red
,
green
,
blue
fields of its structures should be
unsigned int
instead of
unsigned char
.
And when you divide a
superimposition
object by 2, the quotent should be a
picture
object.
picture p1(argument for constructor); picture p2 = (p1 + picture::white) / 2;
I’d like to break a picture down into three pictures containing its red, green, blue components. Invent operators that will do this. I’d like to increase or decrease the contrast of a picture. Invent an operator that will do this. An operator to change a picture to back and white? An operator to subtract one picture from another? A weighted superimposition?
picture p1(argument for constructor); picture p2(argument for constructor); picture result = (5 * p1 + 2 * p2) / 7;You don’t have to do all of these. Just invent some operators that will do interesting things to picture objects. Keep them all 300 × 200.
revolutionary
and
nonrevolutionary
,
or manipulators with argument(s), like
scale
.
Discover the hidden numbers before a dynamically allocated block
on your platform.
vector_obj.C
on p. 439;
study it.
The
fork
examples are
here.
Nothing to hand in.
.h
file comes
before
the corresponding
.C
file.
Instead of making classes
wolf
and
rabbit
friends of class
game
,
should we have given class
game
member functions such as the following:
public: void put(x, y, c) const {term.put(x, y, c);} //just a call-throughThen p. 466, line 7 could be simplified to
g->put(x, y, c);
See my home page for my Fall, 2010 teaching schedule. Any suggestions how I could squeeze in a 30-hour C++ Part III INFO1-CE9266?
i5.nyu.edu
?
#include <cmath> int main() { std::sqrt(2.0); }
/opt/gcc450/bin/g++ junk.C init2.c:37: assertion failed: ((64 - 0)+0) == (((64 - 0)+0)/8) * 8 && sizeof(mp_limb_t) == (((64 - 0)+0)/8) junk.C: In function 'int main()': junk.C:4:22: internal compiler error: Abort Please submit a full bug report, with preprocessed source if appropriate. See <http://gcc.gnu.org/bugs.html> for instructions.
I fixed the bug in
exception1.C
on pp. 590–591.
The problem was that the value of the expression
-2147483648 % -1
0
with g++
version 4.2.1,
and -1
with our current g++
version 4.5.0.
See the loophole in §5.6, ¶ 4, of the
C++
Standard
(p. 113): “if the quotient … is representable”
(italics mine).
wabbit
destructor doesn’t beep, p. 571).
visionary
).,
6.9.3b (throw exceptions, p. 628),
and
6.9.3c (catch exception, p. 629).
On July 28, we’ll start doing templates.
Please bring a digital camera to the first class to take the class photo.
min
examples on pp. 634–640,
you will need
date.h
and
date.C
files;
get them from
http://i5.nyu.edu/~mm64/book/src/mydate/
.
See if you can figure out how
stand
and
step
work on pp. 655–658.
Read ahead in
Chapter 7.
My email address is
mark.meretzky@nyu.edu
Read the syllabus and grading policy. See how much homework there was last semester. Admire our class photo; next week, we’ll make it touch-sensitive.
If you want to use your Solaris Unix account on i5.nyu.edu,
get your
login
name
and
secret password.
You can connect to i5.nyu.edu from a PC via
PuTTY
,
from a Mac via
ssh
,
from an iPhone via
TouchTerm
,
or from Android via
ConnectBot
.
Our GNU
gcc
and
g++
compilers in the directory
/opt/gcc450/bin
i5.nyu.edu
gcc
and
g++
in the directory
/opt/gcc/bin
extern "C" double sqrt(double x); int main() { double d = sqrt(2.0); }
Run (but do not hand in) the program in
Chapter 1,
§1.7.3,
pp. 85–89, consisting of the following three files.
You may have to change the filename suffixes when you download them
(Save As…).
Any machine, compiler, and operating system is fine with me.
Uncomment one line in
term.c
;
see p. 89.
You’ll have to follow special instructions for this program because
To find these special instructions, search for 1.7.3 in the following instructions for Unix, Bloodshed, Xcode, and Code::Blocks.
Run (but do not hand in)
the program in
Chapter 2,
pp. 157–163,
retrofitted with the exceptions in
in
Chapter 6,
pp. 628–629.
It consists of the following six files.
You downloaded the first two last week.
On my platform,
C files end with lowercase
.c
;
C++ files end with uppercase
.C
.
On your platform,
you may have to rename them.
term.h
and
term.c
except.h
,
thrown by the member functions of class
terminal
.terminal.h
and
terminal.C
main.C
Run (but do not hand in) the game consisting of the following 22 files
(plus whatever additional grandchild classes you want, plus class
visionary
if you wrote it in C++ Part II).
term.h
and
term.c
except.h
,
thrown by the member functions of class
terminal
.terminal.h
and
terminal.C
game.h
and
game.C
wabbit.h
and
wabbit.C
manual.h
and
manual.C
immobile.h
,
brownian.h
inert.h
,
victim.h
,
predator.h
,
halogen.h
wolf.h
,
rabbit.h
,
sitting_duck.h
,
boulder.h
main.C
Then hand in one copy of the game incorporating Homeworks 7.2.2b (p. 693) and 7.2.2c (p. 695). Hand in only the files you had to create or modify.
int c; while ((c = getchar()) != EOF) { //do something with c; }
cmd.exe
,
or the Macintosh Terminal
window)
as follows.
progname < infile > outfileThen see how to do the same thing in C++ in Chapter 3, p. 329,
get.C
.
stand
and
step
yet.
To compile the
min
examples on pp. 634–640,
you will need
date.h
and
date.C
files;
get them from
http://i5.nyu.edu/~mm64/book/src/mydate/
.
See if you can figure out how
stand
and
step
work on pp. 655–658.
Read ahead in
Chapter 7.
Read the syllabus and grading policy. See how much homework there was last semester. Admire our class photo; next week, we’ll make it touch-sensitive.
If you want to use your Solaris Unix account on i5.nyu.edu,
get your
login
name
and
secret password.
You can connect to i5.nyu.edu from a PC via
PuTTY
,
from a Mac via
ssh
,
from an iPhone via
TouchTerm
,
or from Android via
ConnectBot
.
Our GNU
gcc
and
g++
compilers in the directory
/opt/gcc450/bin
i5.nyu.edu
gcc
and
g++
in
/opt/gcc/bin
extern "C" double sqrt(double x); int main() { double d = sqrt(2.0); }
Run (but do not hand in) the program in
Chapter 1,
§1.7.3,
pp. 85–89, consisting of the following three files.
You may have to change the filename suffixes when you download them
(Save As…).
Any machine, compiler, and operating system is fine with me.
Uncomment one line in
term.c
;
see p. 89.
You’ll have to follow special instructions for this program because
To find these special instructions, search for 1.7.3 in the following instructions for Unix, Bloodshed, Xcode, and Code::Blocks.
Here’s something that won’t be possible until the end of
Chapter 7,
but it would be educational to try it now and see why it can’t
be done.
The
print template functions
in pp. 643–644 printed a different message for a non-pointer, pointer,
pointer to pointer, pointer to pointer to pointer, etc.
Can we get the
name template
function
on pp. 668–669 to do the same?
For example, it should be capable of returning the strings
"int"
,
"int *"
,
"int **"
,
"int ***"
,
etc.,
either as arrays of characters or as C++
string
objects,
whichever is easier for you.
Note that
name
takes no function arguments (i.e., arguments in the parentheses).
Would it help if
name
did take an argument, preferably by reference?
Run (but do not hand in)
the program in
Chapter 2,
pp. 157–163,
retrofitted with the exceptions in
in
Chapter 6,
pp. 628–629.
It consists of the following six files.
You downloaded the first two last week.
On my platform,
C files end with lowercase
.c
;
C++ files end with uppercase
.C
.
On your platform,
you may have to rename them.
term.h
and
term.c
except.h
,
thrown by the member functions of class
terminal
.terminal.h
and
terminal.C
main.C
Run (but do not hand in) the game consisting of the following 22 files
(plus whatever additional grandchild classes you want, plus class
visionary
if you wrote it in C++ Part II).
term.h
and
term.c
except.h
,
thrown by the member functions of class
terminal
.terminal.h
and
terminal.C
game.h
and
game.C
wabbit.h
and
wabbit.C
manual.h
and
manual.C
immobile.h
,
brownian.h
inert.h
,
victim.h
,
predator.h
,
halogen.h
wolf.h
,
rabbit.h
,
sitting_duck.h
,
boulder.h
main.C
Then hand in one copy of the game incorporating Homeworks 7.2.2b (p. 693) and 7.2.2c (p. 695). Hand in only the files you had to create or modify.
min
examples on pp. 636–640,
you will need
date.h
and
date.C
files;
get them from
http://i5.nyu.edu/~mm64/book/src/mydate/
.
See if you can figure out how
stand
and
step
work on pp. 657–660.
Read ahead in
Chapter 7.
Read the syllabus and grading policy. See how much homework there was last semester. Admire the class photo; next week, we’ll make it touch-sensitive.
If you want to use your Solaris Unix account on i5.nyu.edu,
get your
login
name
and
secret password.
You can connect to i5.nyu.edu from a PC via
PuTTY
,
from a Mac via
ssh
,
from an iPhone via
TouchTerm
,
or from Android via
ConnectBot
.
Run (but do not hand in) the program in
Chapter 1,
§1.7.3,
pp. 86–90, consisting of the following three files.
You may have to change the filename suffixes when you download them
(Save As…).
Any machine, compiler, and operating system is fine with me.
Uncomment one line in
term.c
;
see p. 89.
You’ll have to follow special instructions for this program because
To find these special instructions, search for 1.7.3 in the following instructions for Unix, Bloodshed, Xcode, and Code::Blocks.
mantissa
that we saw in class on March 3 is in
precision.C
.
I improved it to save and restore the precision.
To really appreciate what a template function does for you,
write the
step
and
stand
examples in
Chapter 7,
pp. 657–660 without using templates.
You’ll be amazed!
Do not hand this in.
Could you write the following template function
alignment
?
It should return a
size_t
.
Is there a way you could write this template function as a single
function template that would return the answer for any data type?
Or would you have to write the template function as a
general-purpose template followed by an explicit specialization
for each built-in data type,
like
name
in
Chapter 7,
p. 670
and
mantissa
in
precision.C
?
Don’t hand this in,
but you can show your template function to the class on March 10
if you want to.
cout << "On this machine, an int must be aligned on a " << alignment<int>() << "-byte boundary.\n";
Click on Thomas A. Sullivan’s nose in the class photo to see the C++ Draft Standard. Search for “self-immolation”; it’s on p. 357.
If you have the Microsoft Visual C++ compiler, please email me the version number and the output you get when you compile and run the “point of definition/point of instantiation” program in Chapter 7, pp. 680–681?
Run (but do not hand in)
the program in
Chapter 2,
pp. 159–165,
retrofitted with the exceptions in
in
Chapter 6,
pp. 630–631.
It consists of the following six files.
You downloaded the first two last week.
On my platform,
C files end with lowercase
.c
;
C++ files end with uppercase
.C
.
You may have to rename them.
term.h
and
term.c
except.h
,
thrown by the member functions of class
terminal
.terminal.h
and
terminal.C
main.C
align
template function.
Run the game consisting of the following 22 files
(plus whatever additional grandchild classes you want, plus class
visionary
if you wrote it in C++ Part II).
term.h
and
term.c
except.h
,
thrown by the member functions of class
terminal
.terminal.h
and
terminal.C
game.h
and
game.C
wabbit.h
and
wabbit.C
manual.h
and
manual.C
immobile.h
,
brownian.h
inert.h
,
victim.h
,
predator.h
,
halogen.h
wolf.h
,
rabbit.h
,
sitting_duck.h
,
boulder.h
main.C
step
and
stand
,
p. 729)
and Homework 7.2.8a (p. 742).
Not to be handed in: given an
n-bit mantissa,
how many decimal digits can it hold?
bubble3.C
double
on your machine (p. 750)?
Write a program that tries to store this number into a
double
and prints the resulting contents of the
double
.
Does it round up or down?
Also print out the
round_style
data member of class
numeric_limits<double>
.
Also demonstrate that a
double
can hold the next largest and next smallest whole numbers.
Hand in Homework 7.2.9c (p. 751).
Do Homework 7.2.9d (p. 755),
but don’t hand it in.
node:const_iterator
printable.h
(Homework 7.2.9c, p. 751)
compile on your platform?
stepper.h
and
step.C
from
Chapter 7,
pp. 657 and 729.
bind1st
;
it will be an imitation of the
bind2nd
on p. 865)
and
8.4.2g on p. 873
(compose1
;
it will be a scaled-dwon version of the
compose2
on pp. 868–869).
Here is the
in-class quiz
I gave my C++ Part I students on April 19;
it’s made entirely of homeworks they handed in on April 12.
Please email me a link about C++ lambda functions.
If you use your i5.nyu.edu account, email
comment@i5.nyu.edu
asking him/her/them politely for g++ version 4.5.0 at
http://gcc.gnu.org/gcc-4.5/
.http://i5.nyu.edu/~mm64/book/src/find_if/find_if1.C
contains
p = find_if(a, a + n, __gnu_cxx::compose2( logical_and<bool>(), bind2nd(greater<int>(), 35), bind2nd( less<int>(), 45) ) );
http://i5.nyu.edu/~mm64/INFO1-CE9266/src/find_if1.C
contains
p = find_if(a, a + n, [](int x) {return x > 35 && x < 45;});Invent your own example, so you’re not all mailing him/her/them the same code.
Point your browser at the
ring terminal.
Create a copy of
main.C
with the IP address of the host on which your browser is running
(or modify
main.C
to take the address as a command line argument via
argc
and
argv
,
or as an environment variable via
getenv
).
Then run the C++ program consisting of
makefile
,
term0.h
,
term0.c
,
and your
main.C
.
With the browser window as the active window,
type characters ending with lowercase q.
It didn’t receive my keystrokes in class on April 28
was because
in my excitement
I was typing them into the wrong window on the Mac.
Don’t be shy about saying
netstat -a -f inet -P tcp
Run the program in
Chapter 9,
pp. 972–984
consisting of the two files
terminal.h
and
main.C
.
Then write a C++ program that reads and writes this new terminal.
Or write a class
terminal0
to sit on top of the
term0_
C functions that read and write the ring terminal,
and a C++ program that constructs and uses a
terminal0
.
Next week: valarrays and dispatching in Chapter 8, RTTI in Chapter 10.
for_each.C
ssh
,
from an iPhone via TouchTerm,
or form Android via ConnectBot.
Call the
computer labs
and see if they will let you in.
With the
GNU
C compiler
gcc
,
and the C++ compiler
g++
,
you can have parentheses around the left operand of an assignment.
This came up in
Chapter 7,
p. 646, second line 5.
int i = 10; (i) = 20;
Run (but do not hand in) the program in
Chapter 1,
§1.7.3,
pp. 86–90, consisting of the following three files.
You may have to change the filename suffixes when you download them
(Save As…).
Any machine, compiler, and operating system is fine with me.
Uncomment one line in
term.c
;
see p. 89.
You’ll have to follow special instructions for this program because
To find these special instructions, search for 1.7.3 in the following instructions for Unix, Bloodshed, Xcode, and Code::Blocks.
Make a flag if you have not already done so.
Study the hardest stuff: “Change T to the simplest data type” in Chapter 7, pp. 637–639. After seeing your expressions of shock and horror, I simplified the peek function; see the new description on pp. 646–647. Extra credit: I showed you the layout of a double on my Sparc Sun machine running Solaris. Can you use peek to discover the layout of a double on a different machine?
On Wednesday, June 3, at the last possible moment, print a little more of Chapter 7 and bring it to class. (How far do you think we will get?) Please email me if things don’t work: mark.meretzky@nyu.edu
Run the program in
Chapter 2,
pp. 157–163, consisting of the following six files.
You downloaded the first two last week.
On my platform,
C files end with
.c
;
C++ files end with
.C
.
You may have to rename them.
term.h
and
term.c
except.h
,
thrown by the member functions of class
terminal
.terminal.h
and
terminal.C
main.C
Then run the program consisting of the following 22 files
(plus whatever additional grandchild classes you want, plus class
visionary
if you wrote it in C++ Part II).
term.h
and
term.c
except.h
,
thrown by the member functions of class
terminal
.terminal.h
and
terminal.C
game.h
and
game.C
wabbit.h
and
wabbit.C
manual.h
and
manual.C
immobile.h
,
brownian.h
inert.h
,
victim.h
,
predator.h
,
halogen.h
wolf.h
,
rabbit.h
,
sitting_duck.h
,
boulder.h
main.C
printable
in
Chapter 7,
p. 732
is the class
sat
in
Chapter 3,
pp. 339–341.
Yes, you can have three template preambles stacked on top of each other (Chapter 7, p. 724):
#include <iostream> #include <cstdlib> using namespace std; template <class T> class outer { public: template <class U> class inner { public: template <class V> void f(); }; }; template <class T> template <class U> template <class V> void outer<T>::inner<U>::f() { cout << sizeof (T) << " " << sizeof (U) << " " << sizeof (V) << "\n"; } int main() { outer<char>::inner<short> i; i.f<int>(); return EXIT_SUCCESS; }
1 2 4
The Big Idea:
code is packaged as a class so that it can be passed to a template.
The code is passed at compile time,
as a template argument.
The
isprint
error checking was packaged as a class
printable
(p. 732)
so that it could be passed to the template class
terminal
on pp. 736–741.
The
>
comparison was packaged as a class
greater_int
(p. 764)
so that it could be passed to the template function
sorter
on p. 763.
(In this case,
we are passing in more than just the template argument
COMP
.
We are also passing in the function argument
comp
.
But
comp
is a big nothing—it doesn’t even have data members.)
Run (but do not hand in) the C program
qsort.c
on p. 771.
map
Homeworks 8.1c, 8.1d, 8.1e.
Please hand in only the files you had to modify;
circle or highlight the code you changed.
node::const_iterator
)
on p. 811.
The easiest thing we did on July 8:
took the exposed
p
and fragments of code in the
for
loop in
Chapter 8,
p. 803,
lines 15–17 of
main.C
,
and packaged them in p. 802, lines 13–29 and 32–35 of
node.h
.
The five hardest things we did on July 8:
main.C
calls
count
,
how does
count
decide whether it should return an
int
,
long
,
or whatever?std::iterator<forward_iterator_tag, int>
,
used in p. 810, line 13 of
node3.h
,
aquire the following public member?
typedef int value_type;
node::iterator
aquire the same member?
And then how did the class
iterator_traits<node::iterator>
aquire the same member?
main1.C
,
why is the first record (if any) read from the input file
by the iterator’s constructor,
and why are the remaining records (if any) read from the input file
by the iterator’s
operator++
?
main.C
,
why can’t the
operator*
member function of
it
write the integer to the output file?
find_distance
,
p. 829),
8.3b
(adjacent_find
,
p. 831),
and
8.3c
(sort
,
p. 833).
Extra credit.
Could you write an algorithm (i.e., a template function)
that would accept an iterator passed by value
(or a pair of iterators, if necessary)
and return true (or at least compile)
if the iterator is an input iterator?
It would return false (or fail to compile)
if the iterator is not an input iterator.
To qualify as an input iterator,
its specialization of
iterator_traits
must have the five typedefs.
You would have to verify that statements such as the following
do in fact compile.
typename iterator_traits<IT>:pointer p = &*it; typename iterator_traits<IT>:value_type v = *p;Should the goal be to return true or merely to compile? If the goal is to return true, would the iterator(s) passed in as arguments have to refer to elements in a container, or could they be the end-of-container iterator?
Could you write an algorithm
that would accept an iterator passed by value (only one iterator this time)
and return true (or at least compile)
if the iterator is an output iterator?
It would return false (or fail to compile)
if the iterator is not an output iterator.
Since an output iterator never checks for end-of-container,
you don’t need to verify that you can compare two iterators.
In fact, the function will need only one iterator argument.
To qualify as an output iterator,
its specialization of
iterator_traits
does not need to have the last four typedefs.
Ditto for the other three categories: forward, bidirectional, random access.
min_element
algorithm,
which we’ll do in 30 seconds on July 29).
Hand in Homeworks
8.4.2e (p. 860)
and
8.4.2g (p. 867).
Look at Homework 8.4.2f (p. 864), but don’t do it.
What would be a better example for the
for_each
algorithm?
Stroustrup’s
C++ page.
Please remind me to bring Einstein’s brownian movement book.
Here are the catalog descriptions I wrote for the course (with peppy suggestions from a student).
From online gaming services to global real-time trading systems, C++ is the language of choice for complex, large-scale applications that demand peak performance. This three-part course teaches you the techniques for creating bigger classes and objects out of smaller ones, in Part I with aggregation, in Part II with inheritance, and in Part III with templates. Part I covers the non-object topics of C++: standard i/o, expression evaluation, placement of declarations, type conversion, pointers and references, C functions called from a C++ program, function name overloading, default values for function arguments, and inline functions. You then build classes and objects without inheritance, using data members, member functions, constructors, destructors, friend functions, arrays of objects, constant and static members, pointers to members, aggregation, and operator overloading. Large-scale programming exercises. Prerequisite: knowledge of pointers and structures in the language C.
Make a long-term investment in your future as a developer with this three-semester course. Pick up from Part I with three applications of operator overloading: formatted and file i/o, dynamic memory allocation, and standard containers such as vector, list, and string. Then concentrate on building classes using the different varieties of inheritance: single and multiple, virtual and non-virtual, public and private, and inheritance from an abstract base class with pure virtual functions. See how object-oriented design results in maintainable, extensible architectures; also explore the implementations of, and alternatives to, inheritance. Report and recover from runtime errors by throwing and catching exceptions. Large-scale programming exercises. Prerequisite: C++ Part I INFO1-CE9264.
See how the problems plaguing inherently difficult programming projects are solved with the C++ Standard Template Library (STL) in Part III of this three-semester course. Deploy template functions and template classes, and explore their interaction with the inheritance in Part II. Master the design of the Standard Template Library: containers, iterators, and algorithms; iterator traits and iterator categories, predicates and other function objects, and adapters. Learn how the STL delivers performance competitive with hand-crafted assembly language; decipher the library error messages by tracing how the algorithms are dispatched. Exploit iterators and other design patterns to achieve a loosely coupled architecture for migrating software to new environments and international locales. Large-scale programming exercises. Prerequisite: C++ Part II INFO1-CE9265.
This is a C++ course for people who already know C well enough to handle pointers, structures, and pointers to structures. You must also be able to use a pointer to a structure as the argument of a function, since this is the take-off point from C into C++. No one cares how many semesters of C you have taken, how long ago it was, or what your grades were.
You must also know the pairs of C functions fopen/fclose and malloc/free, since events in C++ happen in pairs. (These pairs are triggered by “constructors” and “destructors”.)
INFO1-CE9264 and INFO1-CE9265 are platform independent. We cover only the C++ language, not the different vendors’ compilation, execution, and debugging environments. You must therefore know how to create, compile, and execute a C or C++ program on the platform of your choice. In Unix, you must type a command line or create a script or “makefile”; in Microsoft Visual C++ and Borland C++, you must create a “project”.
You must also know how to create, compile, and execute a multi-file C or C++ program. For example, the main function of your program may call a function defined in another source file.
To connect to the wireless at 48 Cooper Square, you need an NYU NetID consisting of several lowercase letters followed by several digits. If you don’t already have it, get it from here. You also need a password for the NetID. If you don’t already have it, get it from here. Then configure your device for the wireless network. The SSID is “nyu”. The security should be set to “WPA2 Enterprise.” The username and password is the NetID and its password. More detailed instructions for connecting to the WiFi with the iPhone and other devices are here.
Every student gets a free Unix account on the host i5.nyu.edu for the duration of the course. It has the GNU compilers gcc and g++. Follow these directions.
To run C and C++ on your own Unix or Linux, download the free GNU compilers gcc and g++.
To run C and C++ on your own Windows computer, download the free Bloodshed Dev-C++, a GUI-interface for g++. Follow these directions.
Follow these directions.
Get your i5.nyu.edu login name, activate your account, and log into i5.nyu.edu via ssh. Create a public_html subdirectory of your home directory, and change the nine mode bits of both directories to rwxr-xr-x.
cd pwd
mkdir public_html ls -ld public_html (minus lowercase LD)
chmod 755 . public_html (dot means yourcurrent directory) ls -ld . public_htmlMove or copy the files that you want to display on the web to your public_html subdirectory, and change the mode bits of the files to rw-r--r--.
mv yourfile ~/public_html (with a tilde) cp yourfile ~/public_html
cd ~/public_html pwd ls -l | more (minus lowercase L)
chmod 644 * ls -l | more (minus lowercase L)Then point your web browser at http://i5.nyu.edu/~yourloginname/ (with a tilde).
Then take me disappearin’ through the smoke rings of my mind,
Down the foggy ruins of time, far past the frozen leaves,
The haunted, frightened trees, out to the windy beach,
Far from the twisted reach of crazy sorrow.
Yes, to dance beneath the diamond sky with one hand waving free,
Silhouetted by the sea, circled by the circus sands,
With all memory and fate driven deep beneath the waves,
Let me forget about today until tomorrow.
Hey, Mister Tambourine Man…