Share Pastebin
Guest
Public paste!

C++ Stuff

By: a guest | Sep 9th, 2010 | Syntax: C++ | Size: 1.57 KB | Hits: 25 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. // NOTE: It is assumed that SIZE contains the number of elements in an array and not its subscript (which would start at zero).
  2. // NOTE2: NONE OF THE FUNCTIONS HAVE BEEN DEBUGGED YET.
  3.  
  4.  
  5. // The function accepts two integer arrays and a size argument.
  6. // It returns true if the arrays have the same values, but in the opposite order.
  7. bool equalButOpposite(int* a, int* b, const unsigned short SIZE)
  8. {
  9.         for(int i = 0; i < SIZE; i++)
  10.                 if(a[i] != b[SIZE - 1 - i])
  11.                         return false;
  12.         return true;
  13. }
  14.  
  15. // The function accepts an array of floats, and a size argument.
  16. // It returns true if each element is the average of it's neighbors on either side (with the exception of the first and last elements, they can have any value) otherwise it should return false.
  17. bool averageOfNeighbors(float* a, const unsigned short SIZE)
  18. {
  19.         for(int i = 1; i < SIZE - 1; i++)
  20.                 if((a[i - 1] + a[i + 1]) * 0.5 != a[i])
  21.                         return false;
  22.         return true;
  23. }
  24.  
  25. // This function accepts an array of chars, and a size argument.
  26. // It should return true if each value in the array is included exactly twice. No values appear just once in the array, and no values appear more than twice.
  27. bool allValuesArePaired(char* a, const unsigned short SIZE)
  28. {
  29.         unsigned short counter;
  30.         if(SIZE % 2 != 0) // No array with an odd number of elements will will have two of each element;
  31.                 return false;
  32.         else
  33.         {
  34.                 for(int i = 0; i < SIZE - 1; i++)
  35.                 {
  36.                         counter = 1;
  37.                         for(int j = i + 1; j < SIZE; )
  38.                         {
  39.                                 if(a[i] == a[j])
  40.                                         counter++;
  41.                         }
  42.                         if(counter != 2)
  43.                                 return false;
  44.                 }
  45.         }
  46.         return true;
  47. }