Thread: Sorting this in C language.

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    19

    Sorting this in C language.

    hi, im new to C and wondering how i would be able to sort certain data so it goes from highest to lowest. such as :

    9 8 7 6 5 when inputted using a compiler.

    what would be the best way and how would i do it using source code?

    If someone wil be able to help, let me know and ill send my code !

    Many thanks! MF
    Last edited by MF1991; 01-26-2013 at 06:59 PM.

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The standard way in C is to use the qsort function. To sort from highest to lowest you would do it something like this

    Code:
    int int_compar_desc(const void *a, const void *b) {
        const int *ia = a;
        const int *ib = b;
        if (*ia > *ib)
            return -1;
        return (*ia < *ib);
    }
    
    int main()
    {
        int data[8] = {20, 1, 23, 4, 54, 23, 24, 97};
        qsort(data, 8, sizeof *data, int_compar_desc);
        return 0;
    }

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    what about using bubblesort? and what about if you have letters value? can i send u an example of my code in a private message?

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need to learn what a compiler is, and what it does, first by the sounds of it. Presumably you intend to use scanf or fgets to get the values from the user?
    Then you'd use the function called qsort, searching the web for how to use it.
    Finally, you'd post your attempt at this here, for everyone to see.
    Last edited by iMalc; 01-26-2013 at 07:11 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    If your input is letters, then you modify the compar function to compare letters. It is basically the same as for numbers, but you have to decide whether 'A' and 'a' are the same, etc.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Other duplicates of this thread:
    URGENTLY need help!
    Sorting C Language
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > If someone wil be able to help, let me know and ill send my code !
    Yes, post it here, and lots of people will be able to help you.

    > what about using bubblesort?
    Sure, it's good enough on small arrays.

    > and what about if you have letters value?
    'a' < 'b' works too.

    > can i send u an example of my code in a private message?
    No - post it here.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    So sorry people, im new to this forum!

    this is my code, i hope you will be able to help!

    #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct - Pastebin.com

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    Link is in the above reply (: i really need help on this.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    It seems incomplete. Can't you post the code here? Use the code tag and paste it inbetween them

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    Code:
    1. #include <stdio.h>
    2. #include <stdlib.h>
    3. #include <string.h>
    4. typedef struct {
    5. char* shortValue;
    6. char shortSuit[1];
    7. char value[20];
    8. char suit[20];
    9. } CARD;
    10. CARD cards[5];
    11. int score;
    12. int failed;
    13. int main(void){
    14. /* Putting space between the lines makes it look "Tidy" or "Professional" */
    15. printf("Welcome to Poker Game.\n\n");
    16. printf("Lets Start.\n\n");
    17. printf("Draw Your Cards.\n\n");
    18. for(int i = 0; i < 5; i++){
    19. cards[i].shortValue = calloc(1, sizeof(char));
    20. }
    21. scanf("%c%c %c%c %c%c %c%c %c%c", cards[0].shortValue, cards[0].shortSuit, cards[1].shortValue, cards[1].shortSuit, cards[2].shortValue, cards[2].shortSuit, cards[3].shortValue, cards[3].shortSuit, cards[4].shortValue, cards[4].shortSuit);
    22. /* Use of switch command for long if statement */
    23. /* Use value of 0 as Ten */
    24. /* For each card in input, run check to determine number/letter in word form */
    25. for(int i = 0; i < 5; i++){
    26. switch(cards[i].shortValue[0])
    27. {
    28. case '2':
    29. sprintf(cards[i].value, "Two");
    30. break;
    31. case '3':
    32. sprintf(cards[i].value, "Three");
    33. break;
    34. case '4':
    35. sprintf(cards[i].value, "Four");
    36. break;
    37. case '5':
    38. sprintf(cards[i].value, "Five");
    39. break;
    40. case '6':
    41. sprintf(cards[i].value, "Six");
    42. break;
    43. case '7':
    44. sprintf(cards[i].value, "Seven");
    45. break;
    46. case '8':
    47. sprintf(cards[i].value, "Eight");
    48. break;
    49. case '9':
    50. sprintf(cards[i].value, "Nine");
    51. break;
    52. case '0':
    53. sprintf(cards[i].value, "Ten");
    54. break;
    55. case 'J':
    56. sprintf(cards[i].value, "Jack");
    57. cards[i].value[5] = "11";
    58. score += 1;
    59. break;
    60. case 'Q':
    61. sprintf(cards[i].value, "Queen");
    62. cards[i].value[5] = "12";
    63. score += 1;
    64. break;
    65. case 'K':
    66. sprintf(cards[i].value, "King");
    67. cards[i].value[5] = "13";
    68. score += 1;
    69. break;
    70. case 'A':
    71. sprintf(cards[i].value, "Ace");
    72. cards[i].value[5] = "14";
    73. score += 1;
    74. break;
    75. default:
    76. printf("Invalid value character for card #%d. \n", i+1);
    77. failed = 1;
    78. break;
    79. }
    80. }
    81. /* Run check for all short suits in input */
    82. for(int i = 0; i < 5; i++)
    83. {
    84. switch(cards[i].shortSuit[0])
    85. {
    86. case 'C':
    87. sprintf(cards[i].suit, "Clubs");
    88. break;
    89. case 'D':
    90. sprintf(cards[i].suit, "Diamonds");
    91. break;
    92. case 'H':
    93. sprintf(cards[i].suit, "Hearts");
    94. break;
    95. case 'S':
    96. sprintf(cards[i].suit, "Spades");
    97. break;
    98. default:
    99. printf("Invalid suit character for card #%d.\n", i+1);
    100. failed = 1;
    101. break;
    102. }
    103. }
    104. if(failed == 1){
    105. printf("Invalid value or suit character. Program exiting.\n");
    106. return 0;
    107. }
    108. for(int i = 0; i < 5; i++)
    109. printf("%s of %s\n", cards[i].value, cards[i].suit);
    110. printf("\n"); }

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    This line is not correct:

    Code:
    sprintf(cards[i].value, "Queen");
               cards[i].value[5] = "12";
    You have a few more where you do something similar. What are you trying to do here? You store "Queen" into the character buffer `value' and then you want to tack on a "12" at the end?? I think it would make more sense to make a different member of your struct and then do something like this

    Code:
    sprintf(cards[i].value, "Queen");
    cards[i].valueAmount = 12;

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    i have a 5 card input like 5H 6D 8C 7S 4H and when i put them in, i want it to sort them in a list like this:

    4H
    5H
    6D
    7S
    8C

    but when i display them, it will print the names and of and then the suit. i want to be able to sort the data in order to make it go in order Highest value first to the lowest value

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Fix the errors in your code first. I mentioned it above. Fixing errors should be a prerequisite to adding anything more to the code.

  15. #15
    Registered User
    Join Date
    Jan 2013
    Posts
    19
    well i tried doing that but i kept getting error messages. do u have teamviewer 8 so i can possibly show you what i am doing so you can provide me with an answer and see for yourself?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 07-15-2012, 05:20 PM
  2. Is C++ or C language is a high Level language?
    By uthmankhale in forum C++ Programming
    Replies: 5
    Last Post: 08-25-2011, 06:00 PM
  3. What's the Difference Between a Programming Language and a Scripting Language?
    By Krak in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-15-2005, 04:46 PM
  4. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM
  5. Computer Language VS Spoken Language
    By Isometric in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 02-04-2002, 03:47 PM