I need a program where the user inputs an array of doubles and the program outputs the array sorted

  • Note: This question was severely edited since I first posted it here. The rules were moved to here, read them before posting any answer to understand the purpose of this. This was the first question created in the category.

    Imagine a lazy user on Stack Overflow asks this question:

    I need a program where the user inputs an array of doubles and the program outputs the array sorted. Could you please give the code?

    How could you create a piece of code that will troll this user? Create a piece of code that will appear useful to an inexperienced programmer but is utterly useless in practice.

    The winner is the most upvoted answer, except if the answer is somehow not eligible (for eligibility requirements, check the tag wiki description of ). If the previously most upvoted answer is beaten in the future in the number of upvotes after being accepted, the new best answer is accepted and the previous one is unaccepted. In the case of a tie, I will choose the winner at will among the tied ones or just wait a bit more.

    Answers that have no code are not eligible. They might be fun and get some upvotes, but they won't be accepted.

    Rules can be found at tag description.

    Note: This is a question. Please do not take the question and/or answers seriously. More information here.

    Are you also allowed to use other languages than C?

    Perhaps. I really want to avoid a case where just the choosen language is enough reason to troll the lazy OP. Otherwise, you could just plainly encode the correct solution using brainfuck, golfscript or some crazy language, but this would ruin the intention of this as everybody would do that. But if you use, lets say Java, C++ or Ruby, it would be ok. Further, in most homework questions the OP says what language he wants.

    OK, thanks. I just wanted to know whether I could use C#.

    “Crazy language”? I think the expression you are looking for is _esoteric language_.

    @ProgramFOX I edited the question to clarify your point.

    @manatwork Not exactly. Although this englobes all (or almost all) esoteric languages, there might still be non-esoteric languages that are too crazy for that, e.g. no one would think in using Logo for this, except if trolling. Anyway, I let that be a bit subjective. The chosen language should not be the sole reason of the trolling, and people are discouraging to upvote answers that uncreatively abuses the language choice. If you are intending in abusing the choosen language, be really creative about that.

    hmm... using code from this question and sorting the result?

    This is going to be a great tag. Had to laugh at quite a few posts here, but I think it isn't going to attract a whole lot of OP's. Anyway, nice :)

    This reminds me of something we did for someone suspected of using us for a homework problem: _Could U do it better?_.

    Isn't better to just post the link to good resource, where he/she could learn something ? Instead wasting his/her time and trolling ?

    @bluesm If someone has already decided to ask someone else to solve their problem instead of "wasting" their own time learning, posting a link to where they can learn on their own isn't going to do any good.

    "The idea of this category is..." - it seems like this belong on the tag wiki, not in the question itself.

    @Dukeling Yes, this is true. But since this is the first question in the category, I added the description here to introduce it. Further questions could simply reference the category and go to what the lazy OP asks.

    That does make sense, but the problem is that, if someone who already knows about this (presumably from other, newer questions) comes across this question, about 90% of the question would be unnecessary. Given that [se] questions are supposed to be in it for the long run, I don't think it should be here. Rather in the tag wiki with an associated Meta post, if any discussion is desired. But anyway, that's just my opinion.

    I believe the category would be better if the rules stated that the program should seem ok to the lazy student, but absolutely not to their professor. Otherwise we will get `print [the output requirement from spec]` in every question at least once.

    I suggested an edit to the tag wiki of code-trolling. Not sure if we should edit the description out; I'll leave that up to the OP/other users

    I opened a meta discussion for the rules, hoping if will be of help: http://meta.codegolf.stackexchange.com/questions/746/code-troll-rules

    silly, what is the point? to humiliate and discourage? i get that sometimes people are just trying to get their homework done, but really its easier just to say so and point them in the right direction.

    At this rate, you might get the second *ever* Great Question badge on this site :-D

    Given the limited state of a lot of PRNGs, I wonder how many of these bogosorts are actually capable of terminating at all on non-trivial input.

    Wow, this question's about to get 100 upvotes and 10,000 views in less than 24 hours!

    @JoeZ. Yeah, I could not even dream about that before. Totally awesome. I am very pround of this and would like to thank everybody.

    So, @Victor, I decided to continue the trend, to help expand the category.

    @JoeZ. You were right. It got 100 upvotes and 10,000 views in less than 24 hours (it took some 18 or 19 hours)! Further it already has 80 answers. Got two gold badges!

  • AShelly

    AShelly Correct answer

    7 years ago

    Sometimes the community here doesn't like to help with homework. That's why you are getting so many joke answers. But I like to help. Here is a complete solution in 'C' (since I assume you want to learn "programming", not "scripting" with Java or Ruby). I've included many tips that I wish I had known when I was first learning

    #include <stdio.h>

    //Always use meaningful names for types
    typedef unsigned char boolean;
    #define True 't'
    #define FALSE (!True)

    //this is a really neat trick for swapping values efficiently
    void swap(long* a,long *b) { *a=*a^*b;*b=*b^*a;*a=*a^*b; }

    //Here's a readability improvement
    #define until(condition) while(!(condition))

    int main(int n, char*args[]){
    double *d;
    int i;
    char input[5]; //should be long enough for most doubles.
    boolean sorted = FALSE;

    //In C, you need to specify the array size beforehand, so ask
    printf("Please enter the length of the array\n");
    //scan the input string and convert to a value

    //allocate space, make sure you get the order of arguments right.
    d = calloc(sizeof(double),n);

    //Get and sort the array
    until (sorted) {

    for (i=0;i<n;i++) {
    //It's important to always ask nicely
    printf("Please enter the %d%s array item\n",i,i==1?"st":"th");
    //do a compare and exchange sort:
    sorted = !sorted; //not sorted
    //check all the items
    printf("%d %d\n",i,n);
    for (i=1;i<n;i++) {
    if (d[i]<d[i-1]) {
    sorted = FALSE;
    //show results
    printf("The array is%ssorted\n",sorted?" ":" not "); }
    //use the --> "downto operator" for counting downto 0.
    for (;n-->0;) printf("%lf\n",*d++);

    almost all the advice is wrong, and it simply keeps asking for the input list until you enter it already sorted.

    +1, for `1st, 2th, 3th, 4th...` and the downto operator--very advanced C programing techniques.

    Should use `sscanf(input, "%5s", &input[0])`, otherwise there might be overrun bugs while parsing the input. And input should be declared `char input[sizeof(int)+1]`, for backward compatibility with 64-bit systems.

    `i==1?"st":"th"` hahaha...

    Java is not a scripting language...

    Java has garbage collection. Therefore Java is for "scripting", not real programming. That's basic CS101. (so says the troll.)

    @AShelly this is perfect, has all the nuanced hallmarks of the self-taught know-it-all C lifer.

    Good thing you remembered to put the `calloc` parameters in the right order! It creates such hard-to-find bugs when they are mixed up.

    You forgot to `free` the calloc'ed memory. That's something one should never forget! To be on the safe side, free before the until-loop

    What is this line doing? Tokenizing the first value from the line? `sscanf( input, "%s", &input[0] )`

    I love this entire thing, but +1 for the `i==1?"st":"th"` and "scripting in Java" troll comment ;)

    @redolent, `sscanf` stands for "String SCAN Format", so it's obviously taking the input String and Scanning it into the correct Format. As you probably know, passing an unformatted string to `atol` would cause undefined behavior.

    I really do not want to accept my own answer. So, I will wait a bit more to see if this answer eventually beats it.

    Now I am going to want `until` every time I have to use `while (!condition)`

    @GuySirton That `i==1?"st":"th"` isn’t so hot. At ELU we prefer `i >= 10 && i <= 20 ? "th" : i%10 == 1 ? "st" : i%10 == 2 ? "nd" : i%10 == 3 ? "rd" : "th"` instead, for cromulently self-evident reasons. :)

    @tchrist, That's beauty of the simple version. If the OP tries to fix it, he gets lead down the path to madness leading to your version.

    @AShelly One man’s madness is the previous one’s job security. The more I look at your answer, the more I like it.

    Telling a new programmer to use `atol()` - that's just _mean_ :P

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM