I am having trouble getting the right value for mode and variance.
Everything else works fine but when i run the code i get incorrect answers or values for the mode and variance part of the program. Cant seem to understand where i'm going wrong.
Here is my source code:
#include <iostream>
using namespace std;
void input(int ulist[6], int & n);
void Bubblesort(int ulist[6], int slist[6], int dlist[6], int n);
void print(int list[6], int n);
double median(int list[5], int n);
void mode(int list[6], int n);
void frequency(int list[6], int n);
void variance(int list[6], int n);
double average;
int n = 5;
int sum, number;
int main() {
int ulist[6], slist[6], dlist[6], max, min, range;
input(ulist, n);
cout << "Unsorted";
print(ulist, n);
cout << "Sorted";
Bubblesort(ulist, slist, dlist, n);
print(slist, n);
cout << "Descending";
print(dlist, n);
max = slist[5];
min = slist[1];
range = max - min;
cout << "Maximum element in the array is:" << max << endl;
cout << "Minimum element in the array is:" << min << endl;
cout << "Range is :" << range << endl;
cout << "Median is:" << median(slist, n) << endl;
mode(dlist, n);
frequency(slist, n);
variance(slist, n);
cin >> n;
}
void input(int ulist[6], int & n) {
int i(0), value;
cout << "enter value : \n";
cin >> value;
while (i < 5 && value != -1) {
i++;
ulist[i] = value;
if (i < 5) {
cin >> value;
}
}
n = i;
}
void Bubblesort(int unlist[6], int sortlist[6], int dsortlist[6], int n) {
int i, j, temp;
for (i = 1; i <= n; i++)
sortlist[i] = unlist[i];
for (j = 1; j <= n - 1; j++)
for (i = 1; i <= n - j; i++)
if (sortlist[i] > sortlist[i + 1]) {
temp = sortlist[i];
sortlist[i] = sortlist[i + 1];
sortlist[i + 1] = temp;
}
for (i = 1; i <= n; i++)
dsortlist[i] = unlist[i];
for (j = 1; j <= n - 1; j++)
for (i = 1; i <= n - j; i++)
if (dsortlist[i] < dsortlist[i + 1]) {
temp = dsortlist[i];
dsortlist[i] = dsortlist[i + 1];
dsortlist[i + 1] = temp;
}
}
void print(int list[6], int n) {
int i;
sum = 0;
cout << " list of numbers are : \n";
for (i = 1; i <= n; ++i) {
cout << list[i] << '\n';
sum = sum + list[i];
}
}
double median(int list[5], int n) {
if (n % 2 != 0) {
return (double)(list[(n + 1) / 2]);
} else {
return double(list[n / 2]);
}
}
void mode(int list[], int n) {
int number = list[0];
int x = number;
int count = 1;
int countMode = 1;
for (int i = 1; i < n; i++) {
if (list[i] == number) { // count occurrences of the current number
++count;
} else { // now this is a different number
if (count > countMode) {
countMode = count; // mode is the biggest ocurrences
x = number;
}
count = 1; // reset count for the new number
number = list[i];
}
}
cout << "mode is: " << x << endl;
}
void frequency(int list[6], int n) {
int i, j, o, t;
t = 0; //times appeared.
o = 1; //occurance
cout << "Frequency:" << endl;
for (i = 0; i < n - 1; i++) {
if (list[i] == list[i + 1]) {
o++;
} else {
if (o > 1) {
cout << list[i] << " occurs " << o << " times." << endl;
}
if (o > t)
t = o;
o = 1;
}
}
if (t <= 1) {
cout << "Numbers dont appear more than once." << endl;
}
}
void variance(int list[], int n) {
int i = 0, j = 0, sum = 0;
double k = 0,
var = 0;
for (i = 0; i < n; i++) {
sum = sum + list[i];
}
average = sum / n;
for (j = 0; j < n; j++) {
k = k + pow((list[j] - average), 2);
}
var = k / n;
cout << "variance is: " <<
var;
}
Output:
enter value :
15
15
467
852
1482
Unsorted list of numbers are :
15
15
467
852
1482
Sorted list of numbers are :
15
15
467
852
1482
Descending list of numbers are :
1482
852
467
15
15
Maximum element in the array is:1482
Minimum element in the array is:15
Range is :1467
Median is:467
mode is: 0
Frequency:
15 occurs 2 times.
variance is: 116097