Thread: Sorting a struct array(numerical+alphabetical)

  1. #31
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    Thanks, how am I supposed to save and use the "i" variable? Do you have a suggestion of a page that explain it or could you do it?

  2. #32
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by emano12 View Post
    Thanks, how am I supposed to save and use the "i" variable? Do you have a suggestion of a page that explain it or could you do it?
    Yes, I can explain it. You save i by assigning it to another variable after the loop, which I will name howmany. Then, assuming you exercised some agency over your own code, and kept the value of howmany in scope somehow where you sort, you would call qsort() using howmany. This will make it so qsort sorts howmany elements instead of 100, since sorting 100 is wrong and actually causes your output problems.

  3. #33
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    Quote Originally Posted by whiteflags View Post
    Yes, I can explain it. You save i by assigning it to another variable after the loop, which I will name howmany. Then, assuming you exercised some agency over your own code, and kept the value of howmany in scope somehow where you sort, you would call qsort() using howmany. This will make it so qsort sorts howmany elements instead of 100, since sorting 100 is wrong and actually causes your output problems.
    So let us say that the loop is ending here
    }
    int howmany = i;

    will howmany have the value of "i" after the loop? , and how can I send it to the qsort?
    Last edited by emano12; 01-31-2018 at 08:48 AM.

  4. #34
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    For the first question: you could always print the value of howmany immediately after you assign i to it, to double check.

    I'm going to flip your second question around on you, to make you think more about what you're doing, instead of just waiting for us to spoon feed you line by line. This will help you learn better.

    How do you think you can send it to qsort?

    Try a few different things and see which ones seem to work.

  5. #35
    Registered User
    Join Date
    Jan 2018
    Posts
    18
    Quote Originally Posted by anduril462 View Post
    For the first question: you could always print the value of howmany immediately after you assign i to it, to double check.

    I'm going to flip your second question around on you, to make you think more about what you're doing, instead of just waiting for us to spoon feed you line by line. This will help you learn better.

    How do you think you can send it to qsort?

    Try a few different things and see which ones seem to work.
    I am thinking about like "return i", or "return howmany"(and the count of i, when I printed is was 101), and without the qsort function it was 16(as it should be)
    Last edited by emano12; 02-01-2018 at 07:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting array of struct inside struct
    By blueboyz in forum C Programming
    Replies: 13
    Last Post: 04-24-2012, 02:15 AM
  2. Alphabetical sorting in a character array?
    By YPavluk in forum C Programming
    Replies: 10
    Last Post: 10-28-2011, 08:35 AM
  3. C++ Alphabetical sorting, etc Help
    By Lucifix in forum C Programming
    Replies: 5
    Last Post: 05-17-2010, 01:29 AM
  4. Alphabetical sorting function
    By typer in forum C Programming
    Replies: 6
    Last Post: 05-20-2006, 02:35 AM
  5. Sorting in alphabetical order
    By fkheng in forum C Programming
    Replies: 3
    Last Post: 08-24-2003, 09:07 AM

Tags for this Thread