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();
//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$