<\/span><\/h2>\n\n\n\nIn this challenge, we practice calculating the mean<\/em>, median<\/em>, and mode<\/em>.<\/p>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nGiven an array, X<\/em><\/strong>, of N<\/em><\/strong> integers, calculate and print the respective mean<\/em>, median<\/em>, and mode<\/em> on separate lines. If your array contains more than one modal value<\/em>, choose the numerically smallest one.<\/p>\n\n\n\nNote:<\/strong> Other than the modal value (which will always be an integer), your answers should be in decimal form, rounded to a scale of 1<\/strong> decimal place (i.e., 12.3<\/strong>, 7.0<\/strong> format).<\/p>\n\n\n\nExample<\/strong><\/p>\n\n\n\nN <\/em>= 6<\/strong>
X<\/em> = [1, 2, 3, 4, 5, 5<\/strong>]
The mean is 20\/6 =<\/strong> 3.3<\/strong>.
The median is 3+4\/2<\/strong> 3.5<\/strong>.
The mode is 5<\/strong> because 5<\/strong> occurs most frequently.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains an integer, N<\/em><\/strong>, the number of elements in the array.
The second line contains N<\/strong><\/em> space-separated integers that describe the array’s elements.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 10 <= N<\/em> <= 2500<\/strong><\/li>
- 0 < x<\/em>[i<\/em>] <= 105<\/sup><\/strong> ,where x<\/em>[i<\/em>]<\/strong> is the ith<\/sup><\/strong><\/em> element of the array.<\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nPrint 3<\/strong> lines of output in the following order:<\/p>\n\n\n\n- Print the mean<\/em> on the first line to a scale of 1<\/strong> decimal place (i.e., 12.3<\/strong>, 7.0<\/strong>).<\/li>
- Print the median<\/em> on a new line, to a scale of 1<\/strong> decimal place (i.e., 12.3<\/strong>, 7.0<\/strong>).<\/li>
- Print the mode<\/em> on a new line. If more than one such value exists, print the numerically smallest one.<\/li><\/ol>\n\n\n\n
Sample Input<\/strong><\/p>\n\n\n\n10\n64630 11735 14216 99233 14470 4978 73429 38120 51135 67060<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\n43900.6\n44627.5\n4978<\/code><\/pre>\n\n\n\n<\/span>Explanation<\/strong><\/span><\/h2>\n\n\n\nMean:<\/strong>
We sum all elements in the array, divide the sum by , and print our result on a new line. U<\/em> = 439006 \/ 10 = 43900.6<\/strong><\/p>\n\n\n\nMedian:<\/strong>
To calculate the median, we need the elements of the array to be sorted in either non-increasing or non-decreasing order. The sorted array X<\/em> = {4978, 11735, 14216, 14470, 38120, 51135, 64630, 67060, 73429, 99233}<\/strong>. We then average the two middle elements: median<\/em> = 89255\/2 = 44627.5<\/strong><\/p>\n\n\n\nMode:<\/strong>
We can find the number of occurrences of all the elements in the array:<\/p>\n\n\n\n4978 : 1\n11735 : 1\n14216 : 1\n14470 : 1\n38120 : 1\n51135 : 1\n64630 : 1\n67060 : 1\n73429 : 1\n99233 : 1<\/code><\/pre>\n\n\n\nEvery number occurs once, making 1<\/strong> the maximum number of occurrences for any number in X<\/em><\/strong>. Because we have multiple values to choose from, we want to select the smallest one, 4978<\/strong>, and print it on a new line.<\/p>\n\n\n\n<\/span>Solution – Day 0: Mean, Median, and Mode Solution<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <cmath>\n#include <cstdio>\n#include <vector>\n#include <iostream>\n#include <algorithm>\nusing namespace std;\nconst int max_n = 1e5+5;\n\nint main() \n{\n \/* Enter your code here. Read input from STDIN. Print output to STDOUT *\/ \n \n int n; cin>>n; int a[max_n]; long sum=0;\n \n for(auto i=0;i<n;i++) \n {\n cin>>a[i];\n sum+=a[i];\n }\n \n double mean, median;\n \n mean = (double)sum\/n;\n \n sort(a,a+n);\n if(n%2==0) \n median = (double)(a[n\/2-1]+a[n\/2])\/2;\n else \n median = a[n\/2];\n \n int mode=a[0], mx=1, cnt=1;\n for(auto i=0;i<n-1;i++)\n {\n if(a[i]==a[i+1]) {\n ++cnt;\n }\n else \n cnt=1;\n if(cnt>mx) \n {\n mx=cnt;\n mode=a[i];\n }\n }\n \n cout<<mean<<\"\\n\"<<median<<\"\\n\"<<mode;\n \n return 0;\n}<\/pre>\n\n\n\n