Array

Single dimension Array in Cpp language

Single dimension Array in Cpp language

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..

Single dimension Array in Cpp

 

Array declaration, creation, initiation

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];
Single dimension Array declaration in C++

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];
n number of elements Array

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

 

Array Initiation in Cpp

Int marks[6]={45,76,56,87,43,67}//Array declaration and initiation
Int Array

 

float Height[6]={45,76,56,87,43,67}

float array

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,}

initialization of array

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]

 

Insert the element of an array

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];

Retrieve or display elements

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

 

array in C++ language access element using for loop

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

 

array in C++ language access element using while loop

 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

 

Suggested for you

for loop in C++ language

While loop in C++ language

Nested for loop in C++ language

Nested while loop in C++ language

Two dimension Array in Cpp language
Two Dimensional Array in Java language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

5 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago