Assignment-2
Q1.Write a C++ program to a function.
void arrayFunc(int num[],int size , int sum) To find all pairs of elements in an
Array whose sum is equal to a given number :
Sample
Array numbers= [4, 6, 5, -10, 8, 5, 20], size= 7 , sum=10
Output :
Pairs of elements whose sum is 10 are :
4 + 6 = 10
5 + 5 = 10
-10 + 20 = 10
Q2. Write a C++ program to a function
void arraySort(int A[], int p, int B[], int q) Given two sorted arrays A and B of size
p and q, define function arraySort() to merge elements of A with B by maintaining the
sorted order i.e. fill A with first p smallest elements and fill B with remaining elements.
Example:
Input :
int[] A = { 1, 5, 6, 7, 8, 10 }
int[] B = { 2, 4, 9 }
Output:
Sorted Arrays:
A: [1, 2, 4, 5, 6, 7]
B: [8, 9, 10]
Q3.Write a program to declare a square matrix A[ ][ ] of order MxM where ‘M’ is the number of
rows and the number of columns, such that M must be greater than 2 and less than 10. Accept the
value of M as user input. Display an appropriate message for an invalid input. Allow the user to
input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Rotate the matrix 90° clockwise as shown below:
(c) Find the sum of the elements of the four corners of the matrix.
Test your program for the following data and some random data:
Example 1
INPUT :
M = 3
OUTPUT :
ORIGINAL MATRIX
MATRIX AFTER ROTATION
Sum of the corner elements = 20
Q4.Accept numbers in array A[10] & create another array
B[5]. The array B first location fills with sum of first &
second location of array A. The array B second location
fills with sum of third & forth location of array A. Similarly
fill all the position of array B & display it.
Q5. Write a C++ function to update all the elements of a
given 2D array except the secondary diagonal elements (the
diagonal from bottom-left to top-right). Height and width of
the matrix are also provided as arguments.