Introduction

Preliminary Remarks

Check prerequisites and whether the class has had CS II or is taking it.  This will affect the sequencing of the material that will be taught and the amount of instruction/review on C++.

Explain cheating policy.

Talk about work ethic for class and goal that everyone does well in the class. It is the responsibility of the students to ask questions if they are confused.  Everyone is important.

Mention that I am available by EMAIL  Labs will be turned in via EMAIL where the source code is submitted as attachments.  This is just the .cpp and .h files.  Everyone will use visual studio to do their labs.   We will use visual studio .NET 2005 to develop our programs.  I will teach everyone how to use this software.  Discuss why it is important to be comfortable in the .NET environment.

The URL: http://phobos.ramapo.edu/~vmiller contains notes and assignments for the course.   I will be updating this site as the semester progresses.  Please don't think that the notes are finalized until the weekend after they are presented.  I like to update before and after class.

Text:

Mark Allen Weiss. Data Structures and Algorithm Analysis in C++, Third Edition. Person Education, Inc. (Addison-Wesley) 2006

The book is using the most recent features of C++.  You will grow into it.  In the beginning we will not be tightly tied to it.  My notes might be a better reference for you.

Goals for the course:

  1. A knowledge of the basic data structures. These are the "tools of the trade". Mention abstract data types. (ADT). Mention STL in C++.
  2. An understanding of some of the algorithms used to manipulate ADT. This will include an analysis of their efficiency. Tell story of consultant that did not understand efficiency issues with respect to sorting. 
  3. Improved programming skills. This is the criteria by which you will be initially judged when you start your first job and by which you will continue to be judged.  Mention that it is likely that you will have a programming test on a job interview.
  4. A knowledge of the physical and logical characteristics of storage devices and how to access files in C++.
  5. An introduction to STL.
Grading Policy.

4-8 quizzes 50%  If we have more than 3 quizzes, I will drop the lowest one.

Final Exam 50%

Approximately 10 to 12 programming projects. The projects are an integral part of the presentation of the course material. It is therefore expected that the student will complete each of them. The deadline for each project is one month or the end of the semester - whichever comes first. A deduction of 6 points from final grade will be made for each project not completed. 3 points will be deducted for each late project.  You may hand in a program multiple times if I reject it. 

There will also be a 2 point penalty for coming to class late without an excuse.

Review of C/C++

Why a review?  To reinforce what you know, and detail, and learn a few new things.  The best learning is in steps, with each step more complex than the previous.  In fact, I do a more advanced review for the Software Design class.

This section is devoted to reviewing some of the important ideas from C++ and possibly introducing a few new ideas.  Please make sure that you let me know if you are confused in any area.  Keep in mind that the material will be reinforced by examples and programs in this class and other classes.  I will not go into the detail of the text.

Include Files

Depending on where you learned C++ you might have a question about include files.  For most C++ compilers, there is iostream.h and iostream.  Which should you use and why should you care about the other?  What does the namespace keyword mean.

Integer data:

Discuss short int, int, long, and long long as data types.  We want to understand the number of bits in each and get a feeling for the range of values.  What is the difference between signed and unsigned data types?  Indicate why we care about the sizes.  Note about overflow and how multiplying two big positive numbers can yield a negative.

Character Data

Discuss ASCII representation for character data.  Mention char data type and character variables and constants.  Strings can be stored in character arrays.  Mention functions to handle character strings.  Note that char is a 1 byte integer.

bool data

Allows use to define variables that may be true or false.  Note that is usually represented in a byte with a value of 0 for false and 1 for true.

Floating point numbers

The problem with representing floating point numbers on a computer is to be able to specify a wide range of numbers.  Need to represent both a number and an exponent.  There are 3 floating point data types in C++ float, double, and long double.  Typically float is 4 bytes in size and has 7 digit precision, double is 8 bytes and about 15 digit precision, and long double is 16 bytes and 30 digit precision.  For long double, many compilers (like Visual Studio 2005) cheat and use 64 bit representation.  That is they use double's to serve as long double's.

Note about the weird stuff: we have representations for undefined and for not a number. (NaN)  See class for a discussion.

Data Type sizes and descriptions

The size of the various data types are compiler dependent.  The following is a link to a Microsoft site providing information on Visual Studio 2005 C++ data types:

http://msdn2.microsoft.com/en-us/library/s3f49ktz(VS.80).aspx

Operators

The class will list all the operators that they know.  We will discuss some of the weird things: like the ternary operator ? : and the precedence of the bit operators.

Arrays  (9/12/2007)

Why have? Allow us to keep a collection of similar data. Example: Write a program to read in 100 numbers and display a count of how many are greater than their average.  So how to initialize an array.  Show how to find the number of elements in an array using the sizeof operator.  Discuss why assignment of arrays does not work.

Two dimensional arrays: What are they? How define them. (  int x[5][4]; )  Why use? List of names if use character arrays to hold words. Pictures. Spread sheets.  Topology of earth.   Example: Create a two dimensional array of 100 rows and 200 columns and set all its elements to zero.  

Note: there are substitutes for arrays that work much better.  These will be discussed later.

Strings

Strings are a replacement to using character arrays to hold words and sentences.   What is wrong with using character arrays for this purpose?  It is rare that anyone uses character arrays to hold strings of characters. Why?  Note: If you use character arrays at work to hold strings, people will literally laugh.

Strings are easy to use.  They do have a lot of features.  I will make my initial presentation in an example.  Other features will be discussed during the class.  We will put this into Visual Studio .NET and run it.  There is a link on my home page on how to compile and run programs in Visual Studio 6.0 and Visual Studio .NET. 

Note: cin >> name; returns zero when "end-of-file" encountered.   What is an "end-of-file" for keyboard input.

Pointers:

Pointers: by example.
 

int i;
int *j;
j = &i;
*j = 15; // De-referencing.
Show how pointers can be used to manipulate arrays.

Review or introduce  new and delete by example:
 

int *x;
x = new int;
delete x;
char *y;
y = new char[1000];
delete [] y; // Must indicate that you are deleting an array by using the square brackets.


Notes: about space still being there after delete.

Discuss new, delete, malloc, realloc, and free.  Explain why we use new/delete instead of malloc/free.

Let's write a program to read in N real numbers and display a count of how many are greater than the average.  Assume that you do not know N ahead of time.

Look at pages visual C++. 

struct

The struct provides a way of grouping variables together.  Example;

struct Student {
        string name;
        string address;
        double gpa;
        double moneyOwed;
};

A struct does not set aside memory.  It defines a new data type.  So we can declare variables of type Student
     E.g. Student duck, dog;  or Student x[2000];

We will do the word count program using structures.  This will also be our demonstration program.  We will show how to create a project using visual C++ and how to send it to me.  Then show how we can bring in the example program on strings.

Present the union statement so people know what it is.

Reference variables

These are not available in C.  They allow one variable to refer to another.  For example:

int x = 1;
int &y = x;
y = 3;        // Has the same effect as x = 3;

We will discuss an example where reference variables are useful  We will use them to simplify changing an element of an array of structs.

The further you go in Computer Science, the more useful you will find them.

Functions:

Why use?

Examples:  Write a function to find the largest element in an array.  Write a function to compute the factorial.  Use this to introduce recursion. 

Question: what is the role of the prototype.

We will also discuss:

I will be assigning you two programs on functions.  Note: All labs assigned must be done using visual C++.  EMAIL me when they are complete with the C++ files Do not send me everything that visual C++ generates.   Just send me the .cpp and .h files that you have created.  I will grade your labs and send back either a grade or a request to redo the lab.  No labs will be accepted on paper

If you have a problem with a lab, please send me the source file with your question if there is a problem with the source file.  You should keep a copy of your labs on a portable storage media.  Flash cards are pretty cheap and a nice way of being portable.

