In computer science, Array is a type of Data structure that is used to store elements of the same data type in continuous memory locations. Arrays let us store multiple values of the same data type in a single variable. Different types of arrays make it possible to store the values in various structures. One of the most common structures of an Array in which the values are stored is the One Dimensional Array Structure. We will learn more about the concept of 1D Array in the article below.
Definition
A One-Dimensional Array is the simplest form of an Array in which the elements are stored linearly and can be accessed individually by specifying the index value of each element stored in the array.
A One-Dimensional Array is a group of elements having the same data type which are stored in a linear arrangement under a single variable name.
One Dimensional Array
This is one of the simplest forms of Array. They are very easy to define and use in the programs. The values stored in a One Dimensional Array can be easily initialized and manipulated, making it a very feasible Data structure type.
Input Elements in a 1D Array
There are few methods by which we can assign and store values in an Array. Let us understand by looking at the 2 most common methods.
Direct Initialization
In this method, the elements are assigned during the declaration of the 1D Array.
Example
COPY CODE
int num [10] = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10} ;
User Input Method
In this method, The user is asked to enter the array elements of his/her choice during the run-time of the program.
Example –
COPY CODE
#include
using namespace std;
int main()
{
int num [5] ;
cout<<"Enter array elements : \n";
for(int i = 0; i < 5 ; i++)
{
cin >> num[i] ;
}
}
Do'stlaringiz bilan baham: |