0
double P[5] = {9, 17, 151, -3, 5};

I need to sort this array with help of pointers in ascending order by cosines of array elements.

smac89
  • 39,374
  • 15
  • 132
  • 179

1 Answers1

2
#include <array>
#include <algorithm>  // std::sort
#include <cmath>      // cos()  // cosine function
#include <iostream>

// short function to print a std::array, T must have << operator
template <typename T, std::size_t N>
void printArray(const std::array<T, N>& arr)
{
    for (auto i: arr)
        std::cout << i << ' ';
    std::cout << '\n';
}

int main()
{
    std::array<double, 5> P{9, 17, 151, -3, 5};
    printArray(P);
    // Passing a lambda to std::sort so it compares cosines of the values
    std::sort(P.begin(), 
              P.end(), 
              [](double a, double b) { return cos(a) < cos(b); });
    printArray(P);
}

Instead of a C-array, I am using the std::array library (C++11). It provides more safety. Since sorting is something that's so common, the C++ standard library provides a function for it, and we can tell it how to compare the values. I do that by passing the lambda as the third argument.

If you really want/have to use a C array, the code doesn't change much.

#include <algorithm>  // std::sort
#include <cmath>      // cos()  // cosine function
#include <iostream>
#include <iterator>

// short function to print a std::array, T must have << operator
template <typename T>
void printArray(T *arr, int size)
{
    for (int i = 0; i < size; ++i)
        std::cout << arr[i] << ' ';
    std::cout << '\n';
}

int main()
{
    double P[5] = {9, 17, 151, -3, 5};
    printArray(P, sizeof(P) / sizeof(double));
    // Passing a lambda to std::sort so it compares cosines of the values
    std::sort(std::begin(P),
              std::end(P),
              [](double a, double b) { return cos(a) < cos(b); });
    printArray(P, sizeof(P) / sizeof(double));
}
sweenish
  • 4,793
  • 3
  • 12
  • 23
  • [What's the difference between "STL" and "C++ Standard Library"](https://stackoverflow.com/questions/5205491/whats-the-difference-between-stl-and-c-standard-library). – François Andrieux Oct 21 '19 at 17:22
  • Tip : While `std::array` improve on C arrays in many ways, this solution can still work with a `int[5]`. `std::begin(P)` and `std::end(P)` provide "iterators" for C arrays as well as any class type that has appropriate `begin` and `end` members. – François Andrieux Oct 21 '19 at 17:24
  • I am aware that C-arrays could be used in my solution. But the use of C-arrays should be discouraged when they don't make sense, and I don't think they make sense here. – sweenish Oct 21 '19 at 17:26
  • Right, but sorting an array can come up for *other* use cases or in scenarios where it's not possible to change the type of the container. I feel it's worth while to point out that it can also work in those cases. – François Andrieux Oct 21 '19 at 17:27
  • I get that, I've also added a modified solution that keeps the C array. – sweenish Oct 21 '19 at 17:35
  • Also, could have been worse. I could have gone full SO and just yelled at them for not using a vector. ;-) – sweenish Oct 21 '19 at 17:43