jeudi 10 mai 2018

C - number sorting description

So i have wrote code for C program which sorts an array of 50 of random numbers from 0 - 255. The highest number once sorted is displayed on 8 LEDs as binary digits. The same for the lowest sorted number. I fully understand the program however i need to write up a brief description of it and am struggling with these two functions :

void binaryLowesttNumber()
{
int remainder = lowArray; /* remainder is what I am going to be left with after taking away the value of the pins,
sets remainder as the lowest sorted number */
int j = 128;  // value of pins in binary
int i = 7;    // index of current pin
while (i >= 0)
{
if (remainder >= j) //if remainder is larger or equal to the value of the current pin
{
  digitalWrite(pinArray[i], HIGH);
  remainder = remainder - j;   // takes value of pin away from remainder
  j = j >> 1;   // halves the value of j
  i--;   // moves to next pin
}
else
{
  j = j >> 1;   // halves the value of j
  i--;   // moves to next pin
}
}
}

void binaryHighestNumber()
{
int remainder = highArray; // same as binaryLowestNumber function except the remainder will be the highest sorted number
int i = 128;
int thisPin = 7;

while (remainder > 0)
{
while (remainder >= i)
{
  double j = i / 2;
  digitalWrite(pinArray[thisPin], HIGH);
  remainder = remainder - i;
  i = i - j;
  thisPin--;
}
while (remainder < i)
{
  int j = i / 2;
  if (j >= 1)
  {
    i = i - j;
    thisPin--;
  }
  else
  {
    i = 0;
  }
}
}
}




Aucun commentaire:

Enregistrer un commentaire