Advertisement
saleemthp

Project 1

Sep 16th, 2021
5,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Welcome to GDB Online.
  4. GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
  5. C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
  6. Code, Compile, Run and Debug online from anywhere in world.
  7.  
  8. *******************************************************************************/
  9. #include<iostream>
  10. #include<stdlib.h>
  11. using namespace std;
  12. //function that will intialize array
  13. void intialize_array(int arr[],int size)
  14. {
  15.     for(int i=0;i<size;i++)
  16.     {
  17.         arr[i]=i;
  18.     }
  19. }
  20. void print_array(const int arr[],int size)
  21. {
  22.     cout<<"[";
  23.     for(int i=0;i<size;i++)
  24.     {
  25.         cout<<arr[i]<<",";
  26.     }
  27.     cout<<"]"<<endl;
  28. }
  29.  
  30. int * shuffle_array(const int arr[], int size)
  31. {
  32.     int *NewArray1 = new int[size];
  33.     for(int i=0; i<size; i++)
  34.     {
  35.         NewArray1[i] = arr[i];
  36.     }
  37.  
  38.     for(int i=size-1; i>=0; i--) {
  39.         int j = rand() % (i+1);
  40.  
  41.         int temp = NewArray1[j];
  42.         NewArray1[j] = NewArray1[i];
  43.        NewArray1[i] = temp;
  44.     }
  45.     return NewArray1;
  46. }
  47. int *createOddArray(const int arr[], int size, int &oddnum)
  48. {
  49.     int *OddArray = new int[size],j=0;
  50.     for(int i = 0;i<size;i++)
  51.     {
  52.         if(arr[i] % 2 == 1)
  53.         {
  54.             OddArray[j]=arr[i];
  55.             j++;
  56.         }
  57.        
  58.     }
  59.     oddnum=j;
  60.     return OddArray;
  61. }
  62. int *createEvenArray(const int arr[], int size, int &evennum)
  63. {
  64.     int *EvenArray = new int[size],j=0;
  65.     for(int i = 0;i<size;i++)
  66.     {
  67.         if(arr[i] % 2 == 0)
  68.         {
  69.             EvenArray[j]=arr[i];
  70.             j++;
  71.         }
  72.        
  73.     }
  74.     evennum=j;
  75.     return EvenArray;
  76. }
  77. int main()
  78. {
  79.     int arraysize;
  80.     cout<< "What is the size of your array (from 1 to 52)";
  81.     cin>> arraysize;
  82.     while(arraysize <=0 || arraysize > 52)
  83.     {
  84.         cout<< "Select your array size from 1 to 52";
  85.         cin>> arraysize;
  86.     }
  87.     int array1[arraysize];
  88.     intialize_array(array1,arraysize);
  89.     cout<<"The original array is:";
  90.     print_array(array1,arraysize);
  91.     int *shuffled=shuffle_array(array1,arraysize);
  92.     cout<<"The shuffled array is :";
  93.     print_array(shuffled,arraysize);
  94.     int oddnum;
  95.     int *oddnumArray = createOddArray(array1,arraysize,oddnum);
  96.     cout<<"The odd array is:";
  97.     print_array(oddnumArray,oddnum);
  98.     int evennum;
  99.     int *evennumArray = createEvenArray(array1,arraysize,evennum);
  100.     cout<<"The even array is";
  101.     print_array(evennumArray,evennum);
  102.    
  103.    
  104.     return 0;
  105. }
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement