Saturday, June 17, 2023

C ++ Array

 

ARRAY

ARRAYS

          Array is a collection of similar data type stored in continuous Memory location.

 

Syntax

          Data-type variable-name[size];

Example

          int a[10];     //declaration

Array Initialization

Syntax

data-type  variable-name[size]={value1,……..value n};

Example

int a[5]={1,2,3,4,5};

Types of arrays

ZSingle or one-dimensional array

ZMulti-dimensional or Two-dimensional array

 

 

Single or 1-D  Array

#include<iostream.h>

#include<conio.h>

void main()

{

int a[5],i;

clrscr();

for(i=0;i<5;i++)

{

cin>>a[i];

}

for(i=0;i<5;i++)

{

cout<<a[i]<<endl;

}

getch();

}

Output 



 

 

Two-Dimensional Array

Syntax

datatype variablename[rowsize][column size]={value};

        Column=j

 

0

1

 Row=i                 0

1

2

         1

3

4

          

  int a[2][2]={{1,2},{3,4}};



#include<iostream.h>

#include<conio.h>

void main()

{

int i,j,a[2][2];

clrscr();

cout<<"Enter the values \n";

for(i=0;i<2;i++)

{

for(j=0;j<2;j++)

{

cin>>a[i][j];

}

}

cout<<"\n Matrix a \n";

for(i=0;i<2;i++)

{

for(j=0;j<2;j++)

{

cout<<a[i][j]<<"\t";

}

cout<<endl;

}

getch();

}

Output 

 



0 comments:

Post a Comment