C++ ARRAYS


ARRAY

An array is a collection of elements(variables) of similar types that are referenced by a common name.

All the elements in an array are stored sequentially (contiguous memory locations), and each element of array behaves like a normal variable.

Square brackets ([]) are used to index array values.

NEED OF ARRAY

Ordinary variables store one value at a time, whereas arrays are used for storing more than one values at a time under a single variable name.

Arrays are useful in case where we need to store multiple values having the same datatype. For instance, to store marks of 50 students.

ARRAY DECLARATION 

                               Datatype array_name [size];
                     
                                     int  marks [50];

TYPES OF ARRAY

1. One dimensional array
2. Multidimensional array

PROGRAM

//Program to demonstrate how to input the elements of an array

# include <iostream>

using namespace std;

int main()
{
int a[10],i;

cout<<"ENTER THE ELEMENTS OF ARRAY:\n";

for(i=0;i<10;i++)            //Loop For Input
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
cout<<endl;

      for(i=0;i<10;i++)           //Loop For Output
{
cout<<"a["<<i<<"]=";
cout<<a[i]<<endl;
}
 
return 0;
}

OUTPUT



Share this

Related Posts

FIND US ON FACEBOOK!