In this lab, we’ll practice working with arrays. Arrays are
fundamental to computer science, especially when it comes to
formulating various *algorithms*. We’ve already learned a bit about
arrays through the `string`

data type. In many ways, a character
string reveals the secrets of arrays:

- each element of a string is a common type (char)
- we can use indexing to find any given character, e.g.
`s[i]`

gives us the character at position`i`

. - we know that the string has a finite length, e.g.
`s.Length`

.

So you’ve already learned these *concepts*. But practice is useful
creating and manipulating arrays with different kinds of data.

In this lab, we’re going to practice:

- creating arrays that hold numerical data
- populating an array with data
- using the tools of loops and decisions to do something interesting with the data
- printing the data

Copy the example file array_lab_stub/array_lab.cs to
a new project of yours.
Complete the body of a function
for each main part, and call each function in `Main`

several times with
actual parameters chosen to test it well. To label your illustrations, make
liberal use of the first function, `PrintNums`

, to display and label inputs
and outputs. Where several tests are appropriate for the same function,
you might want to write a separate testing function that prints
and labels inputs, passes the data on to the function being tested,
and prints results.

Recall that you can declare an array in two ways:

```
int[] myArray1 = new int[10];
int[] myArray2 = { 7, 7, 3, 5, 5, 5, 1, 2, 1, 2 };
```

The first declaration creates an array initialized to all zeroes. The second creates an array where the elements are taken from the bracketed list of values. The second will be convenient to set up tests for this lab.

Complete and test the function with documentation and heading:

/// Print label and then each element preceeded by a space, /// all across one line. Example: /// If a contains {3, -1, 5} and label is "Nums:", /// print: Nums: 3 -1 5 static void PrintInts(string label, int[] a) { }

This will be handy for labeling later tests. Note that you end on the same line, but a later label can start with

`\n`

to advance to the next line.Complete and test the function with documentation and heading:

/// Prompt the user to enter n integers, and /// return an array containing them. /// Example: ReadInts("Enter values", 3) could generate the /// Console sequence: /// Enter values (3) /// 1: 5 /// 2: 7 /// 3: -1 /// and the function would return an array containing {5, 7, -1}. /// Note the format of the prompts for individual elements. static int[] ReadInts(string prompt, int n) { return new int[0]; // so stub compiles }

This will allow user tests. The earlier input utility functions are included at the end of the class.

Complete and test the function with documentation and heading:

/// Return the minimum value in a. /// Example: If a contains {5, 7, 4, 9}, return 4. /// Assume a contains at least one value. static int Minimum(int[] a) { return 0; // so stub compiles }

Complete and test the function with documentation and heading:

/// Return the number of even values in a. /// Example: If a contains {-4, 7, 6, 12, 9}, return 3. static int CountEven(int[] a) { return 0; // so stub compiles }

Complete and test the function with documentation and heading:

/// Add corresponding elements of a and b and place them in sum. /// Assume all arrays have the same Length. /// Example: If a contains {2, 4, 6} and b contains {7, -1, 8} /// then at the end sum should contain {9, 3, 14}. static void PairwiseAdd(int[] a, int[] b, int[] sum) { }

To test this out, you’ll need to declare and initialize the arrays to be added. You’ll

*also*need to declare a third array to hold the results. Make sure that the arrays all have the same dimensionality before proceeding.This section is a warm-up for the next one. It is not required if you do the next one:

Complete and test the function with documentation and heading:

/// Return a new array whose elements are the sums of the /// corresponding elements of a and b. /// Assume a and b have the same Length. /// Example: If a contains {2, 4, 6} and b contains {3, -1, 5} /// then return an array containing {5, 3, 11}. static int[] NewPairwiseAdd(int[] a, int[] b) { return new int[0]; // so stub compiles }

See how this is different from the previous part!

Complete and test the function with documentation and heading:

/// Return true if the numbers are sorted in increasing order, /// so that in each pair of consecutive entries, /// the second is always at least as large as the first. /// Return false otherwise. Assume an array with fewer than /// two elements is ascending. /// Examples: If a contains {2, 5, 5, 8}, return true; /// if a contains {2, 5, 3, 8}, return false. static bool IsAscending(int[] a) { return false; // so stub compiles }

This has some pitfalls. You will need more tests that the ones in the documentation! You can code this with a “short-circuit” loop. What do you need to find to be immediately sure you know the answer?

**20 % extra credit:**Complete and test the function with documentation and heading:/// Print an ascending sequence from the elements /// of a, starting with the first element and printing /// the next number after the previous number /// that is at least as large as the previous one printed. /// Example: If a contains {5, 2, 8, 4, 8, 11, 6, 7, 10}, /// print: 5 8 8 11 static void PrintAscendingValues(int[] a) { }

**20 % extra credit:**Complete and test the function with documentation and heading:/// Prints each ascending run in a, one run per line. /// Example: If a contains {2, 5, 8, 3, 9, 9, 8}, print /// 2 5 8 /// 3 9 9 /// 8 static void PrintRuns(int[] a) { }

**20 % extra credit:**Given two arrays,`a`

and`b`

that represent vectors. Write a function that computes the vector dot product of these two floating point arrays. The vector dot product (in mathematics) is defined as the sum of`a[i] * b[i]`

(for all i). Here’s an example of how it should work:double[] a = new double[] { 1.5, 2.0, 3.0 }; double[] b = new double[] { 4.0, 2.0, -1.0 }; double dotProduct = VectorDotProduct(a, b); Console.WriteLine("The dot product is {0}", dotProduct); // Should calculate 1.5 * 4.0 + 2.0 * 2.0 + 3.0 * -1.0 = 7.0

From here on, create your own headings.

**20 % extra credit:**Suppose we have loaded an array with the digits of an integer, where the digit for the highest power of 10 is kept in position 0, next highest in position 1, and so on. The ones position is always at position array.Length - 1:int[] digits = { 1, 9, 6, 7 };

representing \(1(10^3)+9(10^2)+6(10^1)+7(10^0)\).

Without showing you the code, here is how you would convert a number from its digits to an integer efficiently, without calculating high powers for 10 separately:

num = 0 num = 10 * 0 + 1 = 1 num = 10 * 1 + 9 = 19 num = 10 * 19 + 6 = 196 num = 10 * 196 + 7 = 1967 done!

Write a function that converts the array of digits representing a base 10 number to its

`int`

value (or for really long integers, you are encouraged to use a`long`

data type). Note that we only allow single digit numbers to be placed in the array, so negative numbers are not addressed.**20 % extra credit:**Each digit represents a multiple of a*power*of the*base*. In the previous version the base is 10, but other bases are important. Now make the base a parameter. Here we consider bases no bigger than 10, so we can continue to use only digits for place value symbols. Write a function (or revise the previous solution) to return the int or long represented. For example if {1, 0, 0, 1, 1} represents a base 2 number, \(1(2^4)+0(2^3)+0(2^2)+1(2^1)+1(2^0)=19\) is returned. Base 2 is central to computer hardware.