-2

I'm trying to make program to sort numbers higher to lower from left to right, and it seems to glitch whenever the number increments too soon. I tried a handful of inputs:

42145 Sorted to 54421, correct;

41245 Sorted to 54221, wrong but almost right;

1021 Sorted to 2210, wrong but almost right;

254321234 Sorted to 555555552, very much wrong.

Is it just my method of sorting or did I do something wrong? Code:

#include <iostream>
#include <vector>
#include <math.h>
#include <string>
#include <sstream>
using namespace std;
 
uint64_t descend(uint64_t num) {
    vector<uint64_t> numVect;
    int numLength = 1;
    uint64_t numTmp = num;
    stringstream output;
    uint64_t result;
 
    //Find the length of the entered number
    while (true) {
        numTmp /= 10;
        if (numTmp > 0)
            numLength++;
        else
            break;
    }
 
    //Make the entered number into a vector
    for (int i = 1; i < numLength + 1; i++) {
        numTmp = num / pow(10, numLength - i);
        numTmp %= 10;
        numVect.push_back(numTmp);
    }
 
    //Sort the numbers in the vector
    for (int j = 0; j < numLength; j++) {
        numTmp = numVect[j];
        for (int i = j; i < numLength; i++) {
            if (numTmp < numVect[i]) {
                numVect.insert(numVect.begin()+j, numVect[i]);
                numVect.erase(numVect.end() - 1);
                break;
            }
        }
    }
 
    //Convert the vector to a regular number and return
    for (int i = 0; i < numLength; i++) {
        output << numVect[i];
    }
    output >> result;
    return result;
}
 
int main() {
    uint64_t output;
    cin >> output;
    output = descend(output);
    cout << "output: " << output << endl;
    return 0;
}
  • 5
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) and [Debugging Guide](http://idownvotedbecau.se/nodebugging/). FWIW, if you read the input in as a string is becomes really easy to sort it. – NathanOliver Apr 29 '22 at 21:38
  • 1
    `pow` is never a good choice for integer values. – Yksisarvinen Apr 29 '22 at 21:45
  • @DinoNuggies These statements numVect.insert(numVect.begin()+j, numVect[i]); numVect.erase(numVect.end() - 1); do not make a sense. – Vlad from Moscow Apr 29 '22 at 21:45
  • @NathanOliver That sounds good, my current setup/environment is Sublime Text 3 on Windows 10 and compiling code using WSL1 Ubuntu 20.04 and Xming for graphics, you know a debugger that could fit into that criteria? Maybe a Sublime Text plugin? – DinoNuggies Apr 30 '22 at 01:41
  • @VladfromMoscow Why? In that block I'm taking an element from the vector and putting it in the front, by inserting a number that exists in the vector into the beginning, and deleting the element I used to insert, therefore moving an element. Is there a better way? – DinoNuggies Apr 30 '22 at 01:44
  • @Yksisarvinen Explanation? – DinoNuggies Apr 30 '22 at 01:44
  • @NathanOliver Update: I found a debugger for Sublime Text, it is indeed a plugin, thank you for the suggestion! I will try it out and see if I can find the problem. – DinoNuggies Apr 30 '22 at 01:58
  • @DinoNuggies Read [Strange behaviour of the pow function](https://stackoverflow.com/questions/18155883/strange-behaviour-of-the-pow-function) or [Does pow() work for int data type in C?](https://stackoverflow.com/questions/29787310/does-pow-work-for-int-data-type-in-c). You are likely to get errors even with small numbers. – Yksisarvinen Apr 30 '22 at 10:26
  • @Yksisarvinen Interesting, I tried recreating the problem but I couldn't, I don't think I'll have any issues with it, but thanks for the knowledge ahead of time ^-^ – DinoNuggies Apr 30 '22 at 20:30

1 Answers1

-2
int descend(int num) {
    vector<int> numVect;
    int result = 0;

    while (num) {
        numVect.push_back(num % 10);
        num /= 10;
    }

    sort(numVect.begin(), numVect.end());

    for (int i = 0; i < numVect.size(); i++) {
        result += numVect[i] * pow(10, numVect.size() - i - 1);
        cout << result;
    }
    return result;
}

int main()
{
    int output;
    cin >> output;
    output = descend(output);
    cout << "output: " << output << endl;
    return 0;
}
  • 2
    This is a code only answer, and answers with no explanation of what went wrong and how the proposed code solves the problem have an unfortunate tendency to result in [Cargo Cult Programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). This answer can be salvaged by providing the missing explanation. – user4581301 Apr 29 '22 at 22:33