Monday, March 10, 2014

Q3 of CBSE Question Paper

1. Write a function in C++ to swap consecutive pair of elements of a one-dimensional
array of size N (Where N is a even number)

If the Original Array Content is
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
78 12 45 67 32 89 66 31
Swapped Content as directed will be
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
12 78 67 45 89 32 31 66

Answer:
void SwapAlter(int A[], int N)
{
for (int I=0;I<N;I+=2)
{
int T=A[I];
A[I]=A[I+1];
A[I+1]=T;

Write a function in C++ to swap first half of the array with second half of a onedimensional
array of size N.

A) If the Original Array Content is
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
78 12 45 67 32 89 66 31
Swapped Content as directed will be
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
32 89 66 31 78 12 45 67
B) If the Original Array Content is
A[0] A[1] A[2] A[3] A[4] A[5] A[6]
78 12 45 67 32 89 66
Swapped Content as directed will be
A[0] A[1] A[2] A[3] A[4] A[5] A[6]
32 89 66 67 78 12 45

Answer:
void SwapHalves(int A[], int N)
{
for (int I=0;I<N/2;I++)
{
int T=A[I];
A[I]=A[(N+1)/2+I];
A[(N+1)/2+I]=T;
}
}
In the code, if N would have
been given as Even Number
Writing [N/2+I] in place of
[(N+1)/2+I] would have also been
correct

3. Write a function in C++ to swap first row and the last row content of a twodimensional
array of size NxM
If the Original Array Content is
23 51 67 71
78 12 45 95
32 31 66 26
Swapped Content as directed will be
32 31 66 26
78 12 45 95
23 51 67 71

Answer:
void SwapRows(int A[][4],
int NOR,int NOC)
{
for (int I=0;I<NOC;I++)
{
int T=A[0][I];
A[0][I]=A[NOR-1][I];
A[NOR-1][I]=T;
}
}
Note: if the Swapping of first column
with Last column would have been
required, following is the code
void SwapCols(int A[][4],
int NOR,int NOC)
{
for (int I=0;I<NOR;I++)
{
int T=A[I][0];
A[I][0]=A[I][NOC-1];
A[I][NOC-1]=T;
}
}

2 comments:

  1. Top 5 best casinos in South Africa: 2021's BEST Gambling Sites
    In fact, the biggest gambling site of choice is Betway, a famous sports 용인 출장마사지 betting 광양 출장마사지 site in South 의정부 출장샵 Africa. They 김제 출장샵 also 의왕 출장샵 offer a wide variety of games and

    ReplyDelete