Sunday 22 September 2013

More on Variables and introducing Arrays

In this post we are gonna see one more Variable type. And then I will go on to introduce you to arrays.

Boolean

A Boolean variable can have only two values. true or false. It is very useful when we are making yes or no decisions. This will happen in a lot of programs that we will soon start to write. The following code shows you how to declare the Boolean variable and initialize it to some value. Note that the case of the keywords true and false must be the same.



Arrays

This is the first method for data aggregation. Using arrays we store same type of variables together. Suppose you want to store 50 values which are the marks of all the students in your class. You can of course go ahead and declare 50 variables, hopefully you will not run out of variable names! So, we can do that in a simpler way by doing:



This declares 50 float variables. And then you can store your values in them. Moreover, apart from the fact that declaring arrays is simple, you can also work with data in an array without much pain. You just use a loop(we will learn about  it soon!) to cycle through all the values. And thus you can easily find the sum, average, the students who have failed, grades of all the students with minimal code.

The following is the syntax to declare an array:



You can declare an array of any data type.

You have seen that to declare an array you use the square brackets. In case you want to access individual elements of the array then you use the square brackets again but you put the index inside the brackets.

I know you must be asking what the hell is the index! Well index is the identity of each element of an array. In C, Arrays are indexed starting from 0. Another weird thing about C! But what this means is that the marks of the first student can be retrieved by writing marks[0]. Now if you want the marks of the 19th student of the class then you simply do marks[18].

So, in case you want one particular item of the array then you put the index of that item in side the square brackets, which are preceded by the name of the array itself.

NOTE: In C, If you declare an array of size 50 and then access the 60th element as arr[59], then the compiler will not throw an error at you. Simply because while declaring the array the compiler does not store the size of array. So you should always be careful, and make sure that you never access an element whose index exceeds the size of the array.

Character Arrays

They are also called Strings. And strings are collections of characters. Just like normal arrays. But what is different is that strings are things like your name, the name of a place and so on. Thus, Strings refer to any collection of characters. They are represented by putting them in double quotes as so:


Note that a string is always terminated by a character known as the null character and shown as the '\0', pronounced "backslash zero" character. This character indicates that a string has ended. So in the above string, after the last "n" of my name, the compiler will automatically add a '\0' character. Thus if you declare a string of size 50 then you can store only 49 characters in them as the last character will be taken up by the '\0'.

One more thing to note about arrays is that the maximum size of an array is the number that you put inside the square brackets when you declare it. You may or may not use the whole of the array. You can use only a part of it but always bear in mind that the whole array takes up memory space.

You can define a string and then accept it from the user and so on. We will be seeing the code for doing that very soon.

In this post, we talked about the boolean type and got started on the concept of data aggregation via arrays.

No comments:

Post a Comment