Hey there, I'm Rowan. I'm currently studying Software Engineering at UTM, and I'm also an IT Entrepreneur. This is my programming blog - you'll find loads of C# stuff in here, as well as MySQL and C tutorials with source. Comments are most welcome.
I too, am a bug within Māyā.
My Other Blog(s) and Site(s)
Friend sites and blogs
My Recent Posts
My C-Sharp Ideas
Monday, October 03, 2005
Arrays in C# and C
[Static Arrays in C and C#]
I never pictured myself writing a tutorial on arrays but here we are - a tutorial on arrays. I'm basically writing this because we just did arrays at uni (C programming language, duh), and a few of my friends came sort of late (and some of them went to the outing) and couldn't understand fuck at all on what the whole deal was about with arrays.
Arrays. I read about arrays when I was 12. It was a Q-Basic tutorial. I still remember, it was saying something like "picture arrays like boxes" ... well anyway, never mind the way you're picturing arrays, let me just start with a small intro on data and arrays.
Please keep in mind that this is a C# blog; the C language and its construct is outside the scope of this blog. However, because I own and maintain this thing, I do pretty much whatever I want with it. You will notice that arrays in C and C# are somewhat similar. Actually, the concept of Arrays remains the same throughout all programming languages. Except ASM and VB. ASM coz you have to write your own shit and deal with it, and VB coz you prolly need to write a poem for the compiler to give you an array. No, well, it's the same in VB too :)
Useful programs need to be fed data to be processed. At some point or the other, it might happen that we need a large amount of the same type of data to hold stuff. Example, if you wanted to hold the salary of 10 employees - you could do something like:
C and C# Code
But that's not really neat-looking. I mean, what if you had 10,000 employees? Heh, that would sort of suck, wouldn't it? Arrays provide a simpler way of storing large amounts of data. You can declare, in one line:
Declaring an array of of length 9 (int in C# and variableName in C) would be the same as declaring 10 integer elements. Why does an array with accessor  have 10 elements? Simple - because an array's elements start with 0.
C and C# Code
Assigning Values To Arrays
Assigning a value to an array is pretty simple. We can do this:
There is, however, an easier way of assigning values in an array. Here goes the code:
Arrays just make the world easier for us. However, behaviour of arrays on C and other programming languages are sort of different. While most modern languages have bounds checking for arrays, C doesn't. For those who have no clue what bounds checking is about - it's just checking whether someone is trying to access an array's elements outside of its specified bounds. You can access the 5th element in an array of 50 items, but you can't access the 1000th element in that same array. In C#, if you try such a thing, an exception will be thrown. In C, nothing will happen - your program will continue to execute, although you'll now have weird garbage data which was used. Example:
While the above C example will run without bitchin' you, C# will give you a System.IndexOutOfRangeException - which is a kind way of saying you fucked up on the array's bounds. C# does check if your code is straying outside the bouds of an array, while C doesn't. It just applies a formula which calculates the memory location of the designated element in the array, reads the memory location, and returns you the result. Voila. You'll have to deal with it by writing proper code, that's all. While dealing with arrays, if you ever end up with garbage data (e.g. an element of array of int returning you -244324 instead of a plain 0), you'll know that you fucked up with the bounds somewhere in your code.
Here's the deal: You can write programs fast in C#, but your programs will consume more processing power, and might eat up more memory space. In C, writing programs is like anal sex, but the programs will run like Beep-Beep with its ass on fire.
Anyway, let's get on with arrays. You can create an array of any type. An array of characters, an array of integers, an array of float - whatever you can think of. The arrays we're discussing here are called "static" arrays. Static arrays have a fixed size (and therefore number of elements). You can't increase the size of the array, nor decrease it. There are several other kinds of arrays in C#, namely dynamic arrays (ArrayLists - hehe) and jagged arrays, but those are outside of the scope of this tutorial.
Multi Dimensional Arrays
What we're going to do now is look at multidimensional arrays. Never mind about the name - Multi Dimensional arrays are just arrays made up of arrays. Let's imagine for a moment that there is a shop which sells Shoes, Hats, and Gloves. We want to represent the number of items sold per day in a grid, as follows:
To store that data, we could:
Shoes would represent the number of shoes sold on Monday, Shoes the number of shoes sold on Tuesday, Gloves the number of gloves sold on Saturday; while on the alternative example, Mon would represent the number of Shoes sold on monday, Mon the number of Hats sold on Monday, Thu the number of Gloves sold on Thursday, and so on. That's not a bad way of doing things. Nowever, if the shop was selling more than 500 items, it would get a little bit difficult to make this work with our current method. Not that it's impossible - but it's not the right way of doing things. This is where multidimensional arrays come into play.
The declaration ItemsSold in C and int in C# is the one of a multidimensional array. Those kinds of arrays allow the user to create arrays of 2, 3, 4 or even N dimensions, according to the needs of the program. Here's a description of how the data is going to be stored in my array:
Well, yes, we could have reversed it all. We could have made a 2-dimensional array which has its first dimension representing the day, and the second dimension representing the item. The code would look like this:
The arrys would represent:
Assignment of dimensions is up to you - whether you want to put the days in the first dimension or in the second all depends on you and what you want to code.
Finally, when should we use static arrays, and when should we use dynamic arrays? Static arrays should be used when you're ABSOLUTELY certain that the size of the array will never exceed a certain limit. Example - number of days in a week, number of days in a month (even if it's sometimes 28,29,30 or 31, declaring an array with 31 elements is enough. We might be wasting 4-16 bytes, but it's still ok).
However, we SHOULD use dynamic arrays when we're not certain about the number of elements that is going to fit into the array. E.g. Number of employees, number of files in a directory, number of items being sold by a shop(yes, that can vary).
Dynamic arrays contain additional code to make the whole thing work - which makes it slower. Static arrays are pretty straightforward when it comes to execution - not much code, executes fast.
I hope you enjoyed reading the tutorial :)
I would like to express my warmest thanks to:
I'm dedicating this tutorial to my uni friends who are completely clueless about arrays; and to my one and only frequent reader (yeah, I've got only one fucking "frequent" reader. Damn. Thought I was popular.) Ahad. Yo dude! What's up? :P