- "Unix" Commands
- 2-D Indices
- Coding 2-D Arrays
- Passing 2-D Arrays to Functions
- Compiling
Exploring "Unix" Commands
Cal
- At the prompt, type
cal
What do you think it is short for?
- Try:
cal 2010
What does it display?
- What about:
cal 2 2010
- Use the
man calto find out how to display three months
Cat
- Find a directory with more than one file. Try
cat file1 file2
(wherefile1andfile2are your own files). What does it do?
- Can you use
catto print line numbers of a file?
Date
- Try the
datecommand. What do you notice?
- Try
date --helpto figure out how to print only the hour and minute.
Who and W
- To see what these commands do, it is best if everyone logs onto the same machine:
ssh a037102
- Try the
whoandwcommand. What do they do? What is the difference?
Summary of Unix/Linux Commands
Coding 2-D Arrays
/**************************************************** * * FileName: ~ftp/pub/class/170/ftp/cpp/2Darray.cpp * Author: Ada Lovelace * Purpose: * Demonstrate manipulation of a 2D array. * ********************************************************/ #include <iostream> using namespace std; const int R_SIZE=3; const int C_SIZE=5; int main() { int Nums[R_SIZE][C_SIZE]; int i, j; for ( i=0; i < R_SIZE; i++) // march down the rows { for ( j=0; j< C_SIZE; j++) // march across the columns { Nums[i][j] = 0; // set array element to zero } } }
Explore:
- Compile and run this code. What does it do?
- Add code to print the array so that it appears as:
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
- Change the 2D array to a 2 x 4 array. Can you see the advantage of using constants?
- Initialize the array to values:
1 2 3 4
5 6 7 8
Declaring and Initializing in One Step
Note the following code for initializing a 2 x 3 array.
int Nums[2][3] =
{
{1, 2, 3},
{4, 5, 6}
};
You can modify the above code to make it work for your 2 x 4 array.
Passing 2-D Arrays to Functions
#include <iostream> using namespace std; const int R_SIZE=2; const int C_SIZE=3; void InitArray( int [][C_SIZE] ); // function prototype int main() { int Nums1[R_SIZE][C_SIZE]; InitArray(Nums1); } // Function: InitArray // Purpose: To initialize an array. // Parameters: Base address of an array. // Returns: void // -------------------------------------------------------- void InitArray(int p_Array[][C_SIZE]) //function declaration { int i, j; for ( i=0; i < R_SIZE; i++) { for ( j=0; j< C_SIZE; j++) { p_Array[i][j] = 0; } } } // end InitArray function
Explore:
- Are arrays passed by value or by reference?
- Can you include the row size in the function prototype and the function declaration?
- Can you leave out both the row size and the column size in the function prototype and function declaration? (keep the square brackets though)
Note: arrays are not a legal return type. If you want, you can try it out.
This is because arrays are stored in memory in one row after another. The function needs to know how many columns there are in a 2D array so it can find the next row. It's as if 2D arrays were stored as a big 1D array. In a 2D array cells referenced like this:
int array[R_SIZE][C_SIZE];
array[3][2];
are actually being referenced like this:
int array[R_SIZE*C_SIZE];
array[3*C_SIZE + 2];
but the compiler doesn't know what value to multiply by unless you supply it in the function definition.
Compiling Multiple Files
You might want to perform these in your CS115 directory. You can use the ls command after step 1, 2, and 3 to see what files are being added to your directory.
- First, get the three files to be compiled:
-
cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/main.cpp . cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.cpp .cp /net/data/ftp/pub/class/170/ftp/cpp/SeparateCompile/myFunction.h .
- Then, compile the two
.cppfiles:g++ -c main.cppg++ -c myFunction.cpp
- What two files are created? These are refered to as object files and contain the machine code
- Now, the two object files need to be "linked" or combined together into the "executable" (in other words, the file that will be run)
g++ main.o myFunction.o -o main
- You can now run the code. What will you type?

Compiling a Single File
You might want to perform these in your CS115 directory. You can use the ls command after step 1 and 2 to see what files are being added to your directory.
- First, get the file to be compiled:
cp /net/data/ftp/pub/class/115/ftp/cpp/hello.cpp hello.cpp- Then, compile:
g++ -o hello hello.cpp
- You can now run the code:
./hello
Explore:
- Try playing around with the order of the arguments after the
g++in Step 2- If something happens to your hello.cpp code, don't worry--you can always get it again (from Step 1)
- If something happens to your hello.cpp code, don't worry--you can always get it again (from Step 1)
- Try leaving out the
-o helloin Step 2. What is produced? Can you use that to run the code?
- Try replacing
hellowithtest. Try to runtestwith or without the./in front. Does hello work that way too? If you are curious about why, tryman test
Notes:
- On Hercules, the command to compile is: CC (instead of g++)
- Programs compiled on Hercules won't run on Linux and vice versa
./2Darrayg++ 2Darray.cpp -o 2Darray