First Arrays Manipulations

https://csci-1301.github.io/about#authors

August 13, 2021 (08:00:08 AM)

First Array Manipulation

Warm-up

Write a program that implements the following steps:

  1. declares an array myArray of int of size 5,

    Question
    What values are stored in this array after declaring it?
  2. initializes myArray with the values 1, 2, 3, 4 and 5,

    Question
    There are a few different ways you can declare and initialize array of size 5 holding values 1, 2, 3, 4 and 5. Can you think of two different ways of doing this?
  3. displays the content of myArray at the screen.

Going wrong

Now, let us write incorrect statements.

Add the following statement to the program you created in the warm-up part, and observe how C# reacts, that is, try to compile and execute the program after adding this line:

myArray = { 1, 2 ,3, 4, 5};

Remove the previous line. Then add this statement in its place:

Console.WriteLine(myArray[5]);

try to compile and execute the program.

Then, remove previous line, and now add this:

myArray[5] = 12;

try to compile and execute the program again.

Remove previous line. Add this line and execute the program:

Console.WriteLine(myArray);

Now answer the following questions.

  1. One of these statements is not “incorrect” in the sense that it won’t prevent your program from executing, but it is not doing what you would have expected: which one?
  2. Can you read and understand the error messages you obtained for the others?

Second Array Manipulation

Write a program that

  1. declares an array myArray of int of size 10,
  2. initializes myArray with the values 1, 2, 3, …, 9 and 10,
  3. displays the content of myArray.
  4. sums the values stored in myArray and displays the result.
  5. computes the product of the values stored in myArray and displays the result.

Exploring arrays

For this part, create a new array:

Now, write the following statements:

  1. Write a statement to display the last char value in letters (should display f).
  2. Write a statement to display value stored at index 4. What is that value? Why?
  3. Write a statement to display characters in the first half of the array ('a', 'b', 'c' but no others).

Execute your program to ensure you are seeing the expected output before proceeding.

Next, update the part of the program where letters is declared and change letters length to 8. Do not modify any other parts of the program. Then execute the program again.

Answer the following questions:

  1. What is the last char of the letters array now, after changing its length?
  2. Does your program still output the last char value in letters array?
  3. When displaying the first half of the array, does your program still display the first half? (After changing length the first half contains values 'a', 'b', 'c', 'd')
  4. If you did not get the last value or the first half you should have, can you think of a way to perform these array operations in a way that can accomodate arrays of different lengths?