- An array is a collection of data of the same type stored in a contiguous memory location.
- The array is the derived data type in the C programming language which can store the primitive data types, such as pointers structure, etc.
Advantages:
1) Code Optimization: Less code to access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantages:
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can’t exceed the limit. So, it doesn’t grow the size dynamically like Linked List which we will learn later.
Properties of Arrays:
- Each element of an array is of the same data type and carries the same size.
- Elements of an array are stored in contiguous memory locations where the first element is stored at the smallest memory location
- Elements of the array can be randomly accessed since we can calculate the address of each element of the array with the given base address and the size of the data element.
Array Declaration:
syntax:
data_type array_name [arraySize];
int a[5];
int num[5] = {1,2,3,4,5};
we can also initialize an array like this:
int num[] = {1,2,3,4,5};
- once the array is declared, then the individual elements in the array can be referred to by the number in the brackets following the array name this number specifies the position of the element in the array
- Subscript starts with 0, which means
arr[0]
represents the first element in the array arr. - Thus num[2] is not the second element but this is considered as the third element of the array.
For example:
int mydata[20];
mydata[0] /* first element of array mydata*/
mydata[19] /* last (20th) element of array mydata*/
One-dimensional array:
One dimensional array is an array that has only one subscript specification that is needed to specify a particular element of an array. A one-dimensional array is a structured collection of components (often called array elements) that can be accessed individually by specifying the position of a component with a single index value.
Syntax:
data-type arr_name[array_size];
Entering data into an array
for(i=0;i<=4;i++)
{
printf("Enter number");
scanf("%d",&num[i]);
}
}
Here we are iterating the array from 0 to 4 because the size of the array is 5, and all the values will be stored in the corresponding array elements using scanf function.
Reading data from an Array
Suppose we have to display the above entered elements
for(i=0;i<4;i++)
{
printf("%d",num[i]);
}
*Program to find the sum of array elements expand by the user*/
#include<stdio.h>
void main()
{
int sum=0,i;
int num[5]; /*array declaration*/
for(i=0;i<=4;i++)
{
printf("\nEnter number");
scanf("%d",&num[i]); /*Storing data in array*/
}
for(i=0;i<=4;i++)
sum=sum+num[i];
}
printf("Ssum of numbers=%d",sum);
getch();
}
*Passing array to function using call by reference
#including<stdio.h>
void display(int a);
void main()
{
int i;
int num[]={2,4,6,8,10}
for(i=0;i<=4;i++)
{
display(&num[i]);
}
}
void display(int*m)
{
printf("%d",*m);
getch();
}
Output=2,4,6,8,10
Passing an array to a function
Just like variable array can also be passed to a function as an argument
Two methods of passing array in function
- By call by value
- By call by reference, method
Call by value:
In call by value, the original value is not modified.
In call by value, the value being passed to the function is locally stored by the function parameter in a stack memory location. If you change the value of the function parameter, it is changed for the current function only. It will not change the variable value inside the caller method such as main().
EXAMPLE:
#include <iostream>
using namespace std;
void change(int data);
int main()
{
int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5;
}
OUTPUT
Value of the data is: 3
Call by reference:
In call by reference, original value is modified because we pass a reference (address).
Here, the address of the value is passed in the function, so actual and formal arguments share the same address space. Hence, the value changed inside the function is reflected inside as well as outside the function.
EXAMPLE
#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
int swap;
swap=*x;
*x=*y;
*y=swap;
}
int main()
{
int x=500, y=100;
swap(&x, &y); // passing value to function
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of y is: "<<y<<endl;
return 0;
}
OUTPUT
Value of x is: 100
Value of y is: 500