Advertisement
Vickyyy01

SortEvenOddNumbersArray

Jan 13th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     int arrSize;
  9.     int matrix[100];
  10.     int oddNum[100];
  11.     int oddCtr = 0;
  12.     int evenNum[100];
  13.     int evenCtr = 0;
  14.     int temp = 0;
  15.     int a = 0;
  16.     int b = 0;
  17.  
  18.     cin >> arrSize;
  19.  
  20.     for(int i = 0; i < arrSize; i++)
  21.     {
  22.         int num = rand() % 1000 + 1;
  23.         matrix[i] = num;
  24.         cout << matrix[i] << " ";
  25.     }
  26.  
  27.     cout << endl << endl;
  28.  
  29.     while(a < arrSize)
  30.     {
  31.         if(matrix[a] % 2 != 0)
  32.         {
  33.             oddNum[oddCtr] = matrix[a];
  34.             oddCtr++;
  35.         }
  36.         a++;
  37.     }
  38.  
  39.     while(b < arrSize)
  40.     {
  41.         if(matrix[b] % 2 == 0)
  42.         {
  43.             evenNum[evenCtr] = matrix[b];
  44.             evenCtr++;
  45.         }
  46.         b++;
  47.     }
  48.  
  49.     for(int c = 1; c <= oddCtr - 1; c++)
  50.     {
  51.         for(int d = 0; d < oddCtr - c; d++)
  52.         {
  53.             if(oddNum[d] > oddNum[d+1])
  54.             {
  55.                 temp = oddNum[d+1];
  56.                 oddNum[d+1] = oddNum[d];
  57.                 oddNum[d] = temp;
  58.             }
  59.         }
  60.     }
  61.  
  62.     for(int c = 1; c <= evenCtr - 1; c++)
  63.     {
  64.         for(int d = 0; d < evenCtr - c; d++)
  65.         {
  66.             if(evenNum[d] < evenNum[d+1])
  67.             {
  68.                 temp = evenNum[d+1];
  69.                 evenNum[d+1] = evenNum[d];
  70.                 evenNum[d] = temp;
  71.             }
  72.         }
  73.     }
  74.  
  75.     for(int e = 0; e <= evenCtr-1; e++)
  76.     {
  77.         matrix[e] = evenNum[e];
  78.     }
  79.  
  80.     int g = 0;
  81.     int k = evenCtr;
  82.     while(k < arrSize)
  83.     {
  84.         matrix[k] = oddNum[g];
  85.         k++;
  86.         g++;
  87.     }
  88.  
  89.     for(int j = 0; j < arrSize; j++)
  90.     {
  91.         cout << matrix[j] << " ";
  92.     }
  93.  
  94.     cout << endl;
  95. }
  96.  
  97. /*
  98. ----------------------------------------------------------------------------------------------------------------------------------
  99. ----------------------------------------------------------------------------------------------------------------------------------
  100. Creates and array with random numbers and sorts them in even numbers in decresing order followed by odd numbers in increasing order
  101. ----------------------------------------------------------------------------------------------------------------------------------
  102. ----------------------------------------------------------------------------------------------------------------------------------
  103. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement