Answer: Here is one way you could write a C++ program to sort only the even elements of an array of integers in ascending order:
Explanation: Copy this Code
#include <iostream>
#include <algorithm>
using namespace std;
// Function to sort only the even elements of an array in ascending order
void sortEvenElements(int arr[], int n)
{
// create an auxiliary array to store only the even elements
int aux[n];
int j = 0;
// copy only the even elements from the original array to the auxiliary array
for (int i = 0; i < n; i++)
if (arr[i] % 2 == 0)
aux[j++] = arr[i];
// sort the auxiliary array using the STL sort function
sort(aux, aux + j);
// copy the sorted even elements back to the original array
j = 0;
for (int i = 0; i < n; i++)
if (arr[i] % 2 == 0)
arr[i] = aux[j++];
}
int main()
{
// test the sortEvenElements function
int arr[] = {5, 3, 2, 8, 1, 4};
int n = sizeof(arr) / sizeof(arr[0]);
sortEvenElements(arr, n);
// print the sorted array
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}