Aarray_DS
Aarray_DS
Types of Array:
1. One-Dimensional Array
2. Multi-Dimensional Array
One-Dimensional Array:
A type of array in which all elements are arranged in the form of a list is
known as one-dimensional array.it is also called single-dimensional array. It
consists of one column or one row.
Syntax
type arrayName [ arraySize];
Example
int foo [5];
int foo [5] = {16, 2, 77, 40, 12071 } ;
Example
#include<iostream>
using namespace std;
int main()
{
int arr[4];
cout<<"enter the value for index 0"<<endl;
cin>>arr[0];
cout<<"enter the value for index 1"<<endl;
cin>>arr[1];
cout<<"enter the value for index 2"<<endl;
cin>>arr[2];
cout<<"enter the value for index 3"<<endl;
cin>>arr[3];
cout<<"enter the value for index 4"<<endl;
cin>>arr[4];
cout<<"value are"<<endl;
cout<<arr[0]<<endl;
cout<<arr[1]<<endl;
cout<<arr[2]<<endl;
cout<<arr[3]<<endl;
cout<<arr[4]<<endl;
}
#include<iostream>
using namespace std;
int main()
{
int foo [5];
cout<<"enter the five values"<<endl;
for(int x=0;x<5;x++)
{
cin>>foo[x];
}
for(int y=0;y<5;y++)
{
cout<<foo[y]<<endl;
}
return 0; }
Two-Dimensional Array:
Two-dimensional array can be considered as a table that consists of rows
and column. Each element in 2-D array is referred with the help of two
indexes. one index is used to indicate the row and second index indicates
the column of the element.
Syntax
type arrayName [ size][size];
Example
Int jimmy [3][5];
int jimmy [3][5] = {2, 4, 5, 9, 0, 19, 23, 47, 5, 91, 3, 11, 55, 93, 6};
#include<iostream>
using namespace std;
int main()
{
int arr[2][3];
cout<<"enter the value"<<endl;
cin>>arr[0][0];
cout<<"enter the value"<<endl;
cin>>arr[0][1];
cout<<"enter the value"<<endl;
cin>>arr[0][2];
cout<<"enter the value"<<endl;
cin>>arr[1][0];
cout<<"enter the value"<<endl;
cin>>arr[1][1];
cout<<"enter the value"<<endl;
cin>>arr[1][2];
cout<<"value are"<<endl;
cout<<arr[0][0]<<endl;
cout<<arr[0][1]<<endl;
cout<<arr[0][2]<<endl;
cout<<arr[1][0]<<endl;
cout<<arr[1][1]<<endl;
cout<<arr[1][2]<<endl;
}
#include<iostream>
int main()
for(int x=0;x<3;x++)
for(int a=0;a<5;a++)
cin>>arr[x][a];
}
for(int y=0;y<3;y++)
for(int b=0;b<5;b++)
cout<<arr[y][b];
return 0; }
Three-Dimensional Array:
Syntax
type arrayName [ size][size][size];
#include<iostream>
int main()
for(int x=0;x<3;x++)
for(int a=0;a<2;a++)
for(int d=0;d<3;d++)
cin>>number[x][a][d];
}
cout<<"values are"<<endl;
for(int y=0;y<3;y++)
for(int b=0;b<2;b++)
for(int e=0;e<3;e++)
cout<<number[y][b][e]<<endl;
return 0; }