Complete the following lab by Thursday, January 27. Create a lab1 directory for your work and tar the files in the directory and send the tar file 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/Spring2011/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/Spring2011/CPSC270A/lab1/*.java . cp ~cpsc/public_html/Spring2011/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).
//************************************************** // hello.cc - prints a friendly greeting // // Author: J. Ingram //************************************************** #include <iostream> using namespace std; //-------------------------------------------------- // main // - takes no parameters // - puts a string of characters ("Hello World!") // on the output stream //-------------------------------------------------- int main () { cout << "Hello World!" << endl; return (0); }
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 underneath booleans are still integers - true is actually stored as 1 and false is 0.
The boolean operators in C++ are the same as in Java: &&, ||, !
int area (int length, int width) { return length * width; }
The default mode of parameter passing in C++, as in Java, is pass by value. Parameters passed this way are not changed by the function (method) because the value of the actual parameter is copied into the formal parameter and the formal parameter is what the function uses. But in Java, because the "value" of an object variable is a reference, the reference isn't changed but the object it refers to can be changed. Java has no provision for passing a parameter that is a primitive data type in a way that lets the method change the actual parameter but C++ does allow passing by reference.
To pass a parameter by reference in C++ you use the ampersand symbol (which denotes the address of a variable) in the header for the function. So, the following would pass the first two parameters by reference and the third one by value:
void doStuff (int& a, int& b, int c) { c = c * 2; a = a + c; b = b - c; }A call to the function would just use the int variables for the actual parameters - the address of the first two would be passed. For example,
int m = 150; int n = 100; int p = 5; doStuff(m,n,p);
Method #1: Define the function before it is used. The file ReferenceParameters1.cc uses this method for the doStuff function above.
Method #2: Declare the function near the beginning of the file; define it after main. When you do this it is standard practice to just list the type of each parameter in the declaration (when it is separate from the definition). In this example the declaration would be:
void doStuff (int&, int&, int);
The file ReferenceParameters2.cc is the same program as above but using this method.
Exercise:
int n; double x; cout << "Enter a nonzero integer: "; cin >> n; while (n = 0) { cout << "0 is not valid - try again: "; cin >> n; } // ASSERT: n is not zero - it is safe to divide x = 1 / n; cout << "The reciprocal of " << n << " is " << x << endl;