Array Sample Questions
Array Sample Questions
1. Write a function in C++, which accepts an integer array and its size as parameters and
rearranges the array in descending order.
Example: If an array of nine elements initially contains the elements as
4 2 5 1 6 7 8 12 10
Then the function should rearrange the array as
12 10 8 7 6 5 4 2 1
Ans. #include<iostream.h>
#include<conio.h>
void select_sort(int a[ ], int n)
{
int i, j, p,large;
for(i=0;i<n-1;i++)
{
large=a[i];
p=i;
for(j=i+1; j<n; j++)
{
if(a[j]>large)
{
large=a[j];
p=j;
}
}
a[p]=a[i];
a[i]=large;
}
}
3 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. #include<conio.h>
#include<iostream.h>
void colProduct(int arr[4][3],int r,int c)
{
int arr2[3];
for(int i=0;i<c;i++) //loop for column
{
arr2[i]=1;
for(int j=0;j<r;j++) //loop for rows
arr2[i] *= arr[j][i];
cout<<"Product of Column "<<i+1<<"= "<<arr2[i]<<endl;
}
}
4 Write a function in C++, which accepts an integer array and its size as arguments and swap
the elements of every even location with its following odd location.
Example: If an array of nine elements initially contains the elements as
2 4 1 6 5 7 9 23 10
then the function should rearrange the array as
4 2 6 1 7 5 23 9 10
1
ARRAY QUESTIONS – I (XII CS)
Ans. #include<conio.h>
#include<iostream.h>
void swapElement(int arr[ ], int no)
{
int temp;
for(int i=0;i<no-1;i+=2)
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;}
5.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 = 6000
Product of Row 3 =3600
Product of Row 4 = 2400
Ans. #include<conio.h>
#include<iostream.h>
void rowProduct(int arr[4][3],int r,int c)
{
long arr2[4];
for(int i=0;i<r;i++)
{
arr2[i]=1;
for(int j=0;j<c;j++)
{
arr2[i] *= arr[i][j];
}
cout<<"Product of Row "<<i+1<<"= "<<arr2[i]<<endl;
}
}
void main()
{
int arr[4][3]={{20,40,10},{40,50,30},{60,30,20},{40,20,30}};
clrscr();
rowProduct(arr,4,3);
getch();
}
7 Write function in C++ which accepts an integer array and size as arguments and replaces
elements having odd values with thrice its value and elements having even values with twice its
value.
Example : if an array of five elements initially contains elements as
3 4 5 16 9
The the function should rearrange the content of the array as
9 8 75 32 27
2
ARRAY QUESTIONS – I (XII CS)
Ans. #include<conio.h>
#include<iostream.h>
void manipulate(int a[],int size)
{
for(int i=0;i<size;i++)
{
if (a[i]%2==1)
a[i]=a[i]*3;
else
a[i]=a[i]*2;
cout<<a[i]<<',';}
}
8 An array Array[20][15] is stored in the memory along the column with each element
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
5 4 3
6 7 8
1 2 9
Output through the function should be :
Diagonal One : 5 7 9
Diagonal Two : 3 7 1
Ans. #include<conio.h>
#include<iostream.h>
void diag(int a[3][3],int size)
{
cout<<"First Diagonal:";
for (int i=0;i<size;i++)
for(int j=0;j<size;j++)
if(i==j)
cout<<a[i][j]<<" ";
cout<<"\n Second Diagonal:";
for(i=0;i<size;i++)
for(j=0;j<size;j++)
if((i+j)==(size-1))
cout<<a[i][j]<<" ";
}
10 Write a function in C++ which accepts an integer array and its size as arguments and
replaces elements having even values with its half and elements having odd values with twice its
value .
Example : If an array of five elements initially contains the elements as
3 4 5 16 9
then the function should rearrange content of the array as
6 2 10 8 18
Ans. #include<conio.h>
#include<iostream.h>
void accept(int a[ ],int size)
{
for (int i=0;i<size;i++)
{
if(a[i]%2==0)
a[i]=a[i]/2;
else
3
ARRAY QUESTIONS – I (XII CS)
a[i]=a[i]*2;
cout<<a[i]<<',';
}
}
12 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
3 5 4
7 6 9
2 1 8
Output through the function should be :
Middle Row : 7 6 9
Middle Column : 5 6 1
Ans. #include<conio.h>
#include<iostream.h>
void accept(int a[3][3],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(i=0;i<size;i++)
for(j=0;j<size;j++)
if(j==size/2)
cout<<a[i][j]<<'\t';
}
void main()
{
int a[3][3]={{1,2,3},{4,5,6},{7,8,9}};
clrscr();
accept(a,3);
getch();
}
13 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
1 2 3 4 5 6
1 2 3 4 5 0
1 2 3 4 0 0
1 2 3 0 0 0
1 2 0 0 0 0
1 0 0 0 0 0
If the array is 1, 2, 3
The resultant 2D array is given :
1 2 3
1 2 0
1 0 0
Ans. #include<conio.h>
4
ARRAY QUESTIONS – I (XII CS)
#include<iostream.h>
void twoDArray(int aa[ ],int size)
{ int twodarr[6][6];
for (int i=0;i<size;i++)
{
for (int j=0;j<size;j++)
{
if((i+j)>=size)
twodarr [i][j]=0;
else
twodarr[i][j]=a[j];
cout<< twodarr[i][j]<<” “;
}
cout<<endl;
}
}
void main()
{
int arr[6]={1,2,3,4,5,6};
clrscr();
twoDArray (arr,6);
getch();
}
14 Write a function in C++ which accepts an integer array and its size as arguments and
exchanges the values of first half side elements with the second half side elements of the array.
Example :
If an array of 8 elements initial content as
2 4 1 6 7 9 23 10
Ans #include<iostream.h>
void modify(int a[],int size)
{
int i,j,temp;
for(i=0,j=size/2; j<size; i++,j++)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
for(i=0;i<size;i++)
{
cout<<a[i]<<" ";
}
}
15 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 :
1 0 0 0 0 0
5
ARRAY QUESTIONS – I (XII CS)
1 2 0 0 0 0
1 2 3 0 0 0
1 2 3 4 0 0
1 2 3 4 5 0
1 2 3 4 5 6
If the array is 1, 2, 3
The resultant 2D array is given :
1 0 0
1 2 0
1 2 3
Ans.
#include<conio.h>
#include<iostream.h>
void input (int a[],int size)
{
int b[6][6];
for (int i=0;i<size;i++)
{
for (int j=0;j<size;j++)
{
if((i<j))
b[i][j]=0;
else
b[i][j]=a[j];
cout<<b[i][j]<<" ";
}
cout<<endl;
}
}
void main()
{
int a[6]={1,2,3,4,5,6};
clrscr();
input(a,6);
getch();}
16 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. #include<conio.h>
#include<iostream.h>
void Sum(int A[3][3],int R,int C)
{
int i,j,S=0;
for(i=0;i<R;i++)
for(j=0;j<C;j++)
if(A[i][j]%2==0||A[i][j]%3==0)
S=S+A[i][j];
cout<<"\nThe Sum of all the values which are divisible by 2 or 3 in the array = "<<S;
}
6
ARRAY QUESTIONS – I (XII CS)
17 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. #include<conio.h>
#include<iostream.h>
float diasum(float arr[3][3],int r,int c)
{
int i,j;
float Dsum=0.0;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
if((i==j))
{
Dsum=Dsum+arr[i][j];
cout<<arr[i][j]<<endl;
}
cout<<Dsum;
return Dsum;
}
19 Given two arrays of integers X and Y of sizes m and n respectively. Write a function named
MERGE() which will third array named Z, such that the following sequence is followed.
All odd numbers of X from left to right are copied into Z from left to right.
All even numbers of X from left to right are copied into Z from right to left.
All odd numbers of Y from left to right are copied into Z from left to right.
All even numbers of Y from left to right are copied into Z from right to left.
X, Y and Z are passed as arguments to MERGE().
Eg. X is {3, 2, 1, 7, 6, 3} and {9, 3, 5, 6, 2, 8, 10}
the resultant array Z is {3, 1, 7, 3, 9, 3, 5, 10, 8, 2, 6, 6, 2}
for(i=0;i<n;i++)
if (X[i]%2= = 1)
Ans. #include<conio.h>
#include<iostream.h>
void Search(int AR[], int Sno, int EN)
{
int l=0,u=EN-1,m,flag=0;
7
ARRAY QUESTIONS – I (XII CS)
while(l<=u)
{
m=(l+u)/2;
if(Sno==AR[m])
{
flag=1;
break;
}
else if(Sno<AR[m])
u=m-1;
else
l=m+1;
}
if( flag==0)
cout<<"\nThe Search Element "<<Sno<<"is not available";
else
cout<<"\nThe Search Element "<<Sno<<" is available";
}
23. Define a function SWAPCOL( ) in C++ to swap (interchange) the first column elements with the
last column elements, for a two dimensional integer array passed as the argument of the function.
Solution:
void SWAPCOL(int A[ ][100], int M, int N)
{
int Temp, I;
for (I=0;I<M;I++)
{
Temp = A[I][0];
A[I][0] = A[I][N-1];
A[I][N-1] = Temp;
}
}
24. Write a function CHANGEO in C++, which accepts an array of integer and
its size as parameters and divide all those array elements by 7 which are
divisible by 7 and multiply other-array elements by 3. 3
Sample Input Data of the array
Content of the array after Calling CHANGE() function
Ans. void CHANGE (int A[ ], int N)
{
for(int I = 0; I<N; I++)
{
8
ARRAY QUESTIONS – I (XII CS)
if (A[I]%7 = = 0)
A [I] = A [I] /7;
else
A[I] = A[I] * 3;
}
}
25. Write a function int SKIPSUM(int A[ ] [3], int N,int M) in C++ to find and
return the sum of elements from all alternate elements of a two-dimensional
array starting from A[0][0]. 2
Hint:
If the following is the content of the array
The function SKIPSUM() should add elements A[0][0], A[0][2], A[1][l],
A[2][0] and A[2][2].
Ans. int SKIPSUM(int A[ ][ 3 ], int N,int M)
{
int S=0:
for(int I = 0; 1< N; 1++)
for (int J = (I%2)?1:0; J<M; J = J+2)
S = S + A [I][J];
return S };
(OR)
int SKIPSlJM(int A[ ][3], int N, int M)
{
int S=0;
for (int I = 0; 1< N; I++)
for (int J = (I%2==0)? 0:1 ; J<M; J = J+2)
S = S + A [I][J];
return S;
}
(OR)
int SKIPSUM(int A[][3], int N, int M)
{
int I,J, S=O;
for (I = 0; 1 < N; 1++)
{
if (I%2) //OR (1%2 !=0 ) OR (I%2 == 1)
J = l;
else
J = 0;
for ( ; J<M; J = J+2)
S = S + A[I][J];
}
return S;
}
(OR)
int SKIPSUM(int A[][3], int N, int M)
{
int S=0, C=0;
for(int I = 0; 1< N; 1++)
for (int J = 0; J<M; J++ )
{
if (C%2 == 0)
S = S + A[I][J];
C++;
}
9
ARRAY QUESTIONS – I (XII CS)
return S;
}
(OR)
int SKIPSUM(int A[][3], int N, int M)
{
int S=0, C=l;
for(int I = 0; I<N; I++)
for (int J = 0; J<M; J++ )
{
if (C%2 != 0)
S = S + A[I][J];
C++;
}
return S;
}
(OR)
int SKIPSUM (int A[][3], int N, int M)
{
int S=0;
for(int I = 0; l< N; l++)
for (int J = 0; J<M; J++ )
{
if ((I+J)%2 == 0)
S = S + A[I][J];
}
return S;
}
Ans
void Get1From2 (int ALL[],int FIRST[],int SECOND[],
int N,int M)
{
for(int I=0,J=0,K=0;i<N+M; I++)
if (I%2==0)
ALL[I]=FIRST[J++];
else
ALL[I]=SECOND[K++];
}
(OR)
10
ARRAY QUESTIONS – I (XII CS)
int J=0,K=0;
for(int I=0;i<N+M; I++)
{
if (I%2==0)
{
ALL [I] =FIRST [J] ;
J++;
}
354
else
{
ALL[I]=SECOND[K];
K++;
}
}
}
27. ) Write a COLSUM( ) function in C++ to find sum of each column of a NxM Matrix.
Ans
void COLSUM(int A[] [100], int N, int M)
{
int SUMC;
for (int j=0; j<M; j++)
{
SUMC = 0;
for (int i=0; i<N; i++)
SUMC = SUMC + A[i] [j] ;
Cout<< "Sum of Column "<<j<<" = "<<SUMC ;
}}
28) Write a function TRANSFERP( int ALL[ ], int N) , to transfer all the prime numbers from a one
dimensional array ALL[ ] to another one dimensional array PRIME[ ]. The resultant array PRIME[ ]
must be displayed on screen.
11
ARRAY QUESTIONS – I (XII CS)
29. Write a function in C++ to combine the contents of two equi-sized arrays A and B by computing
their corresponding elements with the formula 2*A[i]+3*B[i]; where value i varies from 0 to N-1 and
transfer the resultant content in the third same sized array.
Answer:
void AddNSave(int A[],int B[],int C[],int N)
{ for (int i=0;i<N;i++) C[i]=2*A[i]+3*B[i]; }
30 ) 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;
}
31. Function to find & display sum of rows & sum of cols. of a 2 dim. array A
void SumRowCol(int A[][20], 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<<"Row("<<R<<")="<<SumR<<endl;
}
for(int R=0;R<N;R++)
{
int SumR=0;
for(int C=0;C<M;C++)
SumR+=A[R][C];
cout<<"Row("<<R<<")="<<SumR<<endl;
}
}
12