Posted By

woofeR on 03/19/09


Tagged

array function loop parameter operator Pass for pointer BY void referance if-else remainder


Versions (?)

Array Splitter (random => odd & even)


 / Published in: C++
 

  1. //get an array and by using it, create two different array which are odd and even
  2. void arraySplitter(int *&array, int size, int &sizeEven, int &sizeOdd, int *&array2)
  3. {
  4. int even = 0;
  5. int odd = 0;
  6.  
  7. //determine how many member of given array is even and how many of them is odd
  8. for (int i = 0; i < size; i++)
  9. {
  10. if(array[i]%2==0)
  11. {
  12. even++;
  13. }
  14. else
  15. {
  16. odd++;
  17. }
  18. }
  19.  
  20. //by using even and odd parameter, creates two different array
  21. //which will be used soon
  22. int *arrayOdd= new int[even];
  23. int *arrayEven = new int[odd];
  24.  
  25. int m = 0;
  26. int n = 0;
  27.  
  28. //creates arrays which has only even or odd members
  29. for (int i = 0; i < size; i++)
  30. {
  31. if(array[i]%2==0)
  32. {
  33. arrayEven[m] = array[i];
  34. m++;
  35. }
  36. else
  37. {
  38. arrayOdd[n] = array[i];
  39. n++;
  40. }
  41. }
  42.  
  43. //returns arrays with pass by referance
  44. array = arrayEven;
  45. array2 = arrayOdd;
  46. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: woofeR on March 19, 2009

//tester int main(int argc, char *argv[]) {

int sizeEven = 0; int sizeOdd = 0;
int size = 10;
int *array = new int[size];
int *array2 = new int[size];

for(int i = 0 ; i < size; i++)
        array[i] = i;

arraySplitter(array, size, sizeEven, sizeOdd, array2);

for(int l = 0; l < 5; l++)
        cout
Posted By: woofeR on March 19, 2009

//tester int main(int argc, char *argv[]) {

int sizeEven = 0; int sizeOdd = 0;
int size = 10;
int *array = new int[size];
int *array2 = new int[size];

for(int i = 0 ; i < size; i++)
        array[i] = i;

arraySplitter(array, size, sizeEven, sizeOdd, array2);

for(int l = 0; l < 5; l++)
        cout

You need to login to post a comment.