Array and Address

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Assignment (Array)

1.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. (4)
Example : if an array of five elements initially contains the elements as
3, 4, 5, 16, 9
then the function should rearrange the content of the array as
6, 2, 10, 8, 18

2. An array Arr[15][20] is stored in the memory along the row with each element occupying 4 bytes. Find
out the Base Address and address of the element Arr[3][2], if the element Arr[5][2] is stored at the
address 1500.

3. 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. (2)
[Assuming the 2D Array to be a square matrix with odd dimension i.e. 3×3, 5×5, 7×7 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

4. 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
A[0] A[1] A[2] A[3] A[4]
21 12 35 42 18
Content of the array after Calling CHANGE() function
A[0] A[1] A[2] A[3] A[4]
3 36 5 6 54

5. An array P[50] [60] is stored in the memory along the column with each of the element occupying 2
bytes, find out the memory location for the element P[10][20], if the Base Address of the array is 6800.
(3)

6. 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
A[0][0] A[0][1] A[0][2]
4 5 1
A[1][0] A[1][1] A[1][2]
2 8 7
A[2][0] A[2][1] A[2][2]
9 6 3
The function SKIPSUM() should add elements A[0][0], A[0][2],A[1][l],A[2][0] and A[2][2].
7. Write code for a function void Convert (int T [ ] , int. Num) in C++ , which re-positions all the
elements of the array by shifting each of them one to one position before and by shifting the first element
to the last position. (3)
For example: If the content of the array is
0 1 2 3 4
22 25 70 32 12
The changed content will be:
0 1 2 3 4
25 70 32 12 22

8. An array P[15][10] is stored along the column in the memory with each element requiring 4 bytes of
storage. If the base address of array P is 14000, find out the location of P(8][5]. (3)

9. Write a user-defined function DispNTen ( int L [] [4] , int R , int. C)in C++ to find and
display all the numbers, which are not divisible by 10.
For example if the content of array is: (2)
20 17 30
12 19 10
The output should be
17 12 19

10. Write the definition of a function FixPay(float Pay[], int N) in C++, which should modify each
element of the array Pay having N elements, as per the following rules: (2)
Existing Value of Pay Pay to be changed to
If less than 100000 Add 25% in the existing value
If >=100000 and <20000. Add 20% in the existing value
If >=200000 Add 15% in the existing value

11. T[20][50] is a two dimensional array, which is stored in the memory along the rowwith each of its
element occupying 4 bytes, find the address of the element T[15][5], if the element T[10][8] is stored at
the memory location 52000. (3)

12. Write definition for a function SHOWMID(int P[][5],int R,int C) in C++ to display the elements of
middle row and middle column from a two dimensional array P having R number of rows and C number
of columns. (3)
For example, if the content of array is as follows:
115 112 116 101 125
103 101 121 102 101
185 109 109 160 172
The function should display the following as output:
103 101 121 102 101 116 121 109

13. Write the definition of a function MIXER(int A[], int N) in C++, which should
multiply 2 to the odd values present in the array and multiply 3 to the even values
present in the array. The entire content of the array A having N elements should
change without using any other array. 2
Example : if the array Arr contains
23 20 5 11 10
Then the array should become
46 60 10 22 30
Note :The function should not display the content of the array.
14. Write definition for a function TOPBOTTOM(int M[][5],int N,int M) in C++,
which finds and displays sum of the values in topmost row and sum of the values in
bottommost row of a matrix M (Assuming the parameter N represents number of
Row and the parameter M represents number of Columns).
For example, if the content of array M having N as 4 and M as 5 is as follows :
10 20 30 40 50
12 15 32 4 15
38 4 11 24 15
5 10 15 20 25
The function should find the sum and display the same as :
Sum of Top Row : 150
Sum of Bottom Row : 75

15. G[15][20] is a two dimensional array, which is stored in the memory along the
column with each of its element occupying 4 bytes, find the address of the element
G[5] [10], if the element G[2] [4] is stored at the memory location 52000. 3

16. Write the definition of a function Reverse(int Arr[], int N) in C++, which should
reverse the entire content of the array Arr having N elements, without using any other array. (3)
Example: if the array Arr contains
13 10 15 20 5
Then the array should become
5 20 15 10 13
Note :
• The function should only rearrange the content of the array.
• The function should not copy the reversed content in another array.
• The function should not display the content of the array.

17. Write definition for a function ADDMIDROW(int MAT[][10],int R,int C) in C++, which finds sum
of the middle row elements of the matrix MAT (Assuming C represents number of Columns and R
represents number of rows, which is an odd integer). (2)
For example, if the content of array MAT having R as 3 and C as 5 is as follows:
12345
21345
34125
The function should calculate the sum and display the following :
Sum of Middle Row : 15

18. T[25][30] is a two dimensional array, which is stored in the memory along the
row with each of its element occupying 2 bytes, find the address of the element
T[10] [15], if the element T[5] [10] is stored at the memory location 25000. (3)

You might also like