Pages

Sunday, 27 October 2013

Arrays in C++

ARRAY IMPLEMENTATION:

Definition of Array:
                                    Collection of elements of slimier Data types is known as Array.
 i.e with in one Name  we can store any number of elements.

Syntax:
Declaration :
                        Datatype Arrayname [size];

                        Ex:int a[5];
                            float b[3];

Initialization : 

               Datatype Arrayname [size]={elements...};
               Ex:-      int a[5]={2,6,8,7,8}; 

Elements
 2
8
7
8
Index
0
1
2
3
4

  • Memory will be allocated continuously for all the elements.
  • Each element is identified with the help of index number.
  • The index of the array starts from ‘0’ and end at ‘size-1’.
  • The size of the array must be constant.
/*3.Write a C++  program to
Demonstrate one Dimensional Array
*/
/*4.Write a C++  program to
Demonstrate one Dimensional
Array using loops*/
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5]={9,4,6,2,5};
clrscr( );
cout<<“\nsizeof a= bytes”<<sizeof(a);
cout<<“\a[0]=“<<a[0];
cout<<“\a[1]=“<<a[1];
cout<<“\a[2]=“<<a[2];
cout<<“\a[3]=“<<a[3];
cout<<“\a[4]=“<<a[4];

getch( );
}


Result:
Output:
Size of a=10 bytes
a[0]=9
a[1]=4
a[2]=6
a[3]=2
a[4]=5
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5]={9,4,6,2,5};
int i;
clrscr( );
cout<<“\nsizeof a= bytes”<<sizeof(a);
for(i=0;i<5;i++)
{
cout<<“\na[“<<i<<”]=“<<a[i];
}

getch( );
}


Result:
Output:
Size of a=10 bytes
a[0]=9
a[1]=4
a[2]=6
a[3]=2
a[4]=5

/*5.Write a C++  program to accept
the 5 integer to an one
 Dimensional Array and Display it
*/
/*5.Write a C++  program to accept
the n integers to an one
Dimensional Array and Display it
*/
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5];
int i;
clrscr( );
for(i=0;i<5;i++)
{
cout<<“\nEnter  Element:”<<i+1;
cin>>a[i];
}
for(i=0;i<5;i++)
{
cout<<“\na[“<<i<<”]=“<<a[i];
}
getch( );
}


Result:
Input:
Enter 1 Element:21
Enter 2 Element:63
Enter 3 Element:52
Enter 4 Element:4
Enter 5 Element:9

Output:
a[0]=21
a[1]=63
a[2]=52
a[3]=4
a[4]=9
#include<iostream.h>
#include<conio.h>
void main()
{
int a[5];
int i,n;
clrscr( );
cout<<“enter the value of n:”;
cin>>n;
for(i=0;i<n;i++)
{
cout<<“\nEnter  Element:”<<i+1;
cin>>a[i];
}
for(i=0;i<n;i++)
{
cout<<“\na[“<<i<<”]=“<<a[i];
}
getch( );
}


Result:
Input:
Enter the value of n:3
Enter 1 Element:2
Enter 2 Element:6
Enter 3 Element:5

Output:
a[0]=2
a[1]=6
a[2]=5


/*Write a C++  program to accept the n integers
Display reverse order */
/*5.Write a C++  program to accept the n integers
Display Ascending order */
#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,n,temp;
clrscr( );
cout<<"Enter the value of n:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter  element:”<<i+1;
cin>>a[i];
}
cout<<"\noriginal order:";
for(i=0;i<n;i++)
{
cout<<"”<<a[i];
}

for(i=0;i<n/2;i++)
{
temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;
}
cout<<"\nReverse order:";
for(i=0;i<n;i++)
{
cout<<"  ”<<a[i];
}
getch( );
}

#include<iostream.h>
#include<conio.h>
void main()
{
int a[20],i,j,n,temp;
clrscr( );
cout<<"Enter the value of n:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\nEnter  element:”<<i+1;
cin>>a[i];
}
cout<<"\noriginal order:";
for(i=0;i<n;i++)
{
cout<<"  ”<<a[i];
}

