Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, making tests run much faster; also it makes it easier to distinguish bugs in the I/O from bugs in the algorithm

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 // else both odd, or both even
 // return *a - *b might overflow, so avoid that
 return (*a > *b) - (*a < *b);
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


Additional problems with the supporting code

##Additional problems with the supporting code WhenWhen reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, making tests run much faster; also it makes it easier to distinguish bugs in the I/O from bugs in the algorithm

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 // else both odd, or both even
 // return *a - *b might overflow, so avoid that
 return (*a > *b) - (*a < *b);
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


##Additional problems with the supporting code When reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, making tests run much faster; also it makes it easier to distinguish bugs in the I/O from bugs in the algorithm

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 // else both odd, or both even
 // return *a - *b might overflow, so avoid that
 return (*a > *b) - (*a < *b);
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


Additional problems with the supporting code

When reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

Avoid signed overflow; thanks to Roland Illig
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, making tests run much faster; also it makes it easier to distinguish bugs in the I/O from bugs in the algorithm

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 // else both odd, or both even
 // return *a - *b might overflow, so avoid that
  return (*a > *b) - *b;(*a < *b);
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


##Additional problems with the supporting code When reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, making tests run much faster; also it makes it easier to distinguish bugs in the I/O from bugs in the algorithm

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 else
 return *a - *b;
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


##Additional problems with the supporting code When reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, making tests run much faster; also it makes it easier to distinguish bugs in the I/O from bugs in the algorithm

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 // else both odd, or both even
 // return *a - *b might overflow, so avoid that
  return (*a > *b) - (*a < *b);
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


##Additional problems with the supporting code When reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

Expand on testability
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, somaking tests run much faster.faster; also it makes it easier to distinguish bugs in the I/O from bugs in the algorithm

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 else
 return *a - *b;
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


##Additional problems with the supporting code When reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, so much faster.

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 else
 return *a - *b;
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


##Additional problems with the supporting code When reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

Firstly, we should separate the sorting from the reading of inputs and writing of outputs. We can create a function sort_evens_first() - that's the foundation of writing re-usable code. An advantage (even in this small program) is that a separable function can more easily be tested - no need for an external script to run many instances of the program with different inputs, making tests run much faster; also it makes it easier to distinguish bugs in the I/O from bugs in the algorithm

Secondly, the Standard Library provides us with qsort() to save us having to re-implement sort every time (and it's usually more efficient than the bubble sort implemented here). We need to give it a comparator function as follows:

  • if one element is even and the other is odd, the even number sorts before the odd one,
  • else, the numerically smaller one is lower.

That looks like:

int compare_evens_first(const void *va, const void *vb)
{
 const int *a = va;
 const int *b = vb;
 if (*a % 2 != *b % 2)
 return (*a % 2) - (*b % 2);
 else
 return *a - *b;
}

Then we can simply use it:

#include <stdlib.h>
void sort_evens_first(int *array, size_t count)
{
 qsort(array, count, sizeof *array, compare_evens_first);
}

N.B. I've not had time to test this code; bugs may be lurking.


##Additional problems with the supporting code When reading input with scanf() and family, it is essential to confirm the return value before using any of the results.

It's less important to check the result of printf() as failure there is less likely to lead to bad outcomes, but it's still worth considering so that we can return EXIT_FAILURE if the output wasn't successfully written (it might not be obvious to the user if directed to a file or pipeline, for example).

We should also check when we read our inputs; function aids auto tests
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
Make function name consistent with implementation
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
Fix comparison when one number is even and the other is odd
Source Link
1201ProgramAlarm
  • 7.8k
  • 2
  • 23
  • 39
Loading
Source Link
Toby Speight
  • 87.8k
  • 14
  • 104
  • 325
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /