1

I've been playing around a bit and I've done a program that plays the lottery (EuroMillions), and I was able to do it.

I got four arrays, two for the numbers and two for the stars, and then I'd sort both so I could more easily compare them.

So here's my question, can I sort only the first five numbers and then the last two, separately? Or do I really need to have four arrays like I did?

What I used to sort:

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

and

qsort(b, 7, sizeof(int), cmpfunc);

If the code is needed I can post it, but it's rather big.

2
  • What five numbers? What does that mean? Commented Nov 26, 2015 at 16:40
  • Add 100 to the stars so they get sorted above the non-stars. Commented Nov 26, 2015 at 16:42

1 Answer 1

3

If b is an int array with 7 elements you can do this

qsort(b, 5, sizeof(int), cmpfunc); // sort 5 elements starting from b 
qsort(b + 5, 2, sizeof(int), cmpfunc); // sort 2 elements starting from b+5

In each case the first 2 arguments are the pointer to the start of a memory block and the number of elements.

So if b is defined as

int b[] = {8, 2, 12, 4, 5, 7, 6};

you will get

{2, 4, 5, 8, 12, 6,7}
Sign up to request clarification or add additional context in comments.

5 Comments

This answer looks correct assuming qsort is the one defined in stdlib
I tried using b[5] instead of b+5. Thank you for the help!
This is so trivial, compared to actually figuring out qsort itself, I'm surprised OP was actually asking for this answer.
@this It's probably fairly easy to skip understanding how pointers actually work and start looking for code samples of qsort that can be minimally-adapted.
@Ike Unfortunately. But samples found online must be correct because they are online and their author claims they are correct. /s

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.