What is One Dimensional Array- How to initialize One Dimensional Array in C++

 

How to initialize One Dimensional Array in C++:

How to initialize One Dimensional Array in C++

As we have two types of arrays, one dimensional and two dimensional array, both have their own ways of definition. Here, we are defining and initializing one dimensional array.

One Dimensional Array.

A one-dimensional array or linear array can be represented as a single row of contiguous memory locations. Accessing of its elements involves a single subscript
 

 

Like a regular variable, an array must be declared before it is used. A typical declaration for a one dimensional array in C++ is:
 
data type name[size];
 
For example:
 
int x[7];
char vowel[5];
float temperature[7];
 

Initialization One Dimensional Array.

Arrays can be initialized, like variable, when create. C++ provides a convenient way to initialize entire array via use of an initialize list.
 
The following example shows the initialization of an array.
 
#include<iostream.h>
#include<conio.h>
int main()
{
int A[5]={3,2,7,5,8};
cout<<A[10]<<endl;
cout<<A[1]<<endl;
cout<<A[2]<<endl;
cout<<A[3]<<endl;
cout<<A[4]<<endl;
getch();
return 0;
}
 
Output of the program
 
3
2
7
5
8
 
If we do not initialize all of the elements in array then the remaining elements will be initialized to 0.
 
Similarly, to initialize all the elements of an array to 0, we can define that array as:
int A[5]={0};
Accessing and Writing at an Index in an Array.
Accessing of array means to get into an index in an array and take its value for some operation while by writing at an index of an array is storing some value at a particular index of the array. These are the two important actions that should be taken while dealing with arrays.
 
The following section addresses them with examples.

Accessing an Array.

Array elements are accessed by using respective indices. The format for accessing is as follows:
 
Array name[index];
 
Looking at the example disused in the above section, in which A has 5 elements, each of the element has been accessed as A[0], A[1],…… A[4] using cout statement.
 
To access all elements of an array we use loops instead of individual reference to each element, For example:
 
#include<iostream.h>
#include<conio.h>
int main()
{
int A[5]={3,2,7,5,8};
for(int i=0; i<=4; i++)
cout<<A[i]<<endl;
getch();
return 0;
}
 
Output of the program
 
3
2
7
5
8
 
Here, for loop has been used to access the element of the array one by one and print the result on the screen.

Writing at an index in an Array.

Accessing of an index of an array is followed by the writing operation, only in the case, if we want to store a new value at that index to update the existing one. 
 
For example, if we want to add the values in the array A, we can individually access each array location and write the value using assignment operator or using input statement (cin) it is show in the following program.
 
#include<iostream.h>
#include<conio.h>
int main()
{
int A[5];
A[0]=12;
A[1]=-20;
cout<<“Enter number”<<endl;
cin>>A[2];
cout<<“Enter number”<<endl;
cin>>A[3];
cout<<“Enter number”<<endl;
cin>>A[4];
getch();
return 0;
}
 
Output of the program
 
Enter number
7
Enter number
5
Enter number
8
 
Loops can also be used for writing at all indexes of an array.
 
Consider the following example having an array Temperature that accepts the value of weekly temperature from the users at run time and write them into their respective indexes.
 
#include<iostream.h>
#include<conio.h>
int main()
{
float Temperature[7];
cout<<“Enter 7 days temperature”;<<endl;
for(int i=0; i<7; i++)
cin>>Temperature[i]; // write at indexes
cout<<“The last week temperature was:”<<endl;
for(int i=0; i<7; i++)
cout<<Temperature[i]<<“,”; // accessing the elements
getch();
return 0;
}
 
Output of the program
 
Enter 7 days temperature
35.9 22.0 27.3 25.5 28.3 31.2 33.7
The last week temperature was:
35.9 22.0 27.3 25.5 28.3 31.2 33.7


Read: 3 Types Of Loops in C++

Traversing an Array.

Accessing the elements of an array is called traversing of array.
 
Write a program that read numbers into an array and then searching for an item
 
#include<iostream.h>
#include<conio.h>
int main()
{
int a[10]; // Declare an array of 10 integers
int n=0;
cout<<“Enter 10 integer values”<<endl;
while(n<10)
{
cin>>a[n];
n++;
}
int item; // declare an item to search
cout<<“Enter an item to search”<<endl;
cin>>item;
for(int i=0; i<10; i++)
{
if(item!=a[i]) // comparison
continue’
else
{
cout<<“itm”<<item<<“is found at location”<<i+1;
goto end;
}
}
cout<<“Search unsuccessful:”<<itme<<“is not found”;
end:
getch();
return 0;
}

Sizeof() Operator

The sizeof() operator can be used with arrays to return the size allocated for the entire array. Consider the following program:
 
//size of an array example
#include<iostream.h>
#include<conio.h>
int main()
{
int A[]={0,2,3,4};
cout<<“Size of the array A is:”<<sizeof(A); //5 element * 2 bytes for each element 
getch();
return 0;
}
 
Output of the program
 

Size of the array A is: 10

You may like:

6 Types of C++ Manipulators with Examples