All Programs are Written and Compiled in Dev C++. So, it may generate some error in case of other compilers and may need some modifications in program. Download Dev C++

Wednesday 26 February 2014

Insertion Of a Row in 2D Array

//Insertion Of a Row in 2D Array
#include<iostream.h>
#include<conio.h>
main()
{
 int i,j,k,m,n;
 int a[10][10];
 int b[10];
 cout<<"Enter Dimensions of array";
 cin>>m>>n;
 cout<<"Enter Elements";
 for(i=1;i<=m;i++)
 {for(j=1;j<=n;j++)
 {cin>>a[i][j];}}
 cout<<"Enter a Row to Insert";
 for(i=1;i<=n;i++)
 {cin>>b[i];}

 cout<<"Enter Row no";
 cin>>k;
 //Shifting
 m=m+1;
 for(j=1;j<=n;j++)
 {for(i=m;i>=k;i--)
 {a[i][j]=a[i-1][j];}}

 //insertion
 for(i=1;i<=m;i++)
 {a[k][i]=b[i];}

 //printing
 for(i=1;i<=m;i++)
 {for(j=1;j<=n;j++)
 {cout<<a[i][j];}
  cout<<endl;}
  getch();
}

No comments:

Post a Comment