2
$\begingroup$

I'll be sitting in for a teacher next week and I'll be teaching about arrays in c#. They've been taught about variables, basic types (int, string, double) and how to print data to the console. They began the class with no programming experience. I tried to build this so it would be interactive and they could test it and see for themselves what the code does. Any spelling corrections or other improvements are welcome just try and keep it simple :D

PS: The keyboard shortcuts are for Visual Studio.

    /*
    * !IMPORTANT! Select areas marked in between # and press "ctrl + k + u"
    * to test the code. After testing select it again and press "ctrl + k + c"!
    */

    /*
    * RULE OF PROGRAMMING! Everything is always in English, comments,
    * variable names etc...
    */

    /*
    * So you know what variables are, a variable has a Type for example:
    * string, int and double there are many others but you dont need to know
    * those yet. Think of a variable as a name given to a storage area 
    * in the computers memory.
    */

    /* 
    * When you want to reserve a storage area in the computers memory,
    * its called "defining".
    * When defining a variable you are only reserving the area for 
    * that name! You are not giving it a value so you cant output it yet.
    */

    //#
    //int i;        // this is how u define a variable
    //#

    //Now to give this variable a value you use "=" sign
    //#
    //int i;
    //i = 1;
    //int j = 2;    // all in one step
    //Console.WriteLine("i: {0} j: {1}", i, j);
    //#

    /*
    * Variables are to store one piece of data in the computers memory. 
    * Arrays are used to store a collection of data.
    * So think of a array as a collection of variables of the SAME!!!! type. 
    * So instead of having variables: 
    * number1, number2, number3, .... number99. 
    * You have a array called numbers.
    */ 

    /* 
    * In arrays we dont call each value a variable tho, we call it a 
    * "element". An array has a FIXED amount of variables or so called elements.
    */ 

    /* 
    * You define a array just like a variable, its type so for example a 
    * int array and give it a name:
    */ 
    //#
    //int[] numbers;
    //#

    /* 
    * Since we want it to be a new array we will tell the computer that, 
    * we also need to tell it how many elements it will have,
    * because remember arrays have a fixed amount of elements they can have. 
    * We do this like so:
    */
    //#
    //int[] numbers;
    //numbers = new int[10];      // 10 being the fixed amound of elements
    //int[] numbers = new int[10];  // all in one step
    //#


    /*
    * So a array is a "list" of elements and as I said a element is like 
    * a variable, however a element has a "index" instead of a name.
    * So to access a certain element we use its index instead of a name. 
    * Indexes are always numbers! and the first index in a array is always 
    * 0!!! never 1!
    * So the indexes of a array that has 10 elements would be 
    * 0,1,2,3,4,5,6,7,8,9.
    * Lets assign a value to the first element and then output its value.
    */

    //#
    //int[] numbers = new int[10];
    //numbers[0] = 1;     //0 is the index and we are assigning the value 1
    //Console.WriteLine("fist element value: {0}", numbers[0]);
    //#

    /*
    * But we have a array with 10 elements so lets add 10 numbers to our
    * array:
    */ 
    //#
    //int[] numbers = new int[10];
    //numbers[0] = 1;
    //numbers[1] = 2;
    //numbers[2] = 3;
    //numbers[3] = 4;
    //numbers[4] = 5;
    //numbers[5] = 6;
    //numbers[6] = 7;
    //numbers[7] = 8;
    //numbers[8] = 9;
    //numbers[9] = 10;
    //#

    /* 
    * It isn't very nice to add values to a array like that because u
    * have lots of lines of code so lets do the same with a for loop:
    */
    //#
    //int[] numbers = new int[10];
    //  /*
    //  * We tell it to start at the first index thats why index = 0, then we
    //  * want it to stop when the index is 9 so we tell it to stop when its
    //  * smaller then the arrays length which is 10, then we tell it to
    //  * increment(add 1) after each loop.
    //  */
    //for (int index = 0; index < numbers.Length; index++)     
    //{
    //    numbers[index] = index + 1;     // index + 1 becuase we want the values 1 - 10
    //}
    //#

    /* 
    * Now that we have a array with 10 values we want to output the contents
    * of that array we could write 10x Console.WriteLine(number[index])
    * after the loop.
    * however we can also use the same loop that we used to add the values 
    * since we know this goes through the entire array.
    */
    //#
    //int[] numbers = new int[10];
    //for (int index = 0; index < numbers.Length; index++)
    //{
    //    numbers[index] = index + 1;
    //    Console.WriteLine(numbers[index]);
    //}
    //#

    /* 
    * But what if we add some values and then want to change a value and then
    * output them, the output in the for loop would just show the old values
    * not the new ones.
    * You can quickly output a array by using the foreach loop.
    */
    //#
    //int[] numbers = new int[10];
    //for (int index = 0; index < numbers.Length; index++)
    //{
    //    numbers[index] = index + 1;
    //    Console.WriteLine(numbers[index]);
    //}
    //numbers[5] = 3;     // Changing the value, so now the value at index 5 is 3 not 6
    //numbers[7] = 16;    // 16 not 8

    //foreach (int number in numbers)     // Since our array contains int values we tell it for each int in the array do something.
    //{
    //    Console.WriteLine(number);
    //}
    //#

    /* 
    * Now you know the basics of arrays, lets look at how to find out at 
    * which index a certain value is saved in the array. For things like
    * this c# has built in functions/methods, most languages will have 
    * built in functions that do a lot of things for you. Lets say we have 
    * a String array of animals and we want to find out where the shark 
    * is saved.
    */
    //#
    //string[] animals = new string[5] { "tiger", "giraffe", "shark", "elephant", "panda" };  // This automatically adds the values to the array when creating it.
    //int indexOfShark = Array.IndexOf(animals, "shark");     // We are giving this function the array to search through and the value we want to find and it will return the index, as a int.
    //Console.WriteLine("shark is saved at index {0}", indexOfShark);
    //#


    /*
    * Some other useful functions are sort and reverse, they do what their 
    * name says they do :)
    */
    //#
    //string[] animals = new string[5] { "tiger", "giraffe", "shark", "elephant", "panda" };
    //Array.Sort(animals);    // Alphabetically for string and smalles to largest for int and double.

    //Console.WriteLine("Sorted:");
    //foreach(String animal in animals)
    //{
    //    Console.WriteLine(animal);
    //}
    //Array.Reverse(animals); // Reversed order.  

    //Console.WriteLine("Reversed:");
    //foreach (String animal in animals)
    //{
    //    Console.WriteLine(animal);
    //}
    //#

    Console.ReadLine();
