Table of Contents
In this tutorial, we will discuss the One dimension Array in C++ language
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++
When programmers declare the array, Two important factors are required by an array as follows
Data_type Array_name[elements];
eg:
int marks[30];
float student height[45];
char alphabet[26];
Array Declaration
int marks[6];
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};
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
#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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.