We will get to know about Single Dimension Array in Cpp language in this tutorial.
An array is a special type of data structure in C++ programming language for storing a fixed-size sequential collection of elements of the same data type as int, char, float etc…
The single dimensional array is a type of Array data type used to store the collection of the same type of element such as int or string or char etc..
Declaration of array
Syntax
Data_type Array_Name[Array_size];
Example
int marks[30]; float student_height[40]; char alphabets[26];
When declaring an array in C++, what does in memory
int marks[6];
When we declare an array, memory allocates the fixed size of space according to the declaration of the array.
In the above example, we declared an array size of 6 as the integer. So, memory allocates six places for array data.
Declare number of n element array
data_type array_name[n]; int age[n];
Here, we declared an array named salary of double type and its size is equal to n. Its means allocation of the only n double(similar data type) value memory space.
Note – The size and type can not be changed after its declaration
Int marks[6]={45,76,56,87,43,67}//Array declaration and initiation
float Height[6]={45,76,56,87,43,67}
Always, Array index starts from 0 not 1
Array index is ends in n-1(n is equal to array size or array length)
Array size is starts from 1 and ends in n(n is array length)
Ways of initialization of an Array
method 1
int marks[5]={34,65,78,98,43,}
method 2
int marks[]={34,65,78,98,43,}
34 placed to marks[0]
65 placed to marks[1]
78 placed to marks[2]
98 placed to marks[3]
43 placed to marks[4]
method 1
Insert value
replace a value or insert a value for n th element in array length
marks[n-1]=34
Example
insert 5th element
marks[4]=45; //insert 45 to 4th index of array - 5th element
get input from the user and insert element to the nth value of array length
cin>>marks[n-1]; //array index n-1
Example
insert 3rd element
cin>>marks[2];
print nth element of an array
cout<<marks[n-1] //this is nth element of an marks array
Example
print first element
cout<<marks[0] //this is 1st element of an marks array
Example program
program 1
#include <iostream> using namespace std; int main() { int marks[6]={34,64,38,9,65,43}; //Declaration of an array cout<<"print first marks :"<<marks[0]<<endl; cout<<"print second marks :"<<marks[1]<<endl; cout<<"print third marks :"<<marks[2]<<endl; cout<<"print forth marks :"<<marks[3]<<endl; cout<<"print fifth marks :"<<marks[4]<<endl; cout<<"print sixth marks :"<<marks[5]<<endl; //print every element from array index 0- 5 cout << "End of program"<< endl; return 0; }
When the above code is compiled and executed, it will produce the following results
print first marks 34 print second marks 64 print third marks 38 print forth marks 97 print fifth marks 65 print sixth marks 33
program 2
#include <iostream> using namespace std; int main() { int marks[6]; //Declaration of an array marks[0]=56; marks[1]=86; marks[2]=57; marks[3]=93; marks[4]=38; marks[5]=74; //initialization every element in array cout<<"print first marks :"<<marks[0]<<endl; cout<<"print second marks :"<<marks[1]<<endl; cout<<"print third marks :"<<marks[2]<<endl; cout<<"print forth marks :"<<marks[3]<<endl; cout<<"print fifth marks :"<<marks[4]<<endl; cout<<"print sixth marks :"<<marks[5]<<endl; //print every element from array index 0- 5 cout << "End of program"<< endl; return 0; }
When the above code is compiled and executed, it will produce the following results
print first marks: 56
print second marks: 86
print third marks: 57
print forth marks: 93
print fifth marks: 38
print sixth marks: 74
insert element using for loop
for(i=0; i<=10; i++){ cin>>array_name[i]; }
print element using for loop in C++
for(j=0; j<=10; j++){ cout<<array_name[j]; }
program 1
#include <iostream> #include <conio.h> using namespace std; int main() { int marks[4]={56,87,98,34}; int i; for(i=0; i<=3; i++){ cout << "array index is :" <<i<<"value is :"<<marks[i]<< endl; } getch(); return 0; }
When the above code is compiled and executed, it will produce the following results
array index is :0 value is 56
array index is: 1 value is 87
array index is: 2 value is 98
array index is :3 value is 34
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { int marks[9];//declaring array of five integer in C++ int i,j; //declare i as a integer variable to for-loop //initializing elements of array using for-loop for(i=0; i<=8;i++){ cout<<"Enter marks for students "<<i<<" : "; //ask input for array cin>>marks[i]; } cout<<"\n"<<i<<" Student marks display here"<<endl; //printing values of array using for-loop for(j=0; j<=8;j++){ cout << "array index is :"<<j<<" value is :"<<marks[j]<< endl; } getch(); return 0; }
When the above code is compiled and executed, it will produce the following results
Enter marks for students 0 : 34 Enter marks for students 1 : 67 Enter marks for students 2 : 54 Enter marks for students 3 : 37 Enter marks for students 4 : 86 Enter marks for students 5 : 95 Enter marks for students 6 : 26 Enter marks for students 7 : 70 Enter marks for students 8 : 90 9 student marks display here array index is :0 value is :34 array index is :1 value is :67 array index is :2 value is :54 array index is :3 value is :37 array index is :4 value is :86 array index is :5 value is :95 array index is :6 value is :26 array index is :7 value is :70 array index is :8 value is :90
insert element using while loop in C++
while(i<=10){ cin>>array_name[i]; }
Display element using while loop
while(i<=10){ cout<<array_name[i]; }
program 1
#include <iostream> #include<conio.h> using namespace std; int main() { int stu_marks[10]={16,87,53,69,59,27,65,38,97,99}; //Array declaring and initializing in C++ cout<<"\nyour marks here"<< endl; int j;//declaring a variable as integer j=0; while(j<=9){//Accessing array elements using while loop cout << "Array index is :"<<j<<" value is :"<<stu_marks[j]<< endl; j++;//increment statements } getch(); return 0; }
When the above code is compiled and executed, it will produce the following results
your marks here Array index is : 0 value is : 16 Array index is : 1 value is : 87 Array index is : 2 value is : 53 Array index is : 3 value is : 69 Array index is : 4 value is : 59 Array index is : 5 value is : 27 Array index is : 6 value is : 65 Array index is : 7 value is : 38 Array index is : 8 value is : 97 Array index is : 9 value is : 99
Program 2
#include <iostream> #include<conio.h> using namespace std; int main() { int stu_marks[10]; //Array declaring and initializing in C++ int i;//declaring a variable as integer i=0; while(i<=9){//Accessing array elements using while loop cout<<"Enter your marks for index : "<<i<<" "; cin >>stu_marks[i];//input value from user i++;//increment statements } cout<<"\nyour marks here"<< endl; int j;//declaring a variable as integer j=0; while(j<=9){//Accessing array elements using while loop cout << "Array index is :"<<j<<" value is :"<<stu_marks[j]<< endl; j++;//increment statements } getch(); return 0; }
When the above code is compiled and executed, it will produce the following results
Enter your marks for index : 0 45 Enter your marks for index : 0 65 Enter your marks for index : 0 44 Enter your marks for index : 0 55 Enter your marks for index : 0 34 Enter your marks for index : 0 67 Enter your marks for index : 0 68 Enter your marks for index : 0 98 Enter your marks for index : 0 86 Enter your marks for index : 0 69 your marks here Array index is : 0 value is : 45 Array index is : 1 value is : 65 Array index is : 2 value is : 44 Array index is : 3 value is : 55 Array index is : 4 value is : 34 Array index is : 5 value is : 67 Array index is : 6 value is : 68 Array index is : 7 value is : 98 Array index is : 8 value is : 86 Array index is : 9 value is : 69
Similar post
Two dimension Array in C++ language
Three dimension Array in C++ language
Single dimension Array in Java language
Two dimension Array in Java language
Three dimension Array in Java language
Single dimension Array in C language
Two dimension Array in C language
Three dimension Array in C language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…