for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout<<"\nAscending order:";
for(i=0;i<n;i++)
{
cout<<"  ”<<a[i];
}
getch( );
}



Result:

Input:

Enter the value of n:5

Enter 1 element:9

Enter 2 element:6

Enter 3 element:4

Enter 4 element:1

Enter 5 element:3

Output:

original order:  9  6  4  1  3

Reverse order:  3  1  4  6  9

Result:

Input:

Enter the value of n:5

Enter 1 element:21

Enter 2 element:55

Enter 3 element:41

Enter 4 element:78

Enter 5 element:3

Output:

original order: 21 55 41 78  3

Ascending order:  3 21 41 55 78





Two Dimensional Arrays:

Collection of elements of one dimensional Array is known as a two dimensional Array. 

Syntax:

Declaration :

                        Datatype Arrayname [row size][col size];

                        Ex:int a[5][3];

Initialization : 

                Datatype Arrayname [size]={elements...};

            Datatype Arrayname [size]={{row elements},{row elements}...};


Ex:-      int a[3][3]={2,6,8,7,9,1,5,4,3}; 

            int a[3][3]={{2,6,8},{7,9,1},{5,4,3}}; 



columns
r
o
w
s
a
0
1
2
0
2
6
8
1
7
9
1
2
5
4
3

Output: a[0][0]=2, a[0][1]=6…..,a[2][2]=3


/*Write a C++  program to demonstrate two
Dimensional Arrayand display in the form of matrices */
/*Write a C++  program to accept the elements for
two Dimensional Arrayand display in the form
of 3x3 matrices */
#include<iostream.h>
#include<conio.h>
void main()
{
int a[3][3]= {{2,6,8},{7,9,1},{5,4,3}};
int i,j;
clrscr( );
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
{
cout<<"”<<a[i][j];
}
}
getch( );
}

#include<iostream.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
clrscr( );
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
{
cout<<"enter  row  column Element:”<<i+1,j+1;
cin>>a[i][j];
}
}
for(i=0;i<3;i++)
{
cout<<"\n";
for(j=0;j<3;j++)
{
cout<<"  ”<<a[i][j];
}
}getch( );
}


Result:

Output:


2  6  8
7  9  1
5  4  3















Result:

Input:


enter 1 row 1 column Element:5
enter 1 row 2 column Element:9
enter 1 row 3 column Element:4

enter 2 row 1 column Element:3
enter 2 row 2 column Element:2
enter 2 row 3 column Element:1

enter 3 row 1 column Element:4
enter 3 row 2 column Element:6
enter 3 row 3 column Element:3

Output:

5  9  4
3  2  1
4  6  3

/*Write a C++  program to accept the elements for
two Dimensional Arrayand display in the form
of 3x3 matrices */                    
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,r,c;
clrscr( );
cout<<"enter row and column Size:";
cin>>r>>c;
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<"enter  row  column Element:”<<i+1,j+1;
cin>>a[i][j];
}
}
for(i=0;i<r;i++)
{
cout<<"\n";
for(j=0;j<c;j++)
{
cout<<"  ”<<a[i][j];
}
}
getch( );
}


Result:

Input:
enter row and column Size:2
3

enter 1 row 1 column Element:8
enter 1 row 2 column Element:5
enter 1 row 3 column Element:4

enter 2 row 1 column Element:1
enter 2 row 2 column Element:9
enter 2 row 3 column Element:6

  8  5  4
  1  9  6
