Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Array Functions (2D Array) : Each Questions Carry

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

ARRAY FUNCTIONS (2D ARRAY)

EACH QUESTIONS CARRY 2 OR 3 OR 4 MARKS

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:-

int SumOfAlternate (int B[][5], int M, int N)


{
int flag = 0, sum = 0;
for(int i = 0 ; i < M ; i++)
for(int j = 0 ; j < N ; j++)
{
if(flag == 0)
{
sum = sum + B[i][j];
flag = 1;
}
else
flag = 0;
}

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:

void MatAdd(int M[][4],int N,int M)


{
for (int C=0;C<M;C++)
{
int SumC=0;
for (int R=0;R<N;R++)
SumC+=M[C][R];
cout<<SumC<<endl;
}
}
( ½ Mark for correct function header)
( ½ Mark for appropriate outer loop)
( ½ Mark for appropriate inner loop)
( ½ Mark for correctly initializing SumC and calculate the sum)

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’;

cout<<"\n Diagonal Two:";


for (int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(i= = n-(j+1))
cout<<a[i][j]<<’\t’; }
10. Write a function in C++ which accepts a 2D array of integers and its size as arguments and
displays the elements of middle row and the elements of middle column. [Assuming the 2D
Array to be a square matrix with odd dimension i.e., 3x3, 5x5, 7x7 etc…]
Example : If the array content is
354
769
218
Output through the function should be :
Middle Row : 7 6 9
Middle Column : 5 6 1
Ans:
void middlearray(int a[ ][ ],int size)
{ cout<<"Middle Row:";
for (int i=0;i<size;i++)
for(int j=0;j<size;j++)
if (i= = size/2)
cout<<a[i][j]<<’\t’;
cout<<"\n Middle Column:";
for (int i=0;i<size;i++)
for(int j=0;j<size;j++)
if(j= =size/2)
cout<<a[i][j]<<’\t’;
}
11. Write function in C++ which accepts an integer array and size as arguments and assign
values into a 2D array of integers in the following format :
If the array is 1, 2, 3, 4, 5, 6
The resultant 2D array is given below
123456
123450
123400
123000
120000
100000
Ans:
void input (int a[ ],int size)
{ int b[size] [size];
for (int i=0;i<size;i++)
{ for (int j=0;j<size;j++)
{ if(( i+j)>=size)
b[i][j]=0;
else
b[i][j]=a[j];
cout<<b[i][j]<<’\t’;
}
cout<<endl;
}
}

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:

void Lower_half( int A[ ][ ],int N)


{ int i,j;
for(i=0;i<N;i++)
for(j=0;j<N;j++)
{ if(i>=j)
cout<<A[i][j]<<’\t’;
cout<<endl;
}
}
15. Write a user-defined function in C++ to find and display the multiplication of row elements
of two dimensional array A[4][6] containing integers.

Ans:

void rowmul(int A[4][6])


{ int i,j;
for(i=0;i<4;i++)
{ int prod[i]=1;
for(j=0;j<6;j++)
prod[i]= prod[i]*A[i][j];
cout<<”\nThe multiplication of “<<i+1<<” row = “<<prod[i];
}
}

16. Write the definition of a user-defined function REPEAT_ROW(int A[][3],int R, int C)

in C++ that will store the elements in the following manner


1. All row elements except the 1st element replaced by the 1st element,
2. All row elements except the 1st & 2nd element replaced by the 2nd element,
3. All row elements except the 1st , 2nd & 3rd element replaced by the 3rd element and
so on.
For example: if initially the array was:-

Ans:

You might also like