Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
decimal_to_binary_recursion.c File Reference

Convert decimal to binary using recursion algorithm. More...

#include <assert.h>
Include dependency graph for decimal_to_binary_recursion.c:

Functions

int decimal_to_binary (unsigned int number)
 Decimal to binary using recursion algorithm. More...
 
void test ()
 Test function. More...
 
int main ()
 Driver Code.
 

Detailed Description

Convert decimal to binary using recursion algorithm.

Function Documentation

◆ decimal_to_binary()

int decimal_to_binary ( unsigned int  number)

Decimal to binary using recursion algorithm.

For example, if number = 5, the function returns the decimal integer 101.

Parameters
numberpositive integer number to convert
Returns
integer with digits representing binary value representation of number.
15 {
16  return number == 0 ? 0 : number % 2 + 10 * decimal_to_binary(number / 2);
17 }

◆ test()

test ( )

Test function.

A function to test the kMeans function Generates 100000 points in a circle of radius 20.0 with center at (0,0) and cluster them into 5 clusters

Output for 100000 points divided in 5 clusters

21 {
22  const int sets[][2] = {
23  {0, 0}, {1, 1}, {2, 10}, {3, 11}, {4, 100}, {6, 110}, {7, 111},
24  /* add more data sets to test */
25  };
26 
27  for (int i = 0, size = sizeof(sets) / sizeof(sets[0]); i < size; ++i)
28  {
29  assert(decimal_to_binary(sets[i][0]) == sets[i][1]);
30  }
31 }
Here is the call graph for this function:
decimal_to_binary
int decimal_to_binary(unsigned int number)
Decimal to binary using recursion algorithm.
Definition: decimal_to_binary_recursion.c:14