Array Functions (2D Array) : Each Questions Carry
Array Functions (2D Array) : Each Questions Carry
Array Functions (2D Array) : Each Questions Carry
1. Write a function SumOfAlternate( ) in C++ to find and return the sum of elements from all alternate
elements of a two dimensional array passed as argument with size, starting from [0][0].
For eg
If the following is the content of the array
The function should add elements B[0][0], B[0][2], B[1][1], B[2][0] and B[2][2].
Ans:-
return sum;
}
2. Write a function in C++ to find the sum of both left and right diagonal elements from a two
dimensional array (matrix).
Answer:
void DiagSum(int A[100][100],int N)
{
int SumD1=0,SumD2=0;
for (int I=0;I<N;I++)
{
SumD1+=A[I][I];
SumD2+=A[N-I-1][I];
}
cout<<”Sum of Diagonal 1:”<<SumD1<<endl;
cout<<”Sum of Diagonal 2:”<<SumD2<<endl;
}
3. Write a function in C++ to find sum of rows from a two dimensional array.
Answer:
void MatAdd(int A[100][100],int N,int M)
{
for (int R=0;R<N;R++)
{
int SumR=0;
for (int C=0;C<M;C++)
SumR+=A[R][C];
cout<<SumR<<endl;
}}
4. Write a function in C++ to find sum of columns from a two dimensional array
Ans:
5. Write a function ALTERNATE (int A[ ][3], int N, int M) in C++ to display all alternate elements from
two dimensional array A (starting from A [0][0]).
For example:
If the array is containing:
23 54 76
37 19 28
62 13 19
The output will be: 23 76 19 62 19
Ans:-
void ALTERNATE (int A [ ][3], int N, int M)
{ int c=0;
for(int i=0;i<N;++i)
{ for(int j=0;j<M;j++)
{ if(c%2==0)
cout<< A[i][j]<< “ “;
c++;
}
}
}
6. Write a function in C++ to print the product of each column of a two dimensional array
passed as the arguments of the function.
Example : If the two dimensional array contains
Then the output should appear as:
Product of Column 1 = 24
Product of Column 2 = 30
Product of Column 3 =240
Ans:
void productcol(int A[ ][3 ],int r,int c)
{ int i,j;
for(j=0;j<c;j++)
{
int prod[j]=1;
for(i=0;i<r;i++)
{ prod[j]=prod[j]*A[i][j];
}
cout<<”\nProduct of Column “<<j+1<<” = “<<prod[j];
}
}
7.Write a function in C++ to print the product of each row of a two dimensional array passed
as the arguments of the function
Example: if the two dimensional array contains
Then the output should appear as:
Product of Row 1 = 8000
Product of Row 2 = 60000
Product of Row 3 =36000
Product of Row 4 = 24000
Ans:
void productrow(int A[4][ ],int r,int c)
{ int i,j;
for(i=0;i<r;i++)
{ int prod[i]=1;
for(j=0;j<c;j++)
{ prod[i]=prod[i]*A[i][j];
}
cout<<”\nProduct of Row “<<i+1<<” = “<<prod[i];
}
}
8.
9. Write a function in C++ which accepts a 2D array of integers and its size as arguments and
displays the elements which lie on diagonals. [Assuming the 2D Array to be a square matrix with
odd dimension i.e., 3x3, 5x5 ,7x7 etc…]
Example : if the array content is
543
678
129
Out put through the function should be :
Diagonal One : 5 7 9
Diagonal Two : 3 7 1
Ans:
void daigonalarray(int a[ ][ ],int n)
{ cout<<"Diagonal One:";
for (int i=0;i<n;i++)
for(int j=0;j<n;j++)
if (i= = j)
cout<<a[i][j]<<’\t’;
12. Write a function in C++ to print sum of all values which either are divisible by 2 or divisible
by 3 present in a 2D array passed as the argument of the function.
Ans:
void Sumarray(int A[ ][ ],int R, int C)
{ int i,j,sum=0;
for(i=0;i<R;i++)
for(j=0;j<C;j++)
if(A[i][j]%2= = 0 ||A[i][j]%3== 0)
sum=sum+A[i][j];
cout<<”\nThe Sum of all the values which are divisible by 2 or 3 in the array = “<<sum; }
13. Write a function in C++ to find the sum of diagonal elements from a 2D array of type float.
Use the array and its size as parameters with float as its return type.
Ans:
float displaysum( int A[ ][ ],int n)
{ float sum=0;
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
if((i= = j) || ((i+j)= =(n-1)))
sum=sum+A[i][j];
}
return sum;
}
14. Write a user-defined function named Lower_half() which takes 2D array A, with size N rows
and N columns as argument and prints the lower half of the array.
Eg. 2 3 1 5 0 2
7153171
Input 2 5 7 8 1 the output will be 2 5 7
015010150
3491534915
Ans:
Ans:
Ans: