8.4. Chapter Review
When do you want to use an array rather than just a bunch of individually named variables?
Before writing a program, must you know the exact size of an array that you are going to create?
Before creating a new array in a program, must the program be able to calculate the proper size for the array?
After you have created the array, can you change the size of the original array object?
If I have the declaration
int[] vals = new int[5];
What is stored directly in the memory position for variable
vals
?Does
vals[3]
then have a clear value? If so, what?Can I later make
vals
refer to an array of a different size?
Comment on the comparison between these two snippets:
char[] a = {'n', 'o', 'w'}; a[0] = 'c'; string s = "now"; s[0] = 'c';
If I want to read or modify the first 100 elements of a 999 element array, would I use a
foreach
loop or afor
loop? Explain.If I want to modify all the elements of an array, would I use a
foreach
loop or afor
loop? Explain.If I want to read all the elements of an array, but not change the array, and I do not care about the exact position in the array of any member, would I use a
foreach
loop or afor
loop?Is this legal?
int[] a= {1, 2, 3, 4}; //... a = new int[7];
The definition of a program’s
Main
method may optionally include a parameter. What is the type? How is it used?What is an alias? Why is understanding aliases important with arrays?
If I have a method declared:
static void f(int num) //...
and I call it from my
Main
method:int v = 7; f(v); Console.WriteLine(v);
Could
f
change the value of the variablev
, so 1 is printed inMain
? If so, write a one-line body forf
that does it.If I have a method declared
static void f(int[] nums) //...
and I call it from my
Main
method:int[] v = {7, 8, 9}; f(v); Console.WriteLine(v[0]);
Could
f
change the value of the variablev[0]
, so 1 is printed inMain
? If so, write a one-line body forf
that does it.What is printed by this snippet?
int[] a = {1, 2, 3}; int[] b = {4, 5, 6}; b[0] = 7; a[1] = 8; b[2] = 9; Console.WriteLine("" + a[0] + a[1] + a[2]);
What is printed by this snippet? (Only the second line is changed.)
int[] a = {1, 2, 3}; int[] b = a; b[0] = 7; a[1] = 8; b[2] = 9; Console.WriteLine("" + a[0] + a[1] + a[2]);
After this line, what is the value of
a[2]
?bool[] a = new bool[5];
This will cause a runtime error. Why?
string[] a = new string[5]; foreach(string s in a) { Console.WriteLine(s.Length); }
What is printed by this program? Play computer first to figure out.
1using System; 2 3class ArrayLoop1 4{ //Play computer on this code and then test 5 public static void Main() 6 { 7 int[] a = {1, 2, 3}, b = {7, 2, 3, 5}, 8 c = {7, 0, 3, 2, 5}; 9 Console.WriteLine(foo(a, b, 2)); 10 Console.WriteLine(foo(c, b, 4)); 11 } 12 13 static int foo(int[] x, int[] y, int n) 14 { 15 int k = 0; 16 for (int i = 0; i < n; i++) 17 if (x[i] == y[i]) { 18 k++; 19 } 20 return k; 21 } 22}
Then you can run example array_loop1/array_loop1.cs to check the results and see our table from playing computer included in the project, array_loop1/play_computer1.txt.
What is printed by this program? Play computer first to figure out. Be careful to keep the data current!
1using System; 2 3class ArrayLoop2 4{ //Play computer on this code first and then test 5 public static void Main() 6 { 7 int[] a = {5, 7, 6, 9, 8}; 8 for (int i = 0; i < 3; i++) { 9 a[i+2] = a[i]; 10 } 11 foreach (int x in a) { 12 Console.Write(x); 13 } 14 Console.WriteLine(); 15 } 16}
Then you can run example array_loop2/array_loop2.cs to check the results and see our table from playing computer included in the project, array_loop2/play_computer2.txt.