$\endgroup$
  • 3
    $\begingroup$ I can give a couple of tips: Check your spelling (an array, though) and use string interpolation in the console messages $\endgroup$ – Saul Marquez Feb 13 at 14:47
  • 1
    $\begingroup$ //When you want to reserve a storage area in the computers memory its called "defining" or allocating, or whatever you're actually doing. That statement is misleading. Other comments are quite misleading as well. I won't put this in an answer since I don't think we can tell whether this is a good tutorial without knowing more about your target audience (for example, what do they already know and what age bracket are they in), but the least you could do is for your advice to be correct. $\endgroup$ – Mast Feb 13 at 14:53
  • 2
    $\begingroup$ "Now to give this variable a value you use "=" sign". I'm not an educator then this is just my opinion but I'd try hard to teach the students the right terminology from the very beginning. To build a vocabulary is as important as learning the language syntax. Another opinion: I wouldn't insist on the "...all in one step..." thing, they know they can then I'd try to show only RIIA (which is a very good habit). $\endgroup$ – Adriano Repetti Feb 13 at 15:14
  • $\begingroup$ @AdrianoRepetti would "now to assign a value to this variable" be better. What do you mean with RIIA? $\endgroup$ – Lucifer Uchiha Feb 13 at 15:20
  • $\begingroup$ @Mast They are complete beinngers aged 15/16. Which advice is incorrect if I may ask? $\endgroup$ – Lucifer Uchiha Feb 13 at 15:23
3
$\begingroup$

Treat your pupils with all the respect they deserve.

They deserve teaching material that is free from obvious spelling and grammar mistakes. To reach this, paste your code into Microsoft Word or another spell checker and go through the whole document carefully. As of now, there are at least 30 issues in your document, which is far too easy for the pupils to spot. After that, hand the material to a human proof reader, for getting the final polishing.

They deserve to be treated as humans. Saying u to them doesn't accomplish that. It just leaves a sloppy impression on you as the teacher.

I support Buffy's approach of preventing them from confusing any of the concepts by not starting with an int[] but with a string[].

An array is fundamentally different from the data types they know by now (int, string), because assigning arrays doesn't copy its value, it copies just the reference. Be prepared to answer questions regarding this difference.

The C-style for loop for initializing an array is very hard to grasp. Therefore I'd rather start with the animals array and using an array initializer. I also like the simple foreach loop for writing an array because it doesn't have much syntactic overhead.

Only after these introductions would I start with the for (int i = 0 loop. This is already fairly advanced stuff.

I like your approach of introducing the Array.IndexOf function before teaching the pupils to implement it themselves. It's complicated enough to call a function. Too bad that C# doesn't provide an Array.Shuffle function. I think that function would give the pupils plenty of things to do.

Maybe you can make them enter a string, convert the letters of the string into an array and then sort that array. This gives them anagrams, and they can show them to each other to guess what the original words were. The code for that is pretty simple and I think they'll understand it:

Console.Write("Enter a word: ");
string word = Console.ReadLine();

char[] chars = word.ToCharArray();
Array.Sort(chars);

Console.WriteLine(chars);

Console.ReadLine();
$\endgroup$
  • 3
    $\begingroup$ Welcome to Computer Science Educators! I daresay, we'd welcome some input from the codereview folk, who often have a different (but very valuable) perspective! I hope it feels nice to know that you're impacting how information will be imparted by teachers to the next generation of developers :) Feel free to take a look around, I'm sure that you will have a lot to add. $\endgroup$ – Ben I. Feb 13 at 20:21
1
$\begingroup$

There are a couple of things I would do before presenting this. The goal of the suggested change is to prevent students from getting the idea that things are the same when they are only incidentally similar. First, I'd do the animals array first so that the idea of an index is separate completely from the values stored in the array. The one is an integer and the other is a string.

Next, I wouldn't use the number array as you have done it, but (a) give it a different name that might be more "intention revealing in a real problem" and (b) use a more complex formula than just index + 1. For example, something like powers[index] = 2 ^ index; (Which is Math.pow(2, index), of course).

But anyway something with a more complex relationship to the index. The reasoning is that the similarity can be misleading as to the intent. Doing animals first helps with this, of course.

When a construct has a lot of parts, it can help beginners if the parts are as different from each other as possible so that their natural human classification system doesn't merge concepts that should be separate.

$\endgroup$
0
$\begingroup$

Have you or the teacher that you're filling in for explained the difference between primitive data types and reference data types? If these are to be future developers, they should understand how things are stored on the stack and on the heap. Since this is probably their first non-primitive data type, this is probably the time to make the explanation. It will become important as you get into objects. It can lead into an explanation of when setting x = y and chancing x also changes the value of y when both x and y are reference types and how that differs when x and y are primitive types.

I find it helpful to draw the stack space and the heap space on the whiteboard and when you're going through code, you can modify the stack and heap accordingly so that they get a good understanding of what is going on.

Also, if what you entered here is for distribution you might consider checking the grammar and spelling. I find it very distracting when I read something that isn't written as well as it could be. For example:

In arrays we dont call each value a variable tho, we call it a * "element".

Fix "don't", "though", and "an" element

There are others and I'm not picking on you, so I just suggest you read through it again and fix any similar issues. If you need help proofreading it, let me know, and I'll provide more examples.

Another idea is to explain how the string type works and what immutable means. I notice that you're explaining some of the array methods. That is great. But once you start talking about the methods associated with strings the concept of immutability becomes important.

string x="My String";
Console.WriteLine(x);
Console.WriteLine(x.ToLower());
Console.WriteLine(x);

Because a string is immutable, what is written to the console is the following:

My String
my string
My String

Good Luck with your class.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.