Advertisement
saleemthp

Project 1_updated

Sep 18th, 2021
1,065
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdlib.h>
  3. using namespace std;
  4. //function that will intialize array
  5. void intialize_array(int arr[],int size)
  6. {
  7.     for(int i=0;i<size;i++)
  8.     {
  9.         arr[i]=i;
  10.     }
  11. }
  12. void print_array(const int arr[],int size)
  13. {
  14.     cout<<"[";
  15.     for(int i=0;i<size;i++)
  16.     {
  17.         cout<<arr[i]<<" ";
  18.     }
  19.     cout<<"]"<<endl;
  20. }
  21.  
  22. int * shuffle_array(const int arr[], int size)
  23. {
  24.     int *NewArray1 = new int[size];
  25.     for(int i=0; i<size; i++)
  26.     {
  27.         NewArray1[i] = arr[i];
  28.     }
  29.  
  30.     for(int i=size-1; i>=0; i--) {
  31.         int j = rand() % (i+1);
  32.  
  33.         int temp = NewArray1[j];
  34.         NewArray1[j] = NewArray1[i];
  35.        NewArray1[i] = temp;
  36.     }
  37.     return NewArray1;
  38. }
  39. int *createOddArray(const int arr[], int size, int &oddnum)
  40. {
  41.     int *OddArray = new int[size],j=0;
  42.     for(int i = 0;i<size;i++)
  43.     {
  44.         if(arr[i] % 2 == 1)
  45.         {
  46.             OddArray[j]=arr[i];
  47.             j++;
  48.         }
  49.        
  50.     }
  51.     oddnum=j;
  52.     return OddArray;
  53. }
  54. int *createEvenArray(const int arr[], int size, int &evennum)
  55. {
  56.     int *EvenArray = new int[size],j=0;
  57.     for(int i = 0;i<size;i++)
  58.     {
  59.         if(arr[i] % 2 == 0)
  60.         {
  61.             EvenArray[j]=arr[i];
  62.             j++;
  63.         }
  64.        
  65.     }
  66.     evennum=j;
  67.     return EvenArray;
  68. }
  69. void sort_array(int *arr, int size)
  70. {
  71.     int min;  // Index of minimum number
  72.  
  73.     for (int i = 0; i < size - 1; i++) {
  74.         min= i;
  75.         // Find index of the minimum number in the array after index i
  76.         for (int j = i + 1; j < size; j++)
  77.             if (arr[j] < arr[min])
  78.                 min= j;
  79.  
  80.         // Perform a swap operation
  81.         int temp = arr[min];
  82.         arr[min] = arr[i];
  83.         arr[i] = temp;
  84.     }
  85. }
  86.  
  87. void array_war(int *firstArray, int firstArraySize, int * secondArray, int secondArraySize)
  88. {
  89.     int arraySize=0;
  90.     if(firstArraySize>secondArraySize)
  91.     {
  92.     arraySize = firstArraySize;
  93.     }
  94.     else
  95.     {
  96.     arraySize = secondArraySize;
  97.     }
  98.     int * newArray =new int[arraySize];
  99.     int i=0;
  100. while(i<arraySize)
  101. {
  102.     if(i<firstArraySize && i<secondArraySize)
  103.     {
  104.  
  105.         if(firstArray[i]>secondArray[i])
  106.         {
  107.             newArray[i] = firstArray[i];
  108.         }
  109.         else
  110.         {
  111.             newArray[i] = secondArray[i];
  112.         }
  113.     }
  114.     else if(i<firstArraySize)
  115.     {
  116.         newArray[i] = firstArray[i];
  117.     }
  118.     else if(i<secondArraySize)
  119.     {
  120.         newArray[i] = secondArray[i];
  121.     }
  122.  
  123.     i++;
  124.  
  125. }
  126.  
  127. cout<<"ArrayFight winners were:";
  128.  
  129. print_array(newArray,arraySize);
  130.  
  131. cout<<"Sorted ArrayFight winners were:";
  132.  
  133. sort_array(newArray,arraySize);
  134.  
  135. print_array(newArray,arraySize);
  136.  
  137. }
  138.  
  139. int main()
  140. {
  141.     int arraysize;
  142.     cout<< "What is the size of your array (from 1 to 52)";
  143.     cin>> arraysize;
  144.     while(arraysize <=0 || arraysize > 52)
  145.     {
  146.         cout<< "Select your array size from 1 to 52";
  147.         cin>> arraysize;
  148.     }
  149.     int array1[arraysize];
  150.     intialize_array(array1,arraysize);
  151.     cout<<"Original array is:";
  152.     print_array(array1,arraysize);
  153.     int *shuffled=shuffle_array(array1,arraysize);
  154.     cout<<"Shuffled array is :";
  155.     print_array(shuffled,arraysize);
  156.     int oddnum=0;
  157.     int *oddnumArray = createOddArray(array1,arraysize,oddnum);
  158.     cout<<"After call to createOddArray, oddArray is:";
  159.     print_array(oddnumArray,oddnum);
  160.     int evennum=0;
  161.     int *evennumArray = createEvenArray(array1,arraysize,evennum);
  162.     cout<<"After call to createEvenArrayArray, EvenArray is:";
  163.     print_array(evennumArray,evennum);
  164.     array_war(oddnumArray,oddnum,evennumArray,evennum);
  165.    
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement