find the Median and mode of an array using functions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

find the Median and mode of an array using functions

I need help with this assignment. I have to write a program that creates a random numbers array with 100 elements that I find the median and mode for using functions. The code I have only prints 1s for both values. Any advice would be greatly appreciated. #include <iostream> #include <ctime> #include <cstdlib> #include <bits/stdc++.h> using namespace std; const unsigned int n = 100; double findMedian() { static int a[n]; sort(a, a+n); for(int i = 0; i<n; i++) { a[i] = (rand() % 53) + 1; } if (n % 2 != 0) return (double)a[n/2]; return (double)(a[(n-1)/2] + a[n/2])/2.0; } int findMode () { srand((unsigned)time(NULL)); static int a[n]; int count = 0, mode = 0; for(int i = 0; i<n; i++) { a[i] = (rand() % 53) + 1; for(int j = 0; j<n; j++) { a[j] = (rand() % 53) + 1; int tempMost = a[j]; int tempCount = 0; for(int t = 0; t<n; t++) { a[t] = (rand() % 53) + 1; if(a [t]==tempMost) { tempCount++; if(tempCount>count) { tempCount++; if(tempCount>count) { mode= tempMost; count = tempCount; } } } } } } return a[n]+count; } int main() { srand(time(NULL)); int a[n]; int n = sizeof(a)/sizeof(a[100]); int size = 100; for(int i = 0; i<n; ++i) { a[i] = (rand() % 53) + 1; //cout << findMedian<<endl; //ut << findMode<<endl; } cout << findMedian<<endl; cout << findMode<<endl; return 0; }

27th Nov 2018, 6:35 PM
dominic smith
dominic smith - avatar
1 Answer
0
wow, you're absolutely right, I've been working on this so long I forgot about that once I finally worked out the logic for the mode
28th Nov 2018, 3:38 AM
dominic smith
dominic smith - avatar