8.3. Lab: Arrays
Notes on GAI: Note that the course policy is that you should not use generative AI (GAI) without authorization. GAI’s are great tools and you should learn how to use it, but they are tools and should be used to facilitate but not replace your learning. If you are suspected to have used GAI tools to generate answers to the assignment questions instead of using it as a learning tool, you may be called up to explain/reproduce your work. If you fail to demonstrate your competency, all your related assignments throughout the semester will be regraded as 0. For example, if you fail to produce good code in
while
loops in midterm exam, your lab06 while loop homework and labs will be re-evaluated.
Create a dotnet console app project (see Create a C# Project if you need to) in your
introcscs
directory (C:\Users\*USERNAME*\workspace\introcscs
for Windows orCOMPUTER:introcscs USERNAME$
for macOS) ; call it Ch08ArraysLab.Inside the folder, issue the command
dotnet new console
to create the project in the folder.Still inside the project directory, type
code .
to start VS Code with the folder as the default folder.Prepare your code in VS Code using the file Program.cs to code unless otherwise specified.
The namespace of this project is IntroCSCS.
The class name of this project is Ch08ArraysLab.
When executing code, you will only use the Main() method in class Ch08ArraysLab.
You will prepare methods in the same class to be called from the Main() method.
Use a Word document to prepare your assignment.
Number the questions and annotate your answers (using // when appropriate) to show your understanding.
For coding questions, screenshot and paste 1) your code in VS Code and 2) the results of the code’s execution (command prompt and username are part of the execution).
Overview
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 positioni
.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.
Tasks
Go to the example file array_lab_stub/array_lab.cs and download the file
(Download raw file
) and move it to the project folder. Comment out the Main method
in the default Program.cs
and use the array_lab
file for your tasks.
Complete the body of a method
for each main part, and call each method in Main
several times with
actual parameters chosen to test it well. To label your illustrations, make
liberal use of the first method, PrintNums
, to display and label inputs
and outputs. Where several tests are appropriate for the same method,
you might want to write a separate testing method that prints
and labels inputs, passes the data on to the method 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 method 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 method 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 methods are included at the end of the class.
Complete and test the method 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 method 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 method 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 method 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 method 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?
Complete and test the method 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) { }
Complete and test the method 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) { }
Given two arrays,
a
andb
that represent vectors. Write a method that computes the vector dot product of these two floating point arrays. The vector dot product (in mathematics) is defined as the sum ofa[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.