Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

# Learning About 1 Dimensional Arrays (1 D Arrays)

You may have heard of arrays or "tuples" before, if you've learnt any programming language. If not, then don't worry, you'll learn everything about them in this tutorial and how are they used in C to handle large amounts of data of same data type effectively and easily. So, let's know about them. In this tutorial we'll learn about 1 Dimensional Arrays and in later tutorials we'll look at 2 Dimensional Arrays and Multidimensional arrays.

## What is an array?

An array can be defined as a way to store list of large no. of items of same data type in contiguous memory location. Whenever we need to store some information about the same data type, we use arrays. For instance, consider a case when you need to store roll numbers of all students in the class. One way to do this is, I can define separately variables for all roll numbers like:

```...

int rollno1 = 1;
int rollno2 = 2;
int rollno3 = 3;
int rollno4 = 4;
.
.
.
.
.
.
.
.
.
int rollnon = n; //Here n is the last roll number
```

Surely, this would be an inefficient approach, and thus, a better way to do this is would be through arrays like this:

```int rollno[n]; //Here n is the total number of students for which roll numbers are to be stored.
```

I know, you haven't been introduced to "How to declare arrays", which we would see in just a moment, but I just wanted to show how systematically you can use array to store and manage data efficiently. Below is an image describing how array is stored in the memory location. Note here, that each item can be accessed via index number (explained in detail later), and remember, this index value as shown would always start from 0. Many errors, newbie programmers fall trap to, is because they confuse the first index of the array to be 1, which is not the case, as the first index is always 0.

The contiguous memory means, the data cells of memory would be one after the other, so if we need to store 60 items, we need a location in memory with at least 60 cells one after the other. Consider contiguous as being "continuous".

## How to declare a 1 Dimensional array?

Now let us see how you can declare an array :

```<data type> <array name>[<size of array>];
```

Here data type is the actually the type of data of the elements you would store in your array, array name is simply the name you give to your array (similar to the name of a variable, in order to access it later), which would follow the general rules of naming a variable which we have discussed previously. After the name of the array, we would have a set of square brackets in which we would mention the size of the array.

Now, let's again quickly take a look at our rollno array :

```int rollno[n];
```

Technically, before declaring this statement, you should first define the value of variable n, or else you'll face an error.

Here, n is just use to make you first comfortable with basic declaration of array. More sophisticatedly, you can simply use a definite value of n to define an array as follows:

```int rollno; //Enough space to store roll no of 60 students
```

Next, we need to fill in the values for our array. Traditionally, following an approach as we've seen in the beginning would not only take a lot of time to define each variable corresponding to each student, but makes it a hectic process.

But first, we need to explore more about what exactly the index no or indices of the array is?

Well, typically, the computer memory is divided into small data cells (as shown above in the picture), occupying contiguous memory location. To access any element stored in an array we need something to reference it, like a variable name. In case we followed, the very first traditional approach defining 60 variables, they would have different names, which would be difficult to remember, but we could refer a single element. Using array, makes this more efficient by providing us with index no. or indices, as each subsequent element can be addressed / referred to by it's index value, which we would see in just a moment.

## What would be the size of the array?

An array would consist of many elements and hence the size of the array would be the sum of size of all elements in the array. For instance, in our example of rollno array for 60 student, which is of type int, the memory locations are occupied such that each cell has capacity to store an integer. We know, that in C , integer takes two bytes, so total memory occupied by rollno array would be 2 * 60  = 120.

Thus, you can easily calculate the amount of memory an array occupies by using the following formula:

Size of array = Size of data type * Total no. of elements in the array

Now let's see how to fill up the array:

## Putting values in array

Filling up an array is very simple using for loop, as we would see here :

```int rollno; //Declaring array with 60 elements storage space
for(int i = 0;i < 60; i++)
{
scanf("%d",rollno[i]);  //Here value of i increments, and allow to store next input to the very next space till value of i = 60 where for loop terminates.
}
```

The working is very straight forward as explained in the comments.

One important thing to note here is that, as previously mentioned the index of array would start from 0 not 1. You should always remember this thing while handling data with arrays and your loops should also work in the same fashion.

NOTE : Newbies mistake, assuming array to be start from 1. Remember, it would be started from 0.

## Printing out elements of Array

Now, after filling up the array, let's see how we can access one particular element. For instance, let's say I need to look for roll no. of 11th student, so I know it would be stored as 10th element in memory (not 11th) as indices start from 0. So, let's print the value of 11th student.

```...
printf("%d",rollno);
...
```

In case you want to print all values stored in the array, we simply apply a loop to loop through the all the values in the array from indices 0 to indices 59 (a total of 60 values in this case).

```...
for (int i = 0,i <= 59, i++)
{
printf("%d",rollno[i]);
}
...
```

So, now that you've learned about 1 dimensional arrays, it's time to look for two dimensional arrays.

Have any doubts till now? Please feel free to mention in comments section below, or give me a shout on twitter \@ErSanyamKhurana.

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.