You are on page 1of 9

2D Arrays Assignment

Date of Submission : 23 December 2015

Anushka Sharma XI-A

Q1) Input a 3 x 3 array.


a) Print the sum of diagonal elements( both the diagonals).
b) Initialize all the elements except left to right diagonal to 0. Set elements of left to right
diagonal to -1.
c) Display the array.
Q2) Input a 3 x 4 array and
Player

Match 1

Match 2

Match 3

Match 4

Bharat
Kumar

86

98

34

56

Sanjeev
Singh

77

34

56

66

Rakesh
Singh

78

87

a)
b)
c)
d)
e)
f)
g)

Display the total scores earned by each player along with name of each player.
Display the total scores earned in each match along with Match number.
Display the highest score in the whole 2D array.
Display highest and lowest score of each player along with players name.
Display average score of each player along with players name.
Initialize the Match4 scores of each player to 0
Initialize the score in the entire 2D array to 40 if it is below 40.

Q3) Input 2 two d arrays ( each 3 x 4) . Display their sum.


Q4) Input 2 two d arrays ( each 3 x 4) . Display their Product.
Q5) Input the array mentioned in Q2) and store it in a 1D array, sort it and display it.
**Q6) Input a 4 x 3 array. Generate another 4 x 3 array that should have the rows of original
array sorted in ascending order.
Q7) Input a 3 x 5 array. Display the elements of middle row and middle column.
Q8) Input a 3 x 3 array. Replace all elements with 0 except the left to right diagonal. The left
to right diagonal elements should be replaced with 1. Finally display the 2d (new) array

Program 1:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int sum=0, sum1=0,i,j,a[3][3];
cout<<"\n enter array:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ cin>>a[i][j];}
}
cout<<"\n sum of diagonals left to right:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{if(i==j)
sum=sum+a[i][i];}
}
cout<<sum;

cout<<"\n sum of diagonals right to left :";


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{if(i==j)
sum1=sum1+a[i][2-i];}
}
cout<<sum1;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ if(i==j)
a[i][i]=-1;
else a[i][j]=0;}
}
cout<<"\n new array:"<<"\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)

{cout<<a[i][j]<<" ";}
cout<<"\n";}
getch();
}
Program 2:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
void main()
{
clrscr();
int i,j,t[3],a[3][5],m[4],h[3],l[3],max=0;
t[1]=t[0]=t[2]=m[0]=m[1]=m[2]=m[3]=h[0]=h[1]=h[2]=l[0]=l[1]=l[2]=0;
char n1[40],n2[40],n3[40];
cout<<"\nEnter the names of three players:";
gets(n1);
gets(n2);
gets(n3);
cout<<"\nEnter the scores of the players in 4 matches:";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cin>>a[i][j];
if(a[i][j]>max)
max=a[i][j];
t[i]=t[i]+a[i][j];
}
}
cout<<"\nPlayers
";
cout<<n1<<" "<<n2<<" "<<n3<<"\n"<<"Total Scores
";
for(i=0;i<3;i++)
{cout<<t[i]<<" ";}
for(j=0;j<4;j++)
{
for(i=0;i<3;i++)
{ m[j]=m[j]+a[i][j];}
}
cout<<"\nMatches
Match1 Match2 Match3 Match4"<<"\nTotal scores ";
for(i=0;i<4;i++)
cout<<m[i]<<"
";
h[0]=a[0][0]=l[0];

h[1]=a[1][0]=l[1];
h[2]=a[2][0]=l[2];
cout<<"\nHighest score:"<<max;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{if(a[i][j]>h[i])
h[i]=a[i][j];
if(a[i][j]<l[i])
l[i]=a[i][j];
}
}
cout<<"\nName
Highest Lowest Average";
cout<<"\n"<<n1<<" "<<h[0]<<" "<<l[0]<<"
cout<<"\n"<<n2<<" "<<h[1]<<" "<<l[1]<<"
cout<<"\n"<<n3<<" "<<h[2]<<" "<<l[2]<<"
for(i=0;i<3;i++)
{
a[i][3]=0;
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
if(a[i][j]<40)
a[i][j]=40;
}}
getch();
Program3: #include<iostream.h>

"<<t[0]/4;
"<<t[1]/4;
"<<t[2]/4;

#include<conio.h>
void main()
{clrscr();
int a[3][4],b[3][4],c[3][4],i,j;
cout<<"\n array 'a':";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cin>>a[i][j];
}
}
cout<<"\n array 'b':";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cin>>b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
cout<<"\n array 'c' after adding array 'a' and 'b' is:";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
getch();}

Program 4; #include<iostream.h>
#include<conio.h>
void main()

{clrscr();
int a[3][4],b[3][4],c[3][4],i,j;
cout<<"\n array 'a':";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cin>>a[i][j];
}
}
cout<<"\n array 'b':";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cin>>b[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
c[i][j]=a[i][j]*b[i][j];
}
}
cout<<"\n array 'c' after multiplying array 'a' and 'b' is:";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cout<<c[i][j]<<" ";
}
cout<<"\n";
}
getch();
}

Program 5; #include<iostream.h>
#include<conio.h>
void main()
{

clrscr();
int a[3][4],t=-1,i,j,x[12];
cout<<"\n enter an array:";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cin>>a[i][j];
t=t+1;
x[t]=a[i][j];
}
}
cout<<" \n array in 1d is:";

for(t=0;t<12;t++)
{
cout<<x[t];
}
getch();
}

Program 6; #include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int i,j,a[4][3],c,t;
cout<<"\n enter an array:";
for(i=0;i<4;i++)
{for(j=0;j<3;j++)
{cin>>a[i][j];
}}
cout<<"\n new array is:";
for(i=0;i<=3;i++)
{
for(c=0;c<3;c++)
{
for(j=c+1;j<3;j++)
{
if(a[i][c]>a[i][j])
{
t=a[i][c];

a[i][c]=a[i][j];
a[i][j]=t;
}}}}
for(i=0;i<=3;i++)
{
for(j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
}
getch();}
Program 7; #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,c=0,z=0;
int d[15],a[3][5];
cout<<"\n enter array:";
for (i=0;i<3;i++)
{ for(j=0;j<5;j++)
{
cin>>a[i][j];
}
}
cout<<"\n middle row is:";
for(j=0;j<5;j++)
{
d[c+i]=a[1][j];
cout<<d[c+i]<<" ";
}
cout<<"\n middle column is:";
for(i=0;i<=2;i++)
{
d[z+i]=a[i][2];
cout<<d[z+i]<<" ";
}
getch();
}

Program 8; #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3],i,j;
cout<<"\n enter an array:";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{cin>>a[i][j];
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ if(i==j)
a[i][i]=1;
else a[i][j]=0;}
}
cout<<"\n new array:"<<"\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{cout<<a[i][j]<<" ";}
cout<<"\n";}
getch();
}

You might also like