Consider A, B, C as three arrays of size m,n and m + n respectively. Array A is stored in ascending order
whereas array B is stored in descending order. Write a C++ program to produce a third array C, containing
all the data of arrays A and B and arranged in descending order. Display the data of array C only.
Answers
Answered by
113
C++ program ...
I am writing here the algorithm to solve the problem. Please write it in C++ language statements. It is not difficult. It is in fact quite simple.
Array A = A[m-1] .. A[ i ] .. A[0] descending order of data.
Array B = B[0] .. B[j] .. B[n-1] descending order of data.
Array C = C[0] .. C[k] ... C[m+n-1] to store in descending order.
#include <iostream.h>
// m & n are known.
main()
{
int i = m-1, j = 0, k = 0;
while (i >= 0 && j <= n-1) {
if (A[i] > B[j]) C[k++] = A[i--] ;
else C[k++] = B[j++] ;
}
while (i >= 0)
C[k++] = A[i--] ;
while (j < n)
C[k++] = B[j++] ;
i = 0;
while (i < m+n)
cout << C[i] ;
}
I am writing here the algorithm to solve the problem. Please write it in C++ language statements. It is not difficult. It is in fact quite simple.
Array A = A[m-1] .. A[ i ] .. A[0] descending order of data.
Array B = B[0] .. B[j] .. B[n-1] descending order of data.
Array C = C[0] .. C[k] ... C[m+n-1] to store in descending order.
#include <iostream.h>
// m & n are known.
main()
{
int i = m-1, j = 0, k = 0;
while (i >= 0 && j <= n-1) {
if (A[i] > B[j]) C[k++] = A[i--] ;
else C[k++] = B[j++] ;
}
while (i >= 0)
C[k++] = A[i--] ;
while (j < n)
C[k++] = B[j++] ;
i = 0;
while (i < m+n)
cout << C[i] ;
}
kvnmurty:
:-)
Similar questions