Breaking

C++ Array and initialization, declaration-programmingworld


                                C++ Arrays

An array is a collection of a variable of the same type that is referenced by a common name.
In C++, arrays consist of a contiguous memory location.
We cannot be changed size and type of arrays after its declaration.


How to declare a C++ Array?

An array definition specifies a variable type and a name along with one more feature size to specify how many data items the array will contain.


type arrayName [ arraySize];


Where type declares the base type of the c++ array, which is the type of each element in the array. The array-name specifies the name with which the array will be referenced and size defines how many elements the array will hold.


int marks [50];


The statement declares an array marks of base type int and which can hold 50 elements.

How to initializing C++ arrays:-

Arrays provide the facility of array initialization at the time of declaration.
The arrays are initialized in the same way as other variables are.

Example:-


int marks[5] = {88, 73,83,59,93};


Other methods to declare an array-


Int marks[ ] = {57,45,35,36,88};


Accessing C++ Array Elements:-

Arrays index starts with 0, which means the first array element is firstly inserted at location index 0, second is at location 1 and so on. We can use this to display the array elements.



#include<iostream.h>
int main()
{
Const int size=50;
float marks[size];

for (int i=0; i<size; i++)     //Loop to read 50 marks.
{

cout << “Enter marks of student”<<i+1<<”\n”;
cin >>marks[i];

}
cout<<”\n”;

for (i=0; i<size; i++)         //Loop to display the elements.
{
cout <<”marks [“<<i<<”]=”<<marks[i]<<”\n”;
}
return 0;

}


Output :


Enter marks of student1
97
Enter marks of student2
76
Enter marks of student3
85
.
.
.to 50 students.

Marks[0] = 97
Marks[1] = 76
Marks[2] = 85
:
:
:
Marks[49]=

Type Of Arrays:-

Arrays are basically two types.

·       Single-Dimensional Arrays
·       Multi-Dimensional Arrays


      1.  Single-Dimensional Arrays:-

The simplest form of an array is called a single dimensional array. Single dimensional arrays, comprised of the finite homogeneous element. 

      2.  Multi-Dimensional Array:-

Multi-dimensional arrays are also known as an array of arrays. The data in a multi-dimensional array is stored in a tabular form.

Example 1: Two Dimensional Array:

#include <iostream>
int main()
{
    int test[3][2] ={{2, 7},{4, 0},{9, 1}
    };

    // using two-dimensional array
        for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 2; ++j)
        {
            cout<< "test[" << i << "][" << j << "] = " << test[i][j] << endl;
        }
    }

    return 0;
}
Output
test[0][0] = 2
test[0][1] = 7
test[1][0] = 4
test[1][1] = 0
test[2][0] = 9
test[2][1] = 1


Calling functions with Array:-

When you call a function with an array name, a pointer to the first element in the array is passed into the function. You can pass an array as an argument to a function just like you pass variable as arguments.


#include<iostream.h>
int main()

{
int age[10];                            //declare array
cout <<”Enter element:\n”;

for(int i=0; i<10; i++)              //initialize array age[10]
cin>>age[i];

display(age);                       //passing age array
return 0;

}
Void display(int a[10])           //receiving parameter
{
for(int i=0; i<10; i++)
cout <<”\n”<<a[i];
}