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
whileloops 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
introcscsdirectory (C:\Users\*USERNAME*\workspace\introcscsfor Windows orCOMPUTER:introcscs USERNAME$for macOS) ; call it Ch08ArraysLab.Inside the folder, issue the command
dotnet new consoleto 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:
This will be handy for labeling later tests. Note that you end on the same line, but a later label can start with
\nto advance to the next line.Complete and test the method with documentation and heading:
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:
Complete and test the method with documentation and heading:
Complete and test the method with documentation and heading:
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:
See how this is different from the previous part!
Complete and test the method with documentation and heading:
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:
Complete and test the method with documentation and heading:
Given two arrays,
aandbthat 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.