0% found this document useful (0 votes)
279 views12 pages

Array Sample Questions

The document provides 15 questions related to arrays in C++ and their solutions. The questions cover a range of array operations including: sorting arrays in descending/ascending order; finding products of rows/columns of 2D arrays; swapping elements in arrays; manipulating (multiplying/dividing) elements based on even/odd values; accessing diagonals and middle rows/columns of 2D arrays; filling 2D arrays based on 1D input arrays; and exchanging first and second half elements of arrays. For each question, the corresponding C++ code solution is provided.

Uploaded by

SvLab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
279 views12 pages

Array Sample Questions

The document provides 15 questions related to arrays in C++ and their solutions. The questions cover a range of array operations including: sorting arrays in descending/ascending order; finding products of rows/columns of 2D arrays; swapping elements in arrays; manipulating (multiplying/dividing) elements based on even/odd values; accessing diagonals and middle rows/columns of 2D arrays; filling 2D arrays based on 1D input arrays; and exchanging first and second half elements of arrays. For each question, the corresponding C++ code solution is provided.

Uploaded by

SvLab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ARRAY QUESTIONS – I (XII CS)

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

cout<<"\nThe elements after completed the alterations";


for(i=0;i<no;i++)
cout<<arr[i]<<" ";
}

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

The function should rearrange array as


7 9 23 10 2 4 1 6

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}

Ans. void MERGEARRAY(int X[ ], int m,int Y[ ],int n,int Z[ ])


{
int mn,i,,left=0,right=mn-1;
mn=m+n;
for(i=0;i<m;i++)
if(X[i]%2= = 1)
Z[left++]=X[i]; //For copying odd numbers of X into Z from left to right
else
Z[right- -]=X[i]; //For copying even number of X into Z from right to left

for(i=0;i<n;i++)

if (X[i]%2= = 1)

Z[left++]=Y[i]; //For copying odd numbers of Y into Z from left to right


else
Z[right- -]=Y[i]; //For copying even number of X into Z from right to left
}

20 Suppose a 1D array AR containing integers is arranged in ascending order. Write a user


defined function in C++ to search for one integer from AR with the help of binary search method, to
show presence of the number in the array. The function should have three parameters: (1) an array
AR (2) the number to be searched and (3) the number of elements N in the array.

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.

Example: If the two dimensional array contains


2149
1377
5863
7212
After swapping of the content of 1st column and last column, it should be:
9142
7371
3865
2217

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

26 . Write a Get1From2 ( ) function in C++ to transfer the content from two


arrays FIRST[ ] and SECOND[ ] to array ALL[ ]. The even places (0, 2, 4,
...) of array ALL[ ] should get the content from the array FIRST[ ] and odd
places (1, 3, 5, ) of the array ALL[] should get the content from the array
SECOND[ ].
Example:
If the FIRST[ ] array contains
30, 60, 90
And the SECOND[ ] array contains
10, 50, 80
The ALL[ ] array should contain
30, 10, 60, 50, 90, 80

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)

void Get1From2(int ALL[],int FIRST[],int SECOND[],


int N, int M)
{

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.

TRANSFERP( int ALL[ ], int N)


{ int PRIME[100],i,j,tp=0,count;
for(i=0;i<N;i++)
{
count=0;
for(j=1;j<=ALL[i];j++)
if(ALL[i]%j= =0)
count++;
if(count= = 2)
{
PRIME[tp]=ALL[i];
tp++;
}
} //end of for
//displaying all prime numbers of array PRIME[]
cout<<”\nAll prime numbers in resultant array are:\n”;
for(i=0;i<tp;i++)
cout<<PRIME[i]<<’ ‘; }

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

You might also like