/*Write a C++  program to find sum of  two
matrices */
/*Write a C++  program to find product of  two
matrices */
#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int r1,c1,r2,c2,r3,c3,i,j;
clrscr( );
cout<<"\nEnter row and column Size Matrix A:";
cin>>r1>>c1;
cout<<"\nEnter row and column Size Matrix B:";
cin>>r2>>c2;
if(r1==r2&&c1==c2)
{
cout<<"\nenter elements  for Matrix A:";
for(i=0;i<r1;i++)
{
cout<<"\n";
for(j=0;j<c1;j++)
{
cout<<"enter  row  column Element:”<<i+1,j+1;
cin>>a[i][j];
}
}
cout<<"\nenter elements  for Matrix B:";
for(i=0;i<r2;i++)
{
cout<<"\n";
for(j=0;j<c2;j++)
{
cout<<"enter  row  column Element:”<<i+1,j+1;
cin>>b[i][j];
}
}
r3=r1;//r3=r2;
c3=c1;//c3=c2;
for(i=0;i<r3;i++)
{
for(j=0;j<c3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\nMatrix A:";
for(i=0;i<r1;i++)
{
cout<<"\n";
for(j=0;j<c1;j++)
{
cout<<"  ”<<a[i][j];
}
}
cout<<"\nMatrix B:";
for(i=0;i<r2;i++)
{
cout<<"\n";
for(j=0;j<c2;j++)
{
cout<<"  ”<<b[i][j];
}
}
cout<<"\nAddition of Matrix A and B:";
for(i=0;i<r3;i++)
{
cout<<"\n";
for(j=0;j<c3;j++)
{
cout<<"  ”<<c[i][j];
}
}


}
else
{
cout<<"\nMatrix Addition is not possible";
}
getch( );
}

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int r1,c1,r2,c2,r3,c3,i,j,k;
clrscr( );
cout<<"\nEnter row and column Size of Matrix A:";
cin>>r1>>c1;
cout<<"\nEnter row and column Size of Matrix B:";
cin>>r2>>c2;
if(c1==r2)
{
cout<<"\nenter Element for Matrix A:";
for(i=0;i<r1;i++)
{
cout<<"\n";
for(j=0;j<c1;j++)
{
cout<<"enter  row  column Element:”<<i+1,j+1;
cin>>a[i][j];
}
}
cout<<"\nenter Element for Matrix B:";
for(i=0;i<r2;i++)
{
cout<<"\n";
for(j=0;j<c2;j++)
{
cout<<"enter  row  column Element:”<<i+1,j+1;
cin>>b[i][j];
}
}
r3=r1;
c3=c2;
for(i=0;i<r3;i++)
{
for(j=0;j<c3;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
cout<<"\nMatrix A:";
for(i=0;i<r1;i++)
{
cout<<"\n";
for(j=0;j<c1;j++)
{
cout<<"  ”<<a[i][j];
}
}
cout<<"\nMatrix B:";
for(i=0;i<r2;i++)
{
cout<<"\n";
for(j=0;j<c2;j++)
{
cout<<"  ”<<b[i][j];
}
}
cout<<"\nproduct of Matrix A and B:";
for(i=0;i<r3;i++)
{
cout<<"\n";
for(j=0;j<c3;j++)
{
cout<<"  ”<<c[i][j];
}
}


}
else
{
cout<<"\nMatrix product is not possible";
}
getch( );
}


Result:
Input:

Enter row and column Size Matrix A:2
2

enter elements for Matrix A:
enter 1 row 1 column Element:1
enter 1 row 2 column Element:2

enter 2 row 1 column Element:3
enter 2 row 2 column Element:4

enter elements for Matrix B:
enter 1 row 1 column Element:5
enter 1 row 2 column Element:6

enter 2 row 1 column Element:7
enter 2 row 2 column Element:8

Output:

Matrix A:
1  2
3  4
Matrix B:
5  6
7  8
Addition of Matrix A and B:
6  8
10 12

Result:
Input:

Enter row and column Size of Matrix A:2
2

Enter row and column Size of Matrix B:2
2

enter element for Matrix A:
enter 1 row 1 column element:1
enter 1 row 2 column element:1

enter 2 row 1 column element:1
enter 2 row 2 column element:1

enter element for Matrix B:
enter 1 row 1 column element:1
enter 1 row 2 column element:1

enter 2 row 1 column element:1
enter 2 row 2 column element:1

Output:
Matrix A:
1  1
1  1
Matrix B:
1  1
1  1
product of Matrix A and B:
2  2
2  2

No comments:

Post a Comment