Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- void sort(int arr[], int n)
- {
- //This function sorts the array in ascending order
- //using bubble sort technique
- for (int i = 0; i < n-1; i++)
- for (int j = 0; j < n-i-1; j++)
- if (arr[j] > arr[j+1])
- {
- //Swap adjacent elements
- int temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- int findMode(int arr[], int n)
- {
- //Function to calculate mode of sorted array
- //2 D array to store values with their frequency
- int** a = new int*[n];
- for(int i = 0; i < n; ++i)
- a[i] = new int[2];
- int k = 0;
- int mode, maxFrequency = 0, tempFrequency = 0;
- for(int i = 0; i < n; i++)
- {
- tempFrequency = 0;
- if((arr[i] == arr[i+1])&&(i < n-1))
- {
- for(int j = i; j < n - 1; j++)
- {
- if((arr[j] == arr[j+1])&&(j < n-1))
- {
- a[k][0] = arr[j];
- tempFrequency++;
- i++;
- }
- else
- break;
- }
- tempFrequency++;
- a[k][1] = tempFrequency;
- k++;
- if(tempFrequency > maxFrequency)
- maxFrequency = tempFrequency;
- }
- else
- {
- a[k][0] = arr[i];//Value
- a[k][1] = 1;//Frequency of value
- tempFrequency = 1;
- if(tempFrequency > maxFrequency)
- maxFrequency = tempFrequency;
- k++;
- }
- }
- //Will print if there are more than one mode
- cout<<"\nMode: ";
- for(int i = 0; i < k; i++)
- {
- if(a[i][1] == maxFrequency)
- cout<<a[i][0]<<" ";
- }
- }
- float findMedian(int arr[], int n)
- {
- //Function to calculate median of sorted array
- float median;
- int middle;
- if(n % 2 == 0)
- {
- //Two middle pairs in case of even values
- middle = n/2 - 1;
- median = (arr[middle] + arr[middle+1])/2.0;
- }
- else
- {
- //If n is odd then there is only one middle value
- middle = n/2;
- median = arr[middle];
- }
- return median;
- }
- int main()
- {
- int n;
- do
- {
- cout<<"How many students were surveyed?: ";
- cin>>n;
- }
- while(n < 0);
- int *arr = new int[n];
- for(int i = 0; i < n;i++)
- {
- cout<<"Enter number of movies for student "<<i+1<<" : ";
- cin>>arr[i];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement