10.6. Lab: Arrays

10.6.1. Overview

In this lab, we’ll learn how to work 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 the concept. But what if we want to have arrays that can hold different kinds of data, for example, integer or floating point data? That’s why we need this lab.

10.6.2. Goals

In this lab, we’re going to learn:

  • how to create arrays that hold numerical data.
  • how to populate an array with data
  • how to use the tools of loops and decisions to do something interesting with the data
  • how to print the data

10.6.3. Tasks

Start from the stub in the projects, Labs/Array1D/ArrayLab.cs. It si set up as a MonoDevelop Project, though you may use a different editor and use command line tools if you like. 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. We’re going to use the second line for this lab.

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

  2. 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}. */
          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.

  3. 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.  */
          static int Minimum(int[] a)
          {
             return 0; // so stub compiles
          }
    
  4. 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
          }
    
  5. 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.

  6. 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, int[] sum)
          {
             return new int[0]; // so stub compiles
          }
    

    See how this is different from the previous part!

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

  8. 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)
          {
    
          }
    
  9. 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)
          {
    
          }
    
  10. 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.0, 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 print 1.0 * 4.0 + 2.0 * 2.0 + 3.0 * -1.0 = 5.0
    

    From here on, create your own headings.

  11. Suppose we have loaded an array with the digits of an integer, where the highest power 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 };
    

    Without showing you the code, here is how you would convert a number from its digits to an integer:

    num = 0
    num = 10 * 0 + 1 = 1
    num = 10 * 10 + 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.

  12. Suppose that we not only have the digits but also the base that in which the number is represented. (The base can be at most 10 if it uses only digits for place value.) 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, 19 is returned.

Table Of Contents

Previous topic

10.5. Binary Searching

Next topic

10.7. Lab: Performance

This Page