Advertisement
HighTechRedneck_

Untitled

Jul 3rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.55 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. void sort(int arr[], int n)
  6. {
  7.     //This function sorts the array in ascending order
  8.     //using bubble sort technique
  9.     for (int i = 0; i < n-1; i++)
  10.         for (int j = 0; j < n-i-1; j++)
  11.             if (arr[j] > arr[j+1])
  12.             {
  13.                 //Swap adjacent elements
  14.                 int temp = arr[j];
  15.                 arr[j] = arr[j+1];
  16.                 arr[j+1] = temp;
  17.             }
  18. }
  19.  
  20. int findMode(int arr[], int n)
  21. {
  22.     //Function to calculate mode of sorted array
  23.     //2 D array to store values with their frequency
  24.     int** a = new int*[n];
  25.     for(int i = 0; i < n; ++i)
  26.         a[i] = new int[2];
  27.     int k = 0;
  28.     int mode, maxFrequency = 0, tempFrequency = 0;
  29.     for(int i = 0; i < n; i++)
  30.     {
  31.         tempFrequency = 0;
  32.         if((arr[i] == arr[i+1])&&(i < n-1))
  33.         {
  34.             for(int j = i; j < n - 1; j++)
  35.             {
  36.                 if((arr[j] == arr[j+1])&&(j < n-1))
  37.                 {
  38.                     a[k][0] = arr[j];
  39.                     tempFrequency++;
  40.                     i++;
  41.                 }
  42.                 else
  43.                     break;
  44.             }
  45.             tempFrequency++;
  46.             a[k][1] = tempFrequency;
  47.             k++;
  48.             if(tempFrequency > maxFrequency)
  49.             maxFrequency = tempFrequency;
  50.         }
  51.         else
  52.         {
  53.             a[k][0] = arr[i];//Value
  54.             a[k][1] = 1;//Frequency of value
  55.             tempFrequency = 1;
  56.             if(tempFrequency > maxFrequency)
  57.                 maxFrequency = tempFrequency;
  58.             k++;
  59.         }
  60.     }
  61.     //Will print if there are more than one mode
  62.     cout<<"\nMode: ";
  63.     for(int i = 0; i < k; i++)
  64.     {
  65.         if(a[i][1] == maxFrequency)
  66.             cout<<a[i][0]<<" ";
  67.     }
  68. }
  69. float findMedian(int arr[], int n)
  70. {
  71.     //Function to calculate median of sorted array
  72.     float median;
  73.     int middle;
  74.     if(n % 2 == 0)
  75.     {
  76.         //Two middle pairs in case of even values
  77.         middle = n/2 - 1;
  78.         median = (arr[middle] + arr[middle+1])/2.0;
  79.     }
  80.     else
  81.     {
  82.         //If n is odd then there is only one middle value
  83.         middle = n/2;
  84.         median = arr[middle];
  85.     }
  86.     return median;
  87. }
  88.  
  89. int main()
  90. {
  91.     int n;
  92.     do
  93.     {
  94.         cout<<"How many students were surveyed?: ";
  95.         cin>>n;
  96.     }
  97.     while(n < 0);
  98.     int *arr = new int[n];
  99.     for(int i = 0; i < n;i++)
  100.     {
  101.         cout<<"Enter number of movies for student "<<i+1<<" : ";
  102.         cin>>arr[i];
  103.        
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement