Computer Science 210 Lab 1: C++ Programming Introduction
Due: Feb 1, 1999

Objectives and Overview: This lab will help you become familiar with writing and running C++ programs using the CodeWarrior system on a Mac PowerPC. It will also help you become familiar with basic C++ syntax and its differences from Pascal and Java.

Part 1 - Complete the C++ Tutorial

The document Writing CodeWarrior C++ Applications contains a complete tutorial for starting CodeWarrior on the Macs, and writing and running a C++ program. Please complete this tutorial before going on to Part 2.

After you finish this tutorial, notice that there is a folder called Course Libraries -> CS210 (Chown) in the Macintosh HD folder. To access this folder you will need to go through a login process when you double click on it. The first time you login your ID will be your normal polar ID. Your password will be the same as your ID. The first thing you should do is change your password (I suggest using your polar password. You will need to login everytime you use the course library folder in the future. Once logged in you will have access to the CS210 (Chown) folder which has three items inside it (see below):

The Drop Box folder is the place where you drag completed C++ program files to turn them in. To ensure your identity, please add your name to the program file before submitting it. For instance, if I have just finished writing and testing the program myfirst.cpp, I would rename it myfirst.echown.cpp before dragging it to the Drop Box.

The source folder contains the C++ source code for all the programs and classes shown in your text. You may copy and run any these programs as you need them to complete assignments or clarify the discussion in the text.

The Users folder contains folders for everyone in the class. You will have your own folder which you can use for personal storage, saving assignments, etc. There should be no reason to use disks, Fetch, appleshare, etc. for the duration of the class.

Part 2 - Exercise a C++ Program

The first few lines of a program are shown below. A complete electronic copy of this program is in the source folder as the file named GetInts.cpp.

This program reads an arbitrary number of integers and then displays them. It allocates an array that grows dynamically in response to the size of the input.

The program exhibits several Pascal-like ideas in C++ form -- assignment statements, arrays, for loops, if statements, and output statements. To exercise this program, follow the steps below (starting from the project Lab1 which you created in Part 1.

  1. Create a new C++ project on the desktop, following the steps outlined in the tutorial, and name it Lab1.µ.
  2. Drag a copy of this program file, called GetInts.cpp, from the examples folder onto the desktop, and then add it to the Sources folder inside your project (use the Project -> Add Files menu option).
  3. Remove the HelloWorld.cp program file from the Sources folder in your project (use the Project -> Remove Selected Items menu option).
  4. Compile this program (use the Project -> Make menu option).

Your project window should now look like this:

Now run this program (using the Project -> Run menu option). The output should appear in a window like the one shown below; this window is also used for typing input. For instance, when you type the integers shown on the first line, a fresh copy of these integers will be listed below that line. Note that you must signal the end of your input by typing ctrl-d.

Part 3 - Answer the following questions about this program

  1. What is the initial size of the array Array when the function GetInts is called?
  2. Describe the process by which Array increases in size?
  3. When Array increases in size, by how much does it grow?
  4. What is the purpose of the variable NumItems in the main program? What is its value for the program run shown above?
  5. Why is the statement int *Original = Array; needed?
  6. Why is the statement delete [] Original; needed?
  7. For the input shown above, what is the size of Array when the function GetInts finishes its work?
  8. What is the purpose of the first line in this program (the first line of the file)?
  9. What two different actions are triggered by the line while (cin >> InputVal) in this program?

Part 4 - Write another C++ program

Your new program should ba named myfirst.cpp, and will have a similar overall structure to the one discussed above. However, its output should be a rearrangement (sorting) into ascending order of the integers entered, along with a count of the number of integers that are negative, zero, and positive. For instance, the above input should yield the following output:

-12 0 2 3 4 6666
1 negative
1 zero
4 positive

You may use any sorting algorithm that you know, as long as it is written as a separate C++ function with parameters Array and NumItems (the array and its size) and it calls the function Swap shown below. If you don't know any sorting algorithms, the simplest is probably the bubble sort. To do a bubble sort you go through your array comparing consecutive elements and swapping them if they are in the wrong order. The first time you do this the biggest element will be at the end of the array, the second time, the second biggest will be at the next to last spot, etc.


Swap( int & A, int & B )
{
   int Tmp = A;
   A = B;
   B = Tmp;
}

Lab 1 Deliverables:

Now submit your revised C++ program from Part 4 of this lab by dragging it to the CS210 (Chown) -> Drop Box folder. Don't forget to rename your program file by affixing your name to it. For example, if I were submitting it, the program file would be called myfirst.echown.cpp, not just myfirst.cpp.

Also, hand in a hardcopy listing of your program with your name on it, along with the answers to the questions in Part 3.