An array is a collection of similar types of data element
which holds fixed number of values.
Example:
For example an int array holds the elements of int types while a float array holds the elements of float types
Advantages:
Less code is required to access the data
We can access any element randomly using the array
We can sort the elements of array
Disadvantage:
Array size is fixed so we cannot change the value
Types:
There are 2 types of C++ arrays.They are
For example an int array holds the elements of int types while a float array holds the elements of float types
Less code is required to access the data
We can access any element randomly using the array
We can sort the elements of array
Disadvantage:
Array size is fixed so we cannot change the value
Types:
There are 2 types of C++ arrays.They are
1) One dimensional array
2) Multi dimensional array
2) Multi dimensional array
One dimensional array
Syntax:
Data_type array_name
[array_Size];
Example
int marks[10];
Where int is the data_type, marks is
the array_name and 10 is the array_size.
Note:
Array index is always started from 0.Its range between 0 to n-1.
Where n is the number of elements.0 is the lower index and n-1 is the upper index.
The size and type of arrays cannot be changed after its declaration.
Array index is always started from 0.Its range between 0 to n-1.
Where n is the number of elements.0 is the lower index and n-1 is the upper index.
The size and type of arrays cannot be changed after its declaration.
Declaration with Initialization
There are two ways to declare with initialize the array. They are
1ST Way:
int marks[5];//declaration of array
marks[0]=10;//initialization of array
marks[1]=20;
marks[2]=30;
marks[3]=40;
marks[4]=50;
2nd Way
We can initialize the array at the time of declaration like
int marks[5]={10,20,30,40,50};
or
int marks[]={20,30,40,50,60};
Example:
#include<iostream>
using namespace std;
int main(){
int i;
int
marks[5]={10,20,30,40,50};//declaration and initialization of array
for(i=0;i<5;i++){
cout<<marks[i]<<endl;
}
return 0;
}
Output:
10
20
30
40
50
How to take
1D Array values from user in C
#include<iostream>
using
namespace std;
int
main()
{
int marks[20], i, n;
cout<<"Enter
n:"<<endl;
cin>>n;
for(i=0; i<n; ++i) //Input statement
{
cout<<"Enter number
"<<i+1<<":";
cin>>marks[i];
}
for(i=0;
i<n; ++i) // Output statement
{
cout<<"Array
is:"<<marks[i];
}
return 0;
}
Two Dimensional Array
Two dimensional array is know as matrix because it is represented by rows and columns.Two ,three or other dimensional arrays are also known as multidimensional arrays.
Declaration of 2D Array:
data_type array_name[row][column];
Example:
int arr[2][3];
Where 2 is the row number and 3 is the column number.
Where 2 is the row number and 3 is the column number.
Initialization of 2D Array
int arr[2][3]={{1,2,3},{2,3,4}};
#include<iostream>
using
namespace std;
int
main()
{
int
i,j;
int
arr[2][3]={{1,2,3},{2,3,4}};
for(i=0;i<2;i++){
for(j=0;j<3;j++){
cout<<arr[i][j]<<"
";
}
cout<<"\n";
}
return
0;
}
Output:
1
2 3
3
4 5
How to take
2D Array values from user in C
#include<iostream>
using
namespace std;
int
main()
{
int arr[2][3];
int i, j;
cout<<"Enter array element
\n";
for(i = 0; i < 2;i++)
{
for(j = 0; j < 3; j++)
{
cin>>arr[i][j];
}
}
for(i = 0; i < 2; i++)
{
for(j = 0; j < 3; j++)
{
cout<<arr[i][j]<<"
";
}
cout<<"\n";
}
return 0;
}
Python is an open-source, high level, interpreted programming language which offers great opportunities for object-oriented programming. Choosing a python training program from Red Prism Group, which is best training institute for python in noida.
ReplyDelete