![]() |
| One Dimensional Array in C++ Programming | Complete Beginner Guide |
Single Dimensional Array in C++
Array: A one-dimensional array in c++ is a collection of data of same data types. It can store data of same data types means an integer array can store integer value and character array can store character value. Arrays is allow to store multiple values of the same data type in a single variable. We do not creating separate variables like student1, student2, student3, you can create one array to hold all students. Array can be used to group of data simultaneously. We can not fetch data from array value directly therefore we can use index point value. The indexing of array always start with 0 and index value is always integer value.
C++ Array Declaration Syntax
- Data_type: The array data type of data that the array will hold (int, float, char, etc.).
- Array_name: The name you choose for the array.
- Array_size: The number of elements the array can hold.
int a[5]={10.40,60,80,70}; value=10,40,60,80,70; index=a[0][1][2][3][4][5]; a[0]=10 a[1]=40 a[2]=60 a[3]=80 a[4]=70 |
C++ Array Initialization
Array is the are several ways to initialize arrays. The choice of initialization method depends on the requirements of your C++ program. Here are some common methods for initializing arrays in C++.
C++ Array Initialization After Declaration
- Array_Data_Type: The type of data that the array will hold (integer, float, character etc.).
- Array_Name: The name you choose for the array.
- Array_Size: The number of elements the array can hold. {data1, data2,data3 ... dataN}:
value=10,40,60,80,70; index=a[0][1][2][3][4][5]; a[0]=20 a[1]=40 a[2]=60 a[3]=80 a[4]=90 |
The values to initialize the array with the number of elements inside the curly braces must match the size of the array.
We declare and initialize an integer array numbers with a size of 5 and we provide five values. If you don't specify all the values during initialization. C++ will automatically initialize the remaining elements to zero or garbage for numeric types (integer, float, character) or null characters for character arrays.
Example of Array in C++ Program
| #include<iostream> int main(){ int a[5]={10,40,60,80,70}; cout<<"Value of a[0]:"<<a[0]<<endl; cout<<"Value of a[1]:"<<a[1]<<endl; cout<<"Value of a[2]:"<<a[2]<<endl; cout<<"Value of a[3]:"<<a[3]<<endl; cout<<"Value of a[4]:"<<a[4]<<endl; } ============OUTPUT============ Value of a[0]:10 Value of a[1]:40 Value of a[2]:60 Value of a[3]:80 Value of a[4]:70 |
C++ Compile Time Array Initialization
- This is typically done using constant values or expressions that can be evaluated at compile time.
- Arrays initialized at compile time are often referred to as "static" or "constant" arrays because their values cannot change during program execution.
- Compile-time initialized arrays are placed in the program's data segment or read-only memory, depending on whether they are marked as const.
Print Array Element Using For Loop
int main(){ int a[5]={10,40,60,80,70}; for(int i=0;i<5;i++) { cout<<"Value of a:["<<i<<"]:"<<a[i]<<endl; } } ============OUTPUT============ Value of a[0]:10 Value of a[1]:40 Value of a[2]:60 Value of a[3]:80 Value of a[4]:70 |
Array Length in CPP Function
How To Get Array Length in C++
int main(){ int a[10] = {20, 48, 87, 56, 19,10,89}; int array_size = sizeof(a)/sizeof(a[0]); cout<<"Total Array_Size:"<<array_size; } ============OUTPUT============ Total Array_Size:10 |
C++ Initialize Array at Runtime
How to Store User Input into an Array in C++
int main(){ int a[10],n,i; cout<<"Enter Array Size:"; cin>>n; for(i=0;i<n;i++) { cout<<"Enter Array Element:["<<i<<"]:"; cin>>a[i]; } for(i=0;i<n;i++) { cout<<"Value of a:["<<i<<"]:"<<a[i]<<endl; } } ============OUTPUT============ Enter Array Size:5 Enter Array Element[0]:67 Enter Array Element[1]:86 Enter Array Element[2]:45 Enter Array Element[3]:89 Enter Array Element[4]:43 Value of a[0]:67 Value of a[1]:86 Value of a[2]:45 Value of a[3]:89 Value of a[4]:43 |
Update Array Element in C++
Write a C++ program to modify contents of an integer array
int main(){ int i,a[5] = {20, 48, 87, 56, 19}; for(i=0;i<5;i++){ cout<<"Before Array Element:["<<i<<"]:"<<a[i]<<endl; } a[2]=100; for(i=0;i<5;i++){ cout<<"After Change Array Element:["<<i<<"]:"<<a[i]<<endl; } } ============OUTPUT============ Before Array Element[0]:20 Before Array Element[1]:48 Before Array Element[2]:87 Before Array Element[3]:56 Before Array Element[4]:19 After Change Array Element[0]:20 After Change Array Element[1]:48 After Change Array Element[2]:100 After Change Array Element[3]:56 After Change Array Element[4]:19 |
Conclusion of One Dimensional Array in C++
Conclusion: This is a all about one-Dimensional array in C++ language. In this article cover of every concept of related array. Array program you can understand simple easy method. If your any query or problems array related than you can contact me by email. I can more easy solution provide.

