Categories: ArrayC++Learn C++

C++ language One dimension Array

 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 D 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
Syntax

Data_type Array_name[elements];

eg:
int marks[30];
float student height[45];
char alphabet[26];

Array Declaration

int marks[6];

Array declaration

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

Output

 

initialization during declaration

Syntax

data_Type array_name[array_size]={element_list};

Example

1. int marks[6]={56, 67, 78, 89, 63, 36};

Array initialization
2. float height[4]={84.5, 87.8, 91.5, 74.5};
Array initialization
Explanation of initialization an array
method 1
int marks[]={19,10,45,67,78,98}
method 2
int marks[6]={19,10,45,67,78,98}
Explanation
19 is placed to marks[0];
10 is placed to marks[1];
45 is placed to marks[2];
67 is placed to marks[3];
78 is placed to marks[4];
98 is placed to marks[5];

Insert and display the element of Array

replace a different value or insert the value to the fifth element in array length
marks[4]=7;//insert value to forth index
get input from the user and insert the element of third place in array length
cin>>marks[2];//store value to second index
get input from the user and insert the element of (i+1) in array length
cin>>marks[i];
print first element of an array
cout<<marks[0];//display value from zero index
print (I)th element of an array
cout<<marks(i-1); 
Example 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

Output

Input and display array elements using loops

Using for loop
#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

Output
Using while loop
#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

Output

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

Output
Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

9 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

10 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

10 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

10 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

10 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

10 months ago

This website uses cookies.