### Overview of Lab

After this lab, you should be able to:

• identify the indices for a value in a 2-D array
• declare and initialize a 2-D array
• pass 2-D arrays to functions

### Exercise

• Links will be here

• "Unix" Commands
• 2-D Indices
• Coding 2-D Arrays
• Passing 2-D Arrays to Functions
• Compiling

## Exploring "Unix" Commands

### Cal

1. At the prompt, type `cal`
What do you think it is short for?

2. Try: `cal 2010`
What does it display?

3. What about: `cal 2 2010`

4. Use the `man cal` to find out how to display three months

### Cat

1. Find a directory with more than one file. Try `cat file1 file2`
(where `file1` and `file2` are your own files). What does it do?

2. Can you use `cat` to print line numbers of a file?

### Date

1. Try the `date` command. What do you notice?

2. Try `date --help` to figure out how to print only the hour and minute.

### Who and W

1. To see what these commands do, it is best if everyone logs onto the same machine:
`ssh a037102`

2. Try the `who` and `w` command. What do they do? What is the difference?

## 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:

1. Compile and run this code. What does it do?

2. 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

3. Change the 2D array to a 2 x 4 array. Can you see the advantage of using constants?

4. 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:

1. Are arrays passed by value or by reference?

2. Can you include the row size in the function prototype and the function declaration?

3. 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.

Why do you only need the column size?

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.

Multiple Files

## 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.

1. 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 .`

2. Then, compile the two `.cpp` files:
• `g++ -c main.cpp`
• `g++ -c myFunction.cpp`

• What two files are created? These are refered to as object files and contain the machine code

3. 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`

4. You can now run the code. What will you type?
Single Files

## 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.

1. First, get the file to be compiled:
• `cp /net/data/ftp/pub/class/115/ftp/cpp/hello.cpp hello.cpp`
2. Then, compile:
• `g++ -o hello hello.cpp`
3. 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)

• Try leaving out the `-o hello` in Step 2. What is produced? Can you use that to run the code?

• Try replacing `hello` with `test`. Try to run `test` with or without the `./` in front. Does hello work that way too? If you are curious about why, try `man 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
What comes after the -o?
An address of the variable (array) is passed. The original values (of the array) are modified in the function.
A copy of the variable (array) is made. The original (array) values will remain unchanged.
`./2Darray`
`g++ 2Darray.cpp -o 2Darray`
[3][1]
[1][0]
[1][0]
[0][2]