Complete the following lab by Wednesday, January 20, at 4:00. The parts marked *** are to be handed in. Some of these will be written answers to questions and others will be programs. Tar the files in your lab directory and send them to me ingram@roanoke.edu.
Getting Started: Create a directory for this lab. You can either download the example programs to your directory from the Web as you go through this (go to the course web page at http://cs.roanoke.edu/Spring2010/CPSC270A to get this web page) OR just copy them all to your directory using cp as follows (assuming you are in your directory for this lab):
cp ~cpsc/public_html/Spring2010/CPSC270A/lab1/*.java . cp ~cpsc/public_html/Spring2010/CPSC270A/lab1/*.cc .
int main() or int main (int argc, char * argv[])
Note that main returns an int. Usually we return 0 but this can be used to return status info to the operating system (0 means all is well!).
#include <iostream> using namespace std;
cout << "Average delay: " << delay << endl;The endl represents the new line (the escape sequence \n may also be used).
cin >> num;The data read in becomes the value of the variable num.
g++ myProg.ccThis creates an executable in a file named a.out. To execute the program you just type the pathname of the file (you must include the dot for your current directory):
./a.out
g++ myProg.cc -o runP
g++ -c myProg.ccThis creates a file named myProg.o. Now link (in this case with the library functions) and create the executable in runP:
g++ myProg.o -o runP
if (n = 0) ...What would happen in Java if you had this in your program? How would it be interpreted in C++?
Exercise: See if you were correct by examining the behavior of JavaIf.java and CPlusIf.cc.
Type bool: Even though zero means false and any nonzero value means true in C++, most implementations of C++ have added type bool which includes identifiers true and false. This type should be used in programs rather than using integers. However be aware that true is actually stored as 1 and false is 0.
The boolean operators in C++ are the same as in Java: &&, ||, !
const int DOZEN = 12;This is done inside a function.
#define DOZEN 12This is done outside of a function (before main or, later when we use header files, in the header file).
const int MAX_SIZE = 10; int list[MAX_SIZE];This declaration allocates space for 10 ints. The space is allocated at compile-time and cannot be changed while the program is running. One consequence of this is that the following is not legal in standard C++:
int size; cin >> size; int list[size]; // NOT LEGAL - size was not known at compile-timeLater we will see a way to allocate space at run-time - dynamic rather than static allocation.
C++ allows the programmer to be careless with array subscripts. For this (and other reasons) it is generally best to use the vector class (later!) than the built-in arrays. HOWEVER, there is a lot of code written with arrays so a computer science major should be familiar with them and their idiosyncracies (hazards!).
Exercise: The programs JavaArrays.java and CPlusArrays.cc use arrays.
int sum (int size, int a[])
IMPORTANT FACTS ABOUT ARRAYS AS PARAMETERS:
In C++, unlike Java, the compiler must have encountered the header (signature) of a function before it encounters a call to that function. When the function is in the same file as the call there are two ways to handle the requirement.
Exercise: First study the program ArrayFunctions.cc, then compile and run it. Then add a function that doubles every other element in the array starting at the first one. Note that this function will have void return type. In main add a statement that prints the new array (or add a function to print an array then call it) after the current statement that prints the original sum, the call the sum function to print the new sum.
Exercises:
// Finds all primes less than or equal to n. // PRE: n is a positive integer > 1 // POST: prime is an array containing all primes less than or // equal to n; numPrimes is the number of primes in the array void sieve (int n, int[] prime, int& numPrimes)We will discuss the & in the next lab - it means that numPrimes is passed by reference so the function can actually change it. The call to the function would just put your variable name with no &. An alternative would be to return the number of primes; however, many people frown upon a function that both changes some of its parameters (such as the array in this case) AND returns something.
Note that you should declare a constant for the maximum size of n so you can allocate large enough arrays. Make the max at least 500. Your main function can check to make sure the input for n is valid.