You are on page 1of 6

2D ARRAY SAMPLE PROGRAM

1.
#include<iostream>
#include<conio.h>

using namespace std;

main()
{
int matrix [2][3];
// For taking integer inputs in a matrix //
for (int m1=0 ; m1<2 ; m1++){
for (int m2=0 ; m2<3 ; m2++){
cout<<"Enter Integer :";
cin>>matrix [m1][m2];
}
}

cout<<endl;

cout<<endl<<"Original 2D Array"<<endl;

for (int row=0 ; row<2 ; row++){


for (int col=0 ; col<3 ; col++){
cout<<matrix [row][col]<<" ";
}
cout<<endl;
}

matrix[0][0]=0;matrix[1][1]=0;matrix[0][2]=0;

cout<<endl<<"Updated 2D Array"<<endl;
for (int row=0 ; row<2 ; row++){
for (int col=0 ; col<3 ; col++){
cout<<matrix [row][col]<<" ";
}
cout<<endl;
}

}
2.
#include<iostream>
#include<conio.h>

using namespace std;

main()
{
int matrix [2][3];
// For taking integer inputs in a matrix //
for (int m1=0 ; m1<2 ; m1++){
for (int m2=0 ; m2<3 ; m2++){
cout<<"Enter Integer :";
cin>>matrix [m1][m2];
}
}

cout<<endl;

// For displaying elements of a matrix on a screen //


for (int r=0 ; r<2 ; r++){
for (int c=0 ; c<3 ; c++){
cout<<"Your Entered Integer are "<<r<<","<<c<<": ";
cout<<matrix [r][c];
cout<<endl;
}
}

cout<<endl;

for (int row=0 ; row<2 ; row++){


for (int col=0 ; col<3 ; col++){
cout<<matrix [row][col]<<" ";
}
cout<<endl;
}

getch();
}
3.
#include<iostream>
#include<conio.h>

using namespace std;

int search(int matrix[3][3],int);

main(){
int matrix [3][3]={{1,2,3},{2,4,6},{3,2,1}};
int sv;

cout<<"enter search value: ";


cin>>sv;
//int found = search(matrix,sv);
int found = search(matrix,sv);
if(found==0)
cout<<"Not Found!"<<endl;

int search(int matrix[3][3],int key){


int found=0;
for (int row=0 ; row<3 ; row++){
for (int col=0 ; col<3 ; col++){
cout<<matrix [row][col]<<" ";
}
cout<<endl;
}

for(int r=0; r<3; r++){


for(int c=0;c<3;c++){
if(matrix[r][c]==key){
cout<<"Found in: "<<r<<","<<c<<endl;
found=1;
}
}
}
return found;
}
4.
#include<iostream>
#include<conio.h>
#include<windows.h>

using namespace std;

void gotoxy(short a, short b); //function declaration

main()
{
//declaration and initialization
int matrix [2][3]={1,5,3,7,5,6};
// For displaying elements of a matrix on a screen //
for (int r=0 ; r<2 ; r++){
for (int c=0 ; c<3 ; c++){
cout<<"Your Entered Integer are :";
cout<<matrix [r][c];
cout<<endl;
}
}
getch();
cout<<endl;

for (int row=0 ; row<2 ; row++){


for (int col=0 ; col<3 ; col++){
cout<<matrix [row][col]<<" ";
}
cout<<endl;
}
getch();
for (int row=0 ; row<2 ; row++){
for (int col=0 ; col<3 ; col++){
gotoxy(10 + 5*(col+1),10 +(row+1)*2);cout<<matrix [row][col];
}
cout<<endl;
}

getch();
}

void gotoxy(short a, short b) //Custom gotoxy() function


{
COORD coordinates; //Data type of co-ordinates
coordinates.X = a; //Assign value to X- Co-ordinate
coordinates.Y = b; //Assign value to Y Co-ordinate

SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE), coordinates);

}
5.
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int m1[5][5], m2[5][5], m3[5][5];
int i, j, r, c;

cout<<"Enter the no.of rows of the matrices to be added(max 5):";


cin>>r;
cout<<"Enter the no.of columns of the matrices to be added(max 5):";
cin>>c;

cout<<"\n1st Matrix Input:\n";


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\nmatrix1["<<i<<"]["<<j<<"]= ";
cin>>m1[i][j];
}
}

cout<<"\n2nd Matrix Input:\n";


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\nmatrix2["<<i<<"]["<<j<<"]= ";
cin>>m2[i][j];
}
}
//display matrix 1
cout<<"Matrix 1"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++)
{
cout<<m1[i][j]<<" ";
}
cout<<endl;
}
//display matrix 2
cout<<"Matrix 2"<<endl;
for(i=0;i<r;i++){
for(j=0;j<c;j++)
{
cout<<m2[i][j]<<" ";
}
cout<<endl;
}

getch();
cout<<"\nAdding Matrices...\n";
getch();

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}
cout<<"\nThe resultant Matrix is:\n";

for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
cout<<"\t"<<m3[i][j];
}
cout<<endl;
}

}s

You might also like