Arrays are defined as a finite set of values or variables of same data type. An array is a collection of values of similar data type where all the values have the same name but different index or element number. In some program there can be a condition where we want to process large number of values but are hesitating in declaring so many variables. For example, to process the roll numbers of 100 students, declaring 100 individual variables will be a tedious task. Declaring 100 variables, thinking of 100 unique names and managing so many variables is quite a time consuming task. Arrays are declared to help the user overcome this problem. With the use of array we can easily store as many values as we want without having to create multiple variables.
Syntax
datatype arrayname[size]
Example
int marks[5];
float data[10];
char name[15];
The first array, marks has been declared with a size of 5 which means that we can store any 5 integer values, while the second array data has the size of 10. The internal structure of the first array looks likes this
0
1
2
3
4
As you can see the structure of an array resembles a table having multiple rows and single column. The first row is numbered as 0 rather than 1 which means that the first value of the array will always be stored at 0 whereas the last value will be at index number , size-1.
Why should we use arrays?
One important thing to understand here is why we need arrays when we can do the same task without them. Let’s see the example given below to understand this. The following program will ask the user to enter 10 values and will display their sum.
void main()
{
int num, sum=0;
for(i=0;i<10;i++)>
{
printf(“\nEnter a number : “);
scanf(“%d”,&num);
sum+=num;
}
printf(“\nSum of all numbers is %d”,sum);
}
When you run the above program you will see that it works fine – accepts 10 values from you – adds them up – and displays the result. Even though the code is correct and so is the output there is one serious problem. What if the user wants to see all the values he entered just to confirm the output. If you try to print the value of “num” you will see that only the last value is being printed. This is because the moment the loop runs the second time the previous value gets lost from the memory. It works something like this:
a=10;
a=20;
printf(“\nA = %d”,a);
What will the value of “a” when it is printed, 10 or 20? The answer is 20, why not 10? If you ask. The answer is simple – no variable can hold more than one value at a time so when the statement “a=20” is reached “a” is assigned the value 20 and the previous value of 10 is overwritten. You cannot recover the value once it has been overwritten. The same thing happens to “num” in the above example. Since we are using only a single variable “num” to store 5 values each value overwrites the previous one, so only the last value is printed and there is no way you can get the old values? Let’s try the same problem with arrays
Example 1 : Adding the values of an array.
void main()
{
int values[5], i, sum=0;
for(i=0;i<5;i++)
{
printf(“\nEnter a number : “);
scanf(“%d”,&values[i]);
sum+=values[i];
}
}
On running this program you will get the same output which is sum of 10 values. So what have we achieved by using an array. The advantage is that the values are no longer overwriting the previous values so we can print or access any of the values which we have entered be it the first one or the last one or any other value. The array is helping us in storing these values without overwriting since it can store 10 values at the same time. After filling the array with our values it will look something like this (the values written inside are assumed values entered by the user) 12 63 47 5 93 From the above diagram it is evident that each value has been stored at a particular location which is called the index or element number. For example, 12 is stored at element number 0, 47 at index 2 and so on. Therefore, if you want to print all the values at the end of the program you can append the following code after the first program.
for(i=0;i<5;i++)>
{
printf(“\n%d”,values[i]);
}
In case you want only a particular value you can even do that by specifying the index number of the value which you want to see. For example, printf(“%d”,values[3]); Will print the third value of the array (which will be the fourth value since arrays count them from 0)
Properties of an array
1. An array always starts from index number 0. It is not possible to start it from any other location.
2. The array elements are always stored in contiguous memory. This means that the array elements are always allocated memory in continuous memory blocks.
3. Arrays are static in nature which means that once an array has been declared we cannot increase or decrease its size.
4. If the array is declared with a large size and number of values is less the remaining memory will be wasted similarly if a small size is decided upon it will lead to storage problem.
Example 2: Finding out the highest number among a list of numbers.
This problem can be solved by two methods, first using the && operator along with an if-else block the statement for which can be something like
if(a>b && a>c && a>d && a>e)
printf(“\na is greatest”);
else if(b>a && b>c && b>d && b>e)
printf(“\nb is greatest”);
This approach can solve the problem but we will have to write many lines of code. The same problem can be solved easily using arrays
void main()
{
int list[10],max,i;
for(i=0;i<10;i++)>
{
printf(“\nenter values in the array “);
scanf(“%d”,&list[i]);
}
max=list[0];
for(i=1;i<10;i++)>
{
if (list[i]>max)
max=list[i];
}
printf(“\nMaximum value is %d”,max);
}
When the above program is run it will ask the user to enter 10 values and will store then one by one to the array. The storage will happen from element number 0. After all the values have been stored the variable max has been assigned the first value of the array. The second loop then compares the value of max with the remaining array values starting from 1. If it finds any value having data larger than then max, it replaces the value of max with that number.
0 comments:
Post a Comment