Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C++ language One dimension Array

Posted on December 10, 2016January 29, 2020

Table of Contents

  •  C++ language One dimension Array
      • Knowledge Area
      • Array declaration in C++
      • Initiation of Array
      • Insert and display the element of Array
      • Input and display array elements using loops

 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

C++ language One dimension 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];

C++ language One dimension Array
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

C++ language One dimension Array
Output

 

initialization during declaration

Syntax

data_Type array_name[array_size]={element_list};

Example

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

C++ language One dimension Array
Array initialization
2. float height[4]={84.5, 87.8, 91.5, 74.5};
C++ language One dimension Array
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}
C++ language One dimension Array
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

C++ language One dimension Array
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

C++ language One dimension Array
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

C++ language One dimension Array
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

C++ language One dimension Array
Output

One dim Array in  Java       One dim Array in  C++       One dim Array in  C

Two dim Array in  Java       Two dim Array in  C++      Two dim Array in  C

Three dim Array in  Java  Three dim Array in  C++    Three dim Array in  C

Array programs c++ language

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes