Arrays
One Dimensional Array
An array is a collection of data elements of same data type. It is described by a single name and each element of an array is referenced by using array name and its subscript no.
Declaration of Array
Type arrayName[numberOfElements];
For example,
int Age[5] ;
float cost[30];
Initialization of One Dimensional Array
An array can be initialized along with declaration. For array initialization it is required to place the elements separated by commas enclosed within braces.
It is possible to leave the array size open. The compiler will count the array size.
int A[5] = {11,2,23,4,15};
It is possible to leave the array size open. The compiler will count the array size.
int B[] = {6,7,8,9,15,12};
Referring to Array Elements
In any point of a program in which an array is visible, we can access the value of any of its elements individually as if it was a normal variable, thus being able to both read and modify its value. The format is as simple as:
name[index]
Examples:
name[index]
Examples:
cout<<age[4]; //print an array element
age[4]=55; // assign value to an array element
cin>>age[4]; //input element 4
Using Loop to input an Array from user
int age [10], i ;
for (i=0 ; i<10; i++)
{
cin>>age[i];
}
Arrays as Parameters
At some moment we may need to pass an array to a function as a parameter. In C++ it is not possible to pass a complete block of memory by value as a parameter to a function, but we are allowed to pass its address.
For example, the following function:
accepts a parameter of type "array of int" called A.
In order to pass to this function an array declared as:
we need to write a call like this:
Here is a complete example:
For example, the following function:
void print(int A[])
accepts a parameter of type "array of int" called A.
In order to pass to this function an array declared as:
int arr[20];
we need to write a call like this:
print(arr);
Here is a complete example:
#include <iostream.h>
void print(int A[], int length)
{
for (int n=0; n<length; n++)
cout << A[n] << " ";
cout << "\n";
}
void print(int A[], int length)
{
for (int n=0; n<length; n++)
cout << A[n] << " ";
cout << "\n";
}
int main ()
{
int arr[] = {5, 10, 15};
print(arr,3);
return 0;
}
{
int arr[] = {5, 10, 15};
print(arr,3);
return 0;
}
Basic Operation On One Dimensional Array
Function to traverse the array A
void display(int A[], int n)
{
cout<<"The elements of the array are:\n";
for(int i=0;i<n;i++)
cout<<A[i];
}
{
cout<<"The elements of the array are:\n";
for(int i=0;i<n;i++)
cout<<A[i];
}
Function to Read elements of the array A
void Input(int A[], int n)
{
cout<<"Enter the elements:";
for(int i=0;i<n;i++)
cin>>A[i];
}
{
cout<<"Enter the elements:";
for(int i=0;i<n;i++)
cin>>A[i];
}
Function to Search for an element from A by Linear Search
int Lsearch(int A[], int n, int Data)
{
int I;
for(I=0; I<n; I++)
{
if(A[I]==Data)
{
cout<<"Data Found at : "<<I;
return;
}
}
cout<<"Data Not Found in the array"<<endl;
}
{
int I;
for(I=0; I<n; I++)
{
if(A[I]==Data)
{
cout<<"Data Found at : "<<I;
return;
}
}
cout<<"Data Not Found in the array"<<endl;
}
Function to Search for an element from Array A by Binary Search
int BsearchAsc(int A[], int n, int data)
{
int Mid,Lbound=0,Ubound=n-1,Found=0;
while((Lbound<=Ubound) && !(Found))
{
Mid=(Lbound+Ubound)/2; //Searching The Item
if(data>A[Mid])
Lbound=Mid+1;
else if(data<A[Mid])
Ubound=Mid-1;
else
Found++;
}
if(Found)
return(Mid+1); //returning 1ocation, if present
else
return(-1); //returning -1,if not present
}
{
int Mid,Lbound=0,Ubound=n-1,Found=0;
while((Lbound<=Ubound) && !(Found))
{
Mid=(Lbound+Ubound)/2; //Searching The Item
if(data>A[Mid])
Lbound=Mid+1;
else if(data<A[Mid])
Ubound=Mid-1;
else
Found++;
}
if(Found)
return(Mid+1); //returning 1ocation, if present
else
return(-1); //returning -1,if not present
}
Function to Sort the array A by Bubble Sort
void BSort(int A[], int n)
{
int I,J,Temp;
for(I=0;I<n-1;I++) //sorting
{
for(J=0;J<(n-1-I);J++)
if(A[J]>A[J+1])
{
Temp=A[J]; //swapping
A[J]=A[J+1];
A[J+1]=Temp;
}
}
}
{
int I,J,Temp;
for(I=0;I<n-1;I++) //sorting
{
for(J=0;J<(n-1-I);J++)
if(A[J]>A[J+1])
{
Temp=A[J]; //swapping
A[J]=A[J+1];
A[J+1]=Temp;
}
}
}
Function to Sort the array ARR by Insertion Sort
void ISort(int A[], int n)
{
int I,J,Temp;
for(I=1;I<n;I++) //sorting
{
Temp=A[I];
J=I-1;
while((Temp<A[J]) && (J>=0))
{
A[J+1]=A[J];
J--;
}
A[J+1]=Temp;
}
}
{
int I,J,Temp;
for(I=1;I<n;I++) //sorting
{
Temp=A[I];
J=I-1;
while((Temp<A[J]) && (J>=0))
{
A[J+1]=A[J];
J--;
}
A[J+1]=Temp;
}
}
Function to Sort the array by Selection Sort
void SSort(int A[], int n)
{
int I,J,Temp,Small;
for(I=0;I<n-1;I++)
{
Small=I;
for(J=I+1;J<n;J++) //finding the smallest element
if(A[J]<A[Small])
Small=J;
if(Small!=I)
{
Temp=A[I]; //Swapping
A[I]=A[Small];
A[Small]=Temp;
}
}
}
{
int I,J,Temp,Small;
for(I=0;I<n-1;I++)
{
Small=I;
for(J=I+1;J<n;J++) //finding the smallest element
if(A[J]<A[Small])
Small=J;
if(Small!=I)
{
Temp=A[I]; //Swapping
A[I]=A[Small];
A[Small]=Temp;
}
}
}
Function to merge A and B arrays of lenghts N and M
void Merge(int A[], int B[], int C[], int N, int M, int &K)
{
int I=0, J=0;
K=0; //Initialisation of counters for A, B, and C
while (I<N && J<M)
{
if (A[I]<B[J])
C[K++]=A[I++];
else if (A[I]>B[J])
C[K++]=B[J++];
else
{
C[K++]=A[I++];
J++;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T];
for (T=J;T<M;T++)
C[K++]=B[T];
}
{
int I=0, J=0;
K=0; //Initialisation of counters for A, B, and C
while (I<N && J<M)
{
if (A[I]<B[J])
C[K++]=A[I++];
else if (A[I]>B[J])
C[K++]=B[J++];
else
{
C[K++]=A[I++];
J++;
}
}
for (int T=I;T<N;T++)
C[K++]=A[T];
for (T=J;T<M;T++)
C[K++]=B[T];
}
Two Dimensional Array
It is a collection of data elements of same data type arranged in rows and columns (that is, in two dimensions).
Declaration of Two-Dimensional Array
Type arrayName[numberOfRows][numberOfColumn];
For example,
int Sales[3][5];
Initialization of Two-Dimensional Array
An two-dimensional array can be initialized along with declaration. For two-dimensional array initialization, elements of each row are enclosed within curly braces and separated
by commas. All rows are enclosed within curly braces.
by commas. All rows are enclosed within curly braces.
int A[4][3] = {{22, 23, 10},
{15, 25, 13},
{20, 74, 67},
{11, 18, 14}};
Referring to Array Elements
To access the elements of a two-dimensional array, we need a pair of indices: one for
the row position and one for the column position. The format is as simple as:
name[rowIndex][columnIndex]
Examples:
the row position and one for the column position. The format is as simple as:
name[rowIndex][columnIndex]
Examples:
cout<<A[1][2]; //print an array element
A[1][2]=13; // assign value to an array element
cin>>A[1][2]; //input element
Using Loop to input an Two-Dimensional Array from user
int mat[3][5], row, col ;
for (row = 0; row < 3; row++)
for (col = 0; col < 5; col++)
cin >> mat[row][col];
for (row = 0; row < 3; row++)
for (col = 0; col < 5; col++)
cin >> mat[row][col];
Arrays as Parameters
Two-dimensional arrays can be passed as parameters to a function, and they are passed by reference. When declaring a two-dimensional array as a formal parameter, we can omit the size of the first dimension, but not the second; that is, we must specify the number of columns. For example:
In order to pass to this function an array declared as:
we need to write a call like this:
Here is a complete example:
void print(int A[][3],int N, int M)
In order to pass to this function an array declared as:
int arr[4][3];
we need to write a call like this:
print(arr);
Here is a complete example:
#include <iostream.h>
void print(int A[][3],int N, int M)
{
for (R = 0; R < N; R++)
for (C = 0; C < M; C++)
cout << A[R][C];
}
void print(int A[][3],int N, int M)
{
for (R = 0; R < N; R++)
for (C = 0; C < M; C++)
cout << A[R][C];
}
int main ()
{
int arr[4][3] ={{12, 29, 11},
{25, 25, 13},
{24, 64, 67},
{11, 18, 14}};
print(arr,4,3);
return 0;
}
{
int arr[4][3] ={{12, 29, 11},
{25, 25, 13},
{24, 64, 67},
{11, 18, 14}};
print(arr,4,3);
return 0;
}
Function to read the array A
void Read(int A[][20], int N, int M)
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
{
cout<<"(R<<','<<")?";
cin>>A[R][C];
}
}
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
{
cout<<"(R<<','<<")?";
cin>>A[R][C];
}
}
Function to display content of a two dimensional array A
void Display(int A[][20],int N, int M)
{
for(int R=0;R<N;R++)
{
for(int C=0;C<M;C++)
cout<<setw(10)<<A[R][C];
cout<<endl;
}
}
{
for(int R=0;R<N;R++)
{
for(int C=0;C<M;C++)
cout<<setw(10)<<A[R][C];
cout<<endl;
}
}
Function to find the sum of two dimensional arrays A and B
void Addition(int A[][20], int B[][20],int N, int M)
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
C[R][C]=A[R][C]+B[R][C];
}
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
C[R][C]=A[R][C]+B[R][C];
}
Function to multiply two dimensional arrays A and B of order NxL and LxM
void Multiply(int A[][20], int B[][20], int C[][20],int N, int L, int M)
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
{
C[R][C]=0;
for(int T=0;T<L;T++)
C[R][C]+=A[R][T]*B[T][C];
}
}
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
{
C[R][C]=0;
for(int T=0;T<L;T++)
C[R][C]+=A[R][T]*B[T][C];
}
}
Function to find & display sum of rows & sum of cols. of a 2 dim. array A
void SumRowCol(int A[][20], int N, int M)
{
for(int R=0;R<N;R++)
{
int SumR=0;
for(int C=0;C<M;C++)
SumR+=A[R][C];
cout<<"Row("<<R<<")="<<SumR<<endl;
}
for(int R=0;R<N;R++)
{
int SumR=0;
for(int C=0;C<M;C++)
SumR+=A[R][C];
cout<<"Row("<<R<<")="<<SumR<<endl;
}
}
{
for(int R=0;R<N;R++)
{
int SumR=0;
for(int C=0;C<M;C++)
SumR+=A[R][C];
cout<<"Row("<<R<<")="<<SumR<<endl;
}
for(int R=0;R<N;R++)
{
int SumR=0;
for(int C=0;C<M;C++)
SumR+=A[R][C];
cout<<"Row("<<R<<")="<<SumR<<endl;
}
}
Function to find sum of diagonal elements of a square matrix A
void Diagonal(int A[][20], int N, int &Rdiag, int &LDiag)
{
for(int I=0,Rdiag=0;I<N;I++)
Rdiag+=A[I][I];
for(int I=0,Ldiag=0;I<N;I++)
Ldiag+=A[N-I-1][I];
}
{
for(int I=0,Rdiag=0;I<N;I++)
Rdiag+=A[I][I];
for(int I=0,Ldiag=0;I<N;I++)
Ldiag+=A[N-I-1][I];
}
Function to find out transpose of a two dimensional array A
void Transpose(int A[][20], int B[][20],int N, int M)
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
B[R][C]=A[C][R];
}
{
for(int R=0;R<N;R++)
for(int C=0;C<M;C++)
B[R][C]=A[C][R];
}
0 comments:
Post a Comment