ARRAY– Stores multiple items[Series of characters, multiple strings].
Array is declared in the form of;
int a[3][2]={
{1,4},
{5,2},
{6,5}
};
Declared array is of size 3*2.
This declared array contains total of 6 elements.
Row1: {1,4},
Row2: {5,2},
Row3: {6,5}.
Thus the array can be initialized row wise as well as sequentially.
Arrays are formulated as:
Type of array: Defines the type of element- Such as char, int , structure ec..
Name of array: Defines the name given to an array.
Number of element: Value inside the subscript”[]” gives the number of element.
Example:
char arr[5];
Initialization:
* Initializing each element seperately
Example:
int arr[10];
int i;
for (i=0;i<=10;i++)
{
arr[i]=i;// Each element is initializeed seperarately
}
* Initializing array at the time of decelaration
Example:
int arr[]={‘1′,’2′,’3′,’4′,’5’};
*Initializing array with a string
Type 1
Example:
char arr[]= {‘c’,’o’,’d’,’e’,”};
Type 2
Example:
char arr[] = “code”;
Accessing values in an array
Example:
int arr[10];
int i=0;
for(i=0;i<size of(arr); i++)
{
arr[i]=i; //initializing each element seperately
}
int j = arr[5];//accessing the 5th element of integer array arr and assigning its value to integer j.
Types of array:
One dimensional array:
Its a structured collection of components.
syntax: DataType ArrayName [constIntExpression];
Example:
int number [50];
To store the values in the number array, we can formulate it as
for(int i=0;i<50;i++)
{
number[i]=i;
cout<<“number[“<<i<<“]=”<<number[i]<<endl;
To use the stored values,
for (int i=0; i<50; i++)
{
number[i]=2*number[i];
cout<<“number[“<<i<<“]=”<<number[i]<<endl;
Example for one dimensional array:
// Program arrays.cpp manipulates values in an array.
#include <iostream>
using namespace std;
int main () {
const int MAX_ARRAY = 5;
int numbers[MAX_ARRAY];
int index;
int sum;
// Stored values in the array.
for (index = 0; index < MAX_ARRAY; index++)
numbers[index] = index * index;
// The values in the array are summed.
sum = 0;
for (index = 0; index < MAX_ARRAY; index++)
sum = sum + numbers[index];
cout << “Sum is ” << sum << endl;
// Add code to print out the values of all the array elements
return 0;
}
Array used for maintaining multiple variable names using single name:
For
Example
int roll1,roll2,roll3,roll4,roll5;- This can be re formed as
int roll[5];
Array are also used for sorting elements
SORTING
Basically sorting are classified as
Bubble sort
Insertion sort
Selection sort
Bucket sort
Array can perform matrix operation:
Matrix can performed using multi dimensional array.
One dimensional array
Example
#include <stdio.h>
int main()
{
int num[] = {2,8,7,6,0};
int i;
for(i=0;i<5;i++) {
printf(“\nArray Element num[%d] : %d”,i+1,num[i]);
}
return 0;
}
Limitations of array in c programming:
1. Static Data
*Array is static data structure
*Memory allocated during compile time
*once memory is allocated at compile time it cannot be changed during run time.
2.Can hold data belonging to same data types.
3.Inserting data in array is difficult.
4.Deletion operation is difficult.
5. Bound checking.
6.Shortage of memory
7.Wastage of memory.
Common mistakes:
1. Constant expression require
#include<stdio.h>
void main()
{
int i=10;
int a[i];
}
In the above example, i is initialized to 10, and denoting as a[i] doesnot mean as a[10] because i is the integer variable whose value can be changed inside the program.
so the above example can be rereturn as
#include<stdio.h>
void main()
{
const int i=10;
int a[10];
}
OR
int a[10];
2. Empty valued
#include<stdio.h>
void main()
{
int arr[];
}
OR
#include<stdio.h>
void main()
{
int a[]={};// This aso creates an error.
}
In this example, the size of the array must be specified as a constant value.
So, the correct form of writing this example is given as,
#include<stdio.h>
void main()
{
int arr[]={2,2};
}
3. Does not checks bound checking
#include<stdio.h>
void main()
{
int a[5];
printf(“%d”,a[7]);
}
In this example, array size is specified as 5, so the array element is given by a[0], a[1], a[2], a[3] and a[4].
but accesing a[5] causes the garbage value to be used since c doesnt perform arrays bound check.
4. Case sensitive
#include<stdio.h>
void main()
{
int a[5];
printf(“%d”,A[2]);
}
Tips for assigning array:
1. Use #define to specify array size
#include<stdio.h>
#define MAX 5
void main()
{
int a[MAX];
printf(“%d”,a[2]);
}
In this example, as MAX is replaced by 5 for every occurence, inorder to increase or decrease the size of array then the changes should be made in #define max 10
2.a[i] and i[a] are same