2 Data Structure Questions
2 Data Structure Questions
5. Write a code in C++ for a function void Convert ( int T[], int N) , which repositions all the elements of array
by shifting each of them one to one position before and by shifting the fast element to last.
e.g. if the content of array is
0 1 2 3
22 25 14 30
The changed array content will be:
0 1 2 3
25 14 30 22
6. Write a for a function void ChangeOver ( int P[], int N) in C++ , which repositions all the elements of array by
shifting each of them to next position and shifting last element to first position.
e.g. if the content of array is
0 1 2 3
10 14 11 21
The changed array content will be:
0 1 2 3
21 10 14 11
7.
Write a function SWAP2BEST (intARR[], intSize) in C++ to
modify the content ofthe array in such a way that the elements,
which are multiples of10swap with the value present inthe very
next positionin the array.
For example :
ifthe content ofarray ARRis
90, 56, 45, 20, 34, 54
The contentofarray ARRshould become
56, 90, 45, 34, 20, 54
8. Write a Get1From2( ) function in C++ to transfer the content from
twoarrays FIRST[ ] and SECOND[ ] to array ALL[ ]. The even places(0,2,
4,...) of array ALL[ ] should get the content from the array FIRST[ ] and
oddplaces (1, 3, 5, ) of the array ALL[] should get the content from the
arraySECOND[ ].
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
11. Write a function REASSIGNO in C++, which accepts an array of integers and its size as parameters
and divide all those array elements by 5 which are divisible by 5 and multiply other array elements by 2.
Sample Input Data of the array
12. 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
13. Write a function in C++, which accepts an integer array and its size
as parameters and rearranges the array in reverse.
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
10, 12, 8, 7, 6, 1, 5, 2, 4
14. 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 function should rearrange the content of the array as 9, 8, 75, 32,27
15. 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, and 10
The function should rearrange array as 7, 9, 23, 10, 2, 4, 1 and 6
16. Write a Function to Search for an element from Array A by Linear Search.
17. Write a Function to Search for an element from Array A by Binary Search.
18. Write a function to Sort the array A by Bubble Sort.
19. Write a function to Sort the array A by Selection Sort.
20. Write a function to Sort the array A by Insertion Sort.
21. What will be the status of the following list after fourth pass of bubble sort and fourth pass of
selection sort used for arranging the following elements in descending order?
14, 10, -12, 9, 15, 35
22. A two dimensional array P [20] [50] is stored in the memory along the
row with each of its element occupying 4 bytes, find the address of the
element P [10] [30], if the element P[5] [5] is stored at the memory
location 15000.
23. A two dimensional array ARR [50][20] is stored in the memory along the
row with each of its elements occupying 4 bytes. Find the address of the
element ARR[30][10], if the element ARR[10] [5] is stored at the memory
location 15000.
24. An array T [25] [20] is stored along the row in the memory with each
element requiring 2 bytes of storage. If the base address of array T is
42000, find out the location of T [l0] [15]. Also, find the total number of
elements present in this array.
25.An array A[20][30] is stored along the row in the memory with each
element requiring 4 bytes of storage. If the base address of array A is
32000, find out the location of A[15][10]. Also, find the total number of
elements present in this array.
26. Given an array A[10][12] whose base address is 10000. Calculate the
memory location of A[2][5] if each element occupies 4 bytes and array is
stored column-wise.
27. An array P[15][10] is stored along the column in the memory with
each elementrequiring 4 bytes of storage. If the base address of array P
is 14000, find out thelocation of P[8][5].
28. An array T[15][10] is stored along the row in the memory with each
element requiring 8 bytes of storage. Ifthe base address of array T is
14000, find out the location of T[10][7].
29. An array T[20][10] is stored in the memory along the column with each
of the element occupying 2 bytes, findout the memory location of T[10][5],
if an element T[2][9] is stored at location 7600.
30. An array P[20] [50] is stored in the memory along the column with each
of itselement occupying 4 bytes, find out the 1ocation of P[15][10], if
P[0][0] isstored at 5200.
31. An array G[50] [20] is stored in the memory along the row with each of
itselement occupying 8 bytes, find out the 1ocation of G[10][15], if P[0]
[0] is stored at 4200.
32. An array P[50] [60] is stored in thememory along the column with each
of theelement occupying 2 bytes, find out thememory location for the
element P[10][20],if the Base Address of the array is 6800.
33. An array T[90][100] is stored in thememory along the column with each
of theelements occupying 4 bytes. Find out thememory location for the
element T[10][40],if the Base Address of the array is 7200.
34. An array S[40][30] is stored in thememory along the column with each
of theelement occupying 4 bytes, find out thebase address and address of
element S[20][15], if an element S[15][10] is stored at the
memory location 7200.
35. An array Arr[50][10] is store in thememory along the row with each
elementoccupying 2 bytes. Find out the Baseaddress of the location Arr[20]
[50], if thelocation Arr[10][25] is stored at the address
10000.
38. An array S[40][30] is stored in the memoryalong the row with each of
the elementoccupying 2 bytes, find out the memory locationfor the element
S[20][10], if an element S[15][5] is stored at the memory location 5500.
39. Write a function REVCOL (intP[][5], int N, int M) in C++to display the
content of a two dimensional array, with each column content in reverse
order.
Note: Array may contain any number of rows.
For example, if the content of array is as follows:
15 12 56 45 51
13 91 92 87 63
11 23 61 46 81
The function should display output as:
11 23 61 46 81
13 91 92 87 63
15 12 56 45 51
46. Write a COLSUM( ) function in C++ to find sum of each column of a NxM
Matrix.
47. Write a ROWSUM( ) function in C++ to find sum of each row of a rxc
Matrix.
48. Write a DSUM function in C++ to find the sum of diagonal element of
an*n matrix.
50. Write a user defined function in C++ to display the sum of row
elements of two dimensional array A[5][6]containing integers.
51. Write a function in C++ to print the product of each column of a two
dimensional integer array passed as theargument of the function.
Explain: if the two dimensional array contains
1 2 4
3 5 6
4 3 2
2 1 5
Then the output should appear as:
Product of Column 1 = 24
Product of Column 2 = 30
Product of Column 3 = 240
52. Write a function in C++ which accepts a 2D array of integers and its
size as arguments and display the elementswhich lie on diagonals.
[Assuming the 2D Array to be a square matrix with odd dimension i.e., 3 x
3, 5 x 5, 7 x 7 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
53. Write a function in C++ which accepts a 2D array of integers and its
size as arguments and display the elementsof middle row and the elements of
middle column.
[Assuming the 2D Array to be a square matrix with odd dimension i.e., 3 x
3, 5 x 5, 7 x 7 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
54. Write a function int ALTERSUM (int B[ ][5], int N, int M in C++ to
find and returnthe sum of elements from all alternateelements of a two-
dimensional arraystarting from B[0][0].
60. Write the equivalent infix expression for a, b, AND, a, c, AND, OR.
78. Write the definition of a member function Pop() in C++, to delete a book from a
dynamic stack of TEXTBOOKS considering the following code is already included in the
program.
struct TEXTBOOKS
{
char ISBN[20]; char TITLE[80];
TEXTBOOKS *Link;
};
class STACK
{
TEXTBOOKS *Top;
public:
STACK() {Top=NULL;}
void Push();
void Pop();
~STACK();
};
79. Write the definition of a member function PUSH() in C++, to add a new book in a
dynamic stack of BOOKS considering the following code is already included in the
program:
struct BOOKS
{
char ISBN[20], TITLE[80];
BOOKS *Link;
};
class STACK
{
BOOKS *Top;
public:
STACK()
{Top=NULL;}
void PUSH();
void POP();
~STACK();
};
};