Search code examples
cnanbubble-sort

How to sort an array that includes NaN's C


Have been trying to implement my code as a means to sort all integers including NaNs. However can not seem to find a function that would sort NaNs into my program. Code is able to sort other integers including infinities, however when a nan is entered the program recognizes the input but does not sort it to the start of the list. Any help would be appreciated.

#include <stdio.h>
#include <math.h>

int main()
{
    float array[100], swap;
    int c, d, n;

    printf("Enter the size of array\n");
    scanf("%d", &n);

    printf("Enter %d integers\n", n);

    for (c = 0; c < n; c++)
        scanf("%f", &array[c]);

    for (c = 0; c < (n - 1); c++)
    {
        for (d = 0; d < n - c - 1; d++)
        {
            if (array[d] > array[d + 1]) 
            {
                swap = array[d];
                array[d] = array[d + 1];
                array[d + 1] = swap;
            }
        }
    }

    printf("Sorted array in ascending order:\n");


    for (c = 0; c < n; c++)
        printf("%f\n", array[c]);

    return 0;
}

Solution

  • As mentioned in the comments, your code is C code and not C++. Here is your code in C++ with an added condition which should fix your problem:

    #include <iostream>
    #include <vector>
    #include <cmath>
    
    int main()
    {
        std::size_t array_size;
        std::cout << "Enter the size of array\n";
        std::cin >> array_size;
    
        std::cout << "Enter " << array_size << " integers\n";
    
        std::vector<float> array(array_size);
        for(std::size_t i = 0; i < array.size(); ++i)
            std::cin >> array[i];
    
        for(std::size_t a = 0; a < array.size() - 1; ++a)
            for(std::size_t b = 0; b < array.size() - 1 - a; ++b)
                if(std::isnan(array[b + 1]) || array[b] > array[b + 1])
                    std::swap(array[b], array[b + 1]);
    
        std::cout << "Sorted array in ascending order:\n";
    
        for(const auto& a : array)
            std::cout <<  a << '\n';
    
        return 0;
    }
    

    And if you don't want to write all the sort stuff by yourself, you can do it with even more C++ and the algorithm library (and an added input check):

    template<typename T>
    T get_input()
    {
        T input;
        while(true)
        {
            std::cin >> input;
            if(std::cin)
                return input;
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input! Please try again.\n";
        }
    }
    
    int main()
    {
        std::cout << "Enter the size of array\n";
        std::size_t array_size = get_input<std::size_t>();
    
        std::cout << "Enter " << array_size << " integers\n";
    
        std::vector<float> input(array_size);
        for(auto& a : input)
            a = get_input<float>();
    
        std::sort(input.begin(), input.end(), [](const auto& a, const auto& b){ return std::isnan(a) || a < b; });
    
        std::cout << "Sorted array in ascending order:\n";
    
        for(const auto& a : input)
            std::cout << a << '\n';
    }