Lab1  Write a program that will read in n integers and display the difference between each integer and the smallest. Use new to allocate space for the integers.   Why do we need to use new?

Lab2  Modify lab1 so that the user will indicate when they are done typing by entering a "-1".  Use new and delete to accomplish this task.  This lab is a bit more tricky.  We will discuss this lab in class.

Please submit labs to me using email.  Remember, I only want the .cpp and .h files used by your program.

9/19/2007

I got the permission of a student to use a variation of his lab as an example to demonstrate a few concepts in C++.  Consider the following program:

StrangeExample

We will run this program and see that it does solve lab 2.  But it doesn't really solve it.

  1. It demonstrates how scope issues can cause interesting problems.
  2. It demonstrates that you can exceed the bounds of allocated memory without always having problems.
  3. I will ask in class how to have the make problems.  It will not be easy.  We will do a little arithmetic to find the problem.  Need more than 10 numbers.

Classes

This section will be devoted to discussion various aspects of classes.  I will not assume any previous knowledge.  If you have only had Computer Science I, your Computer Science II course will reinforce the information presented here.  If you have taken Computer Science II, then this presentation  will reinforce the material you learned in this course.

Classes are essential in developing object oriented programs.  They are not only found in C++, but in other languages such as Java, C#, and VB .NET.  (Java and C# requires that all functions, including the main function be in classes.)  A class may be thought of as a package of variables and functions to implement some "thing".  For example, you might want to have date, time, point, client, trading model classes.  Classes, once they are written, let you think in the "big picture" instead of details.  For example, once I have a good date class, I never have to worry about writing code to manipulate dates.

Why Classes?????

Everything I discuss here won't make complete sense until I present more material.  But it is good to know why learning classes is worth the effort.

  1. To tie together data and the functions to manipulate them in a coherent manner.  Consider for example a date class.  A date may be represented by a single integer of the form YYYYMMDD.  Where YYYY is a four digit year, MM a 2 digit month and DD a 2 digit day.  What functions that you would like to package with a date.  Note: once you do a good job with a date class, you will not have to rewrite it.   You will only add to it.  Give example about faking today.
  2. To allow for a hierarchy of tools and representation. Example, communications code. This is a good way of reusing existing code.  We won't need this feature for the data structures course.
  3. To separate the implementation from the API.  What does this mean?
  4. To hide the data.  Why would we want to do this?  Mention the concept of changing the way data is stored in a class without disturbing any program that uses it.
  5. To allow for a more natural organization in programming. That is organization by data element rather than by function. It is easier to think of a project in terms of things rather than actions.  This is big because it makes it easier to handle large programming projects.
  6. We do NOT use classes to make programs smaller! Just more readable.   Actually, by using classes, the source code will be larger.

Format of a class:

A class declaration has the following format:  (I do not list everything.)

9/26/2007

Since the public section is used to provide an interface to the user of your class, it usually comes first.  What does an interface mean? 

In class I will show you how to declare a variable of type duck and how to access its members.

The class can have inline functions rather than just prototypes. If you have a pure prototypes in the .h file, the functions must be implemented in a C++ source file. (That is a .cpp file) In the function header, the function name must be prefixed by the class name followed by "::". See the vtime project for an example of a simple class.  Here is the vtime.h and vtime.cpp files.   Note that for short functions, you implement in the .h.  Otherwise you implement in the .cpp.

Notes

  1. a class definition does not allocate memory.
  2. Can define operators that use the class as the first argument.   Why would you want to do that?
  3. Note about friend functions and classes.
  4. You can defined operators on the class. Will do a + to add seconds onto time. (Example does not check validity of data.)
  5. Show how to test a class.  That is we will put the class into a Visual Studio solution and write a main program to exercise it.

Lab3

Change the vtime class so that time will be represented as a single integer.  (The class example used 3 integers.)  That is time will be represented as hours * 3600 + minutes * 60 + seconds.  In class we will discuss why we do this.