C++:平均中位数和众数
C++: Mean Median and Mode
我最近创建了一个C++程序来查找值数组的平均中位数和众数。我意识到在课堂上这样做会好得多。但是,我生成平均值的函数并没有吐出正确的数字,尽管我很确定逻辑很好。
此外,我能够修改我在网上找到的东西中的一个截图,以创建一个函数来生成我能够实现的模式,或者至少是它能找到的第一个最常出现的值。但是,我不是 100% 确定如何了解函数中实际发生的事情。
更好地了解模式函数中发生了什么以及我的平均函数中到底出了什么问题,将不胜感激。
这是我到目前为止的代码:
#include <iostream>
using namespace std;
void mode(int[], int);
void mean(int[], int);
void sort(int[], int);
void median(int[], int);
int main()
{
int array[15];
float total, mode;
int n = 15;//number of elements in array
//fill in the value of array
for(int i=0; i<n; i++){
cout << "fill in the "<< i+1 << " number. :";
cin >> array[i];
}
sort(array, n);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void mean(int new_array[], int num){
//GET TOTAL & CALCULATE MEAN
float total;
for(int i=0;i<num; i++){
total += new_array[i];
}
cout << "The mean is " << total/num << endl;
mode(new_array, num);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void median(int new_array[], int num){
//CALCULATE THE MEDIAN (middle number)
if(num % 2 != 0){// is the # of elements odd?
int temp = ((num+1)/2)-1;
cout << "The median is " << new_array[temp] << endl;
}
else{// then it's even! :)
cout << "The median is "<< new_array[(num/2)-1] << " and " << new_array[num/2] << endl;
}
mean(new_array, num);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void mode(int new_array[], int num) {
int* ipRepetition = new int[num];
// alocate a new array in memory of the same size (round about way of defining number of elements by a variable)
for (int i = 0; i < num; i++) {
ipRepetition[i] = 0;//initialize each element to 0
int j = 0;//
while ((j < i) && (new_array[i] != new_array[j])) {
if (new_array[i] != new_array[j]) {
j++;
}
}
(ipRepetition[j])++;
}
int iMaxRepeat = 0;
for (int i = 1; i < num; i++) {
if (ipRepetition[i] > ipRepetition[iMaxRepeat]) {
iMaxRepeat = i;
}
}
cout<< "The mode is " << new_array[iMaxRepeat] << endl;
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
void sort(int new_array[], int num){
//ARRANGE VALUES
for(int x=0; x<num; x++){
for(int y=0; y<num-1; y++){
if(new_array[y]>new_array[y+1]){
int temp = new_array[y+1];
new_array[y+1] = new_array[y];
new_array[y] = temp;
}
}
}
cout << "List: ";
for(int i =0; i<num; i++){
cout << new_array[i] << " ";
}
cout << "n";
median(new_array, num);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
不要忘记初始化变量:
float total = 0.0f;
在C++中,具有自动存储持续时间的变量可以保持未初始化状态。使用这样的变量会给你未定义的行为。
首先,你还没有初始化一些变量。例如,在mean()
中,您应该拥有以下内容:
float total = 0;
默认情况下,变量不会初始化为任何定义的值。
我建议您提高编译器上的警告级别。如果您使用的是 g++,请使用 -Wall
。这将检测诸如使用未初始化的变量和未使用的变量(您在main()
中具有)等问题。
模式是基本的统计运算符之一;它表示数组中频率最高的元素。
你的函数mode
是这个运算符的实现:它创建一个新数组,在其中存储数组中每个元素的频率(即每个元素出现的次数)。该函数返回具有最高频率的元素,或者,如果有更多元素具有相同的最高频率,则返回较大的元素。
希望对你有帮助
相关文章:
- 在 1.5 秒内找到 3 到 4 个不同整数的中位数超过 2000 万
- 十进制的中位数是多少?
- 如何使用第一个、中间和最后一个元素的中位数正确分区?
- 使用 boost::累加器::统计来查找数组的中位数
- 从向量中提取最小值、最大值和中位数的最有效方法是什么
- 如果方法是常量,如何找到向量的中位数?
- 在C++中实现两个大小相等的排序数组的中位数
- 查找一组循环数据的中位数
- 为什么我的中位数快速选择算法segfault
- 是否可以实施中位数的中位数intreselect而没有掉期或堆的分配
- 两个分类阵列的中位数不同
- C++平均值和中位数绝对偏差返回相同的奇怪符号
- 通过 uva 的中位数程序
- 找到二叉搜索树的中位数,C++
- 双向链表的中位数
- 通过 C 加加号中的函数查找中位数
- C++:平均中位数和众数
- 返回带有 void 函数的排序数组以查找中位数
- 使用计数选择 (C++) 运行中位数算法
- 运行常量大小数组的中位数