Single Dimensional Array Example Program in C++ Programming in C++
On this page (9sections)
About this program
This is an example program in c array example programs. Read the concept first: Understanding Array in C++, then study the code and output below.
Definition
An array is a collection of data that holds homogeneous values. That means values should be in same type
Syntax
type variable_name[size]
Details of Array
int varName[10];
Here, varName has 10 containers,
varName[0], varName[1] to varName[9]
For example,
Array values stores like below structure,
int value[6] = {5,10,15,20,25,30};
Value[0] = 5
Value[1] : 10
Value[2] : 15
Value[3] : 20
Value[4] : 25
Value[5] : 30
Example Program
/* Example Program For Single Dimensional Array In C++ Programming Language
Array Example In C++*/
// Header Files
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{
int i;
// declaring and Initialising array in C
int value[6] = {5,10,15,20,25,30};
cout<<"Single Dimensional Array In C++ Example Program\n";
for (i=0;i<6;i++)
{
// Accessing each variable using for loop
cout<<"Position : "<<i<<" , Value : "<< value[i]<<" \n";
}
// Wait For Output Screen
getch();
//Main Function return Statement
return 0;
}
Sample Output
Position : [0] , Value : 5
Position : [1] , Value : 10
Position : [2] , Value : 15
Position : [3] , Value : 20
Position : [4] , Value : 25
Position : [5] , Value : 30
Related Pages
Learn the concept first, then study the code:
- C++ Programs — Browse all C++ Programs.
- Understanding Array in C++ — Tutorial — arrays, indexing and multidimensional arrays.
- Sum of Array C++ Example Program — More in c array example programs.
- Read Array and Print Array C++ Example Program — More in c array example programs.
Frequently Asked Questions
What does this C++ program do?
It is a C++ example program that demonstrates Single Dimensional Array in Programming, including the complete source code and the expected sample output.
How do I compile and run this C++ program?
Save the code in a `.cpp` file, compile it with `g++ filename.cpp -o program`, then run it with `./program` (or `program.exe` on Windows).
What concepts does this example use?
This example uses loops to iterate over data and arrays, illustrating a common pattern in C++ programming.