Table of Contents
C++ language One dimension Array
In this tutorial, we will discuss the One dimension Array in C++ language
Knowledge Area
- What is Array
- Type of Arrays
- Declaration of one dimension Array
- Array Initialization
- Array processing
One of the data structure in C++, The Array helps a serious of the variable (when we need continuously data type accessed) of the same type of element to placed in the memory location.
Array index- It is a unique identifier used to individually referenced memory location.
Three types of arrays in C programming language
1. One dimensional array
2. Two-dimensional array
3. multidimensional array
One dimension Array in C++
Array declaration in C++
When programmers declare the array, Two important factors are required by an array as follows
- Type of the element
- Number of element
Data_type Array_name[elements];
eg:
int marks[30];
float student height[45];
char alphabet[26];
Array Declaration
int marks[6];
Initiation of Array
initialization – Using standard method
marks[0]=45;//initialize element to marks[0] index
Example
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[5];//declaration of one D array //initializing one D array arr[0]=45;//store first element arr[1]=57; arr[2]=97; arr[3]=68; arr[4]=68;//store forth element //Displaying element from array cout << "Element of arr[0]= "<<arr[0] << endl; cout << "Element of arr[1]= "<<arr[1] << endl; cout << "Element of arr[2]= "<<arr[2] << endl; cout << "Element of arr[3]= "<<arr[3] << endl; cout << "Element of arr[4]= "<<arr[4] << endl; getch(); return 0; }
When the above code is executed it produces the following result
initialization during declaration
Syntax
data_Type array_name[array_size]={element_list};
Example
1. int marks[6]={56, 67, 78, 89, 63, 36};
Insert and display the element of Array
marks[4]=7;//insert value to forth index
cin>>marks[2];//store value to second index
cin>>marks[i];
cout<<marks[0];//display value from zero index
cout<<marks(i-1);
#include <iostream> using namespace std; int main() { int marks[5]={17,56,47,53,78}; cout << marks[0] << endl; cout << marks[1] << endl; cout << marks[2] << endl; cout << marks[3] << endl; cout << marks[4] << endl; return 0; }
When the above code is executed it produces the following result
Input and display array elements using loops
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[10],i; cout << "Enter the 10 numbers for array" << endl; for(i=0; i<10; i++){ cin>>arr[i];//store array elements } cout << "Your entered elements display here" << endl; for(i=0; i<10; i++){ cout << "you entered to index arr["<<i<<"]: "<<arr[i] <<endl; } getch(); return 0; }
When the above code is executed it produces the following result
#include <iostream> #include <conio.h> using namespace std; int main() { int arr[10],i; cout << "Enter the 10 numbers for array" << endl; i=0; while(i<10){ cin>>arr[i];//store array elements i++; } cout << "Your entered elements display here" << endl; i=0; while(i<10){ cout << "you entered to index arr["<<i<<"]: "<<arr[i] <<endl; i++; } getch(); return 0; }
When the above code is executed it produces the following result
Using do-while loop
using namespace std; int main() { int arr[10],i; cout << "Enter the 10 numbers for array" << endl; i=0; do{ cin>>arr[i];//store array elements i++; }while(i<10); cout << "\nYour entered elements display here\n" << endl; i=0; do{ cout << "you entered to index arr["<<i<<"]: "<<arr[i] <<endl; i++; }while(i<10); getch(); return 0; }
When the above code is executed it produces the following result