{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "40ef54c2",
   "metadata": {},
   "source": [
    "# Chapter Review\n",
    "\n",
    "01. When do you want to use an array rather than just\n",
    "    a bunch of individually named variables?\n",
    "\n",
    "02. Before writing a program, must you know the exact size of an array that\n",
    "    you are going to create?\n",
    "\n",
    "03. Before creating a new array in a program,\n",
    "    must the program be able to calculate the proper size for the array?\n",
    "\n",
    "04. After you have created the array, can you change the size of the original\n",
    "    array object?\n",
    "\n",
    "05. If I have the declaration\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    int[] vals = new int[5];\n"
   ],
   "id": "4596b7d8"
  },
  {
   "cell_type": "markdown",
   "id": "5b0a1087",
   "metadata": {},
   "source": [
    "\n",
    "    1. What is stored directly in the memory position for variable `vals`?\n",
    "    2. Does `vals[3]` then have a clear value? If so, what?\n",
    "    3. Can I later make `vals` refer to an array of a different size?\n",
    "\n",
    "06. Comment on the comparison between these two snippets:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    char[] a = {'n', 'o', 'w'};\n",
    "    a[0] = 'c';\n",
    "\n",
    "    string s = \"now\";\n",
    "    s[0] = 'c';\n"
   ],
   "id": "1a8f0b21"
  },
  {
   "cell_type": "markdown",
   "id": "816513c0",
   "metadata": {},
   "source": [
    "\n",
    "07. If I want to read or modify the first 100 elements of a 999 element\n",
    "    array, would I use a\n",
    "    `foreach` loop or a `for` loop? Explain.\n",
    "\n",
    "08. If I want to modify all the elements of an array, would I use a\n",
    "    `foreach` loop or a `for` loop? Explain.\n",
    "\n",
    "09. If I want to read all the elements of an array, but not change the array,\n",
    "    and I do not care about the exact position in the array of any member,\n",
    "    would I use a `foreach` loop or a `for` loop?\n",
    "\n",
    "10. Is this legal?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    int[] a= {1, 2, 3, 4};\n",
    "    //...\n",
    "    a = new int[7];\n"
   ],
   "id": "11735e77"
  },
  {
   "cell_type": "markdown",
   "id": "edaef704",
   "metadata": {},
   "source": [
    "\n",
    "11. The definition of a program's `Main` method may optionally\n",
    "    include a parameter. What is the type? How is it used?\n",
    "\n",
    "12. What is an alias? Why is understanding aliases important with arrays?\n",
    "\n",
    "13. If I have a method declared:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    static void f(int num)\n",
    "    //...\n"
   ],
   "id": "cefe3b38"
  },
  {
   "cell_type": "markdown",
   "id": "8721ccb6",
   "metadata": {},
   "source": [
    "\n",
    "    and I call it from my `Main` method:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    int v = 7;\n",
    "    f(v);\n",
    "    Console.WriteLine(v);\n"
   ],
   "id": "0d904255"
  },
  {
   "cell_type": "markdown",
   "id": "a7fad1ca",
   "metadata": {},
   "source": [
    "\n",
    "    Could `f` change the value of the variable `v`, so 1 is printed\n",
    "    in `Main`?\n",
    "    If so, write a one-line body for `f` that does it.\n",
    "\n",
    "14. If I have a method declared\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    static void f(int[] nums)\n",
    "    //...\n"
   ],
   "id": "37399f63"
  },
  {
   "cell_type": "markdown",
   "id": "02ba088d",
   "metadata": {},
   "source": [
    "\n",
    "    and I call it from my `Main` method:\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    int[] v = {7, 8, 9};\n",
    "    f(v);\n",
    "    Console.WriteLine(v[0]);\n"
   ],
   "id": "f6c15b37"
  },
  {
   "cell_type": "markdown",
   "id": "b8d75484",
   "metadata": {},
   "source": [
    "\n",
    "    Could `f` change the value of the variable `v[0]`, so 1 is printed\n",
    "    in `Main`?\n",
    "    If so, write a one-line body for `f` that does it.\n",
    "\n",
    "15. What is printed by this snippet?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    int[] a = {1, 2, 3};\n",
    "    int[] b = {4, 5, 6};\n",
    "    b[0] = 7;\n",
    "    a[1] = 8;\n",
    "    b[2] = 9;\n",
    "    Console.WriteLine(\"\" + a[0] + a[1] + a[2]);\n"
   ],
   "id": "00e0328c"
  },
  {
   "cell_type": "markdown",
   "id": "0e606e41",
   "metadata": {},
   "source": [
    "\n",
    "16. What is printed by this snippet? (Only the second line is changed.)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    int[] a = {1, 2, 3};\n",
    "    int[] b = a;\n",
    "    b[0] = 7;\n",
    "    a[1] = 8;\n",
    "    b[2] = 9;\n",
    "    Console.WriteLine(\"\" + a[0] + a[1] + a[2]);\n"
   ],
   "id": "f2d6939b"
  },
  {
   "cell_type": "markdown",
   "id": "3f12e2a5",
   "metadata": {},
   "source": [
    "\n",
    "17. After this line, what is the value of `a[2]`?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    bool[] a = new bool[5];\n"
   ],
   "id": "8f263463"
  },
  {
   "cell_type": "markdown",
   "id": "57edaa9b",
   "metadata": {},
   "source": [
    "\n",
    "18. This will cause a runtime error. Why?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "    string[] a = new string[5];\n",
    "    foreach(string s in a) {\n",
    "       Console.WriteLine(s.Length);\n",
    "    }\n"
   ],
   "id": "7adab41d"
  },
  {
   "cell_type": "markdown",
   "id": "bd4b7262",
   "metadata": {},
   "source": [
    "\n",
    "19. What is printed by this program? Play computer first\n",
    "    to figure out.\n",
    "\n",
    "    ```{literalinclude} ../../examples/array_loop1/array_loop1.cs\n",
    "    :linenos: true\n",
    "    ```\n",
    "\n",
    "    Then you can run example\n",
    "    [array_loop1/array_loop1.cs](https://github.com/mstbit/introcs-csharp-examples/blob/master/array_loop1/array_loop1.cs) to check the results and see our\n",
    "    table from playing computer included in the project,\n",
    "    [array_loop1/play_computer1.txt](https://github.com/mstbit/introcs-csharp-examples/blob/master/array_loop1/play_computer1.txt).\n",
    "\n",
    "20. What is printed by this program? Play computer first\n",
    "    to figure out. Be careful to keep the data current!\n",
    "\n",
    "    ```{literalinclude} ../../examples/array_loop2/array_loop2.cs\n",
    "    :linenos: true\n",
    "    ```\n",
    "\n",
    "    Then you can run example\n",
    "    [array_loop2/array_loop2.cs](https://github.com/mstbit/introcs-csharp-examples/blob/master/array_loop2/array_loop2.cs) to check the results and see our\n",
    "    table from playing computer included in the project,\n",
    "    [array_loop2/play_computer2.txt](https://github.com/mstbit/introcs-csharp-examples/blob/master/array_loop2/play_computer2.txt).\n",
    "\n",
    "% #.   If you get a data sequence from a ``Random`` object,\n",
    "\n",
    "% is it really random?\n",
    "\n",
    "% #.   Explain the significance of a *seed* for a ``Random`` object.\n",
    "\n",
    "% #.   Suppose I create an object ``table`` of type ``double[,]``,\n",
    "\n",
    "% and I think of the first index as referring to a row and the second\n",
    "\n",
    "% index as referring to a column.\n",
    "\n",
    "% a.  Must each row be the same length?\n",
    "\n",
    "% b.  Does each row have a type ``double[]`` ?\n",
    "\n",
    "% #.   (Optional) Suppose I create an object ``table`` of type ``double[][]``,\n",
    "\n",
    "% and I think of the first index as referring to a row and the second\n",
    "\n",
    "% index as referring to a column.\n",
    "\n",
    "% a.  Must each each row be the same length?\n",
    "\n",
    "% b.  Does each row have a type ``double[]`` ?\n",
    "\n",
    "% Follow Array Loop Exercise/Example\n",
    "\n",
    "% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "\n",
    "% #.  What is printed by this program?  Play computer first\n",
    "\n",
    "% to figure out.\n",
    "\n",
    "% .. literalinclude:: ../../examples/array_loop1/array_loop1.cs\n",
    "\n",
    "% :linenos:\n",
    "\n",
    "% Then you can run example\n",
    "\n",
    "% :repsrc:`array_loop1/array_loop1.cs` to check the results and see our\n",
    "\n",
    "% table from playing computer included in the project,\n",
    "\n",
    "% :repsrc:`array_loop1/play_computer1.txt`.\n",
    "\n",
    "% #.  What is printed by this program?  Play computer first\n",
    "\n",
    "% to figure out.  Be careful to keep the data current!\n",
    "\n",
    "% .. literalinclude:: ../../examples/array_loop2/array_loop2.cs\n",
    "\n",
    "% :linenos:\n",
    "\n",
    "% Then you can run example\n",
    "\n",
    "% :repsrc:`array_loop2/array_loop2.cs` to check the results and see our\n",
    "\n",
    "% table from playing computer included in the project,\n",
    "\n",
    "% :repsrc:`array_loop2/play_computer2.txt`.\n",
    "\n",
    "% #.  What is printed by this program?  Play computer first\n",
    "\n",
    "% to figure out.\n",
    "\n",
    "% .. literalinclude:: ../../examples/array_loop3/array_loop3.cs\n",
    "\n",
    "% :linenos:\n",
    "\n",
    "% Then you can run example\n",
    "\n",
    "% :repsrc:`array_loop3/array_loop3.cs` to check the results and see our\n",
    "\n",
    "% table from playing computer included in the project,\n",
    "\n",
    "% :repsrc:`array_loop3/play_computer3.txt`.\n",
    "\n",
    "% .. _sign-array-exercise:\n",
    "\n",
    "% Sign Array Exercise/Example\n",
    "\n",
    "% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "\n",
    "% Complete the code for this function:\n",
    "\n",
    "% .. literalinclude:: ../../examples/sign_array1/sign_array1.cs\n",
    "\n",
    "% :start-after: chunk\n",
    "\n",
    "% :end-before: chunk\n",
    "\n",
    "% :dedent: 3\n",
    "\n",
    "% and place it in a program with a main function that demonstrates it.\n",
    "\n",
    "% You can compare your solution with ours in\n",
    "\n",
    "% :repsrc:`sign_array1/sign_array1.cs`.\n",
    "\n",
    "% #.  If my only use for variable ``temp`` is to set up this call to ``f``::\n",
    "\n",
    "% int[] temp = {1, 2, 3};\n",
    "\n",
    "% f(temp);\n",
    "\n",
    "% how could I rewrite it with an anonymous array?"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "name": "csharp"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
