C ++:平均中位数,众 [英] C++: Mean Median and Mode

查看:160
本文介绍了C ++:平均中位数,众的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近创建了一个C ++程序找到值的数组的平均中位数和模式。我意识到这将是更好的一类内完成。然而,我的函数生成平均值不随地吐痰出合适的数量,虽然我pretty一定的逻辑是罚款。

另外,我是能够从东西我在网上找到创建生成模式的功能修改snipbit,或者至少1最大出现的数值就可以发现,我是能够实现。不过,我不是100%确定如何总结我的周围是什么函数内实际发生的头。

有一个更好的理解什么模式功能正在发生的事情,什么到底是怎么回事错在我的均值功能将大大AP preciated的。

这是我的code迄今:

 的#include<&iostream的GT;使用命名空间std;无效模式(INT [],int);在
平均无效(INT [],int);在
无效的排序(INT [],int);在
虚空中位数(INT [],int);在诠释的main()
{   int数组[15];
   总浮动,模式;
   INT N = 15; //数组中元素的个数    //填写阵列的价值
    的for(int i = 0; I< N;我++){
        COUT<< 中填入<< i + 1的&所述;&下; 号。
        CIN>>数组[我]
    }    排序(数组,N);
    返回0;
}
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////
平均无效(INT new_array [],INT NUM){
 //获取总计&安培;计算平均
    总浮动;
    的for(int i = 0; I<民;我++){
        总+ = new_array [I]
    }
    COUT<< 中庸是<<总/ NUM<< ENDL;
    模式(new_array,NUM);
    }
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////
虚空中位数(INT new_array [],INT NUM){
    //计算中位数(中号)
    如果(NUM%2!= 0){//是元素#奇怪?
        INT临时=((NUM + 1)/ 2)-1;
        COUT<< 中位数<< new_array [临时]<< ENDL;
    }
    其他{//那么它即使! :)
        COUT<< 中位数<< new_array [(NUM / 2)-1]下;&下; 和&所述;&下; new_array [NUM / 2]<< ENDL;
    }
    平均(new_array,NUM);
}
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////
无效模式(INT new_array [],INT NUM){
    为int *我prepetition = INT新[NUM]
    //在相同尺寸的存储器alocate新阵列(四围由可变限定元件的数量的方式)
    的for(int i = 0; I<民;我++){
        我prepetition [I] = 0; //初始化每个元素为0
        诠释J = 0; //
        而((J<我)及和放大器;!(new_array [I] = new_array [J])){
            如果(new_array [I]!= new_array [J]){
                J ++;
            }
        }
        (我prepetition [J])++;
    }
    INT iMaxRepeat = 0;
    的for(int i = 1; I<民;我++){
        如果(ⅰprepetition [I] I标记prepetition [iMaxRepeat]){
            iMaxRepeat = I;
        }
    }
    COUT<< 模式是&所述;&下; new_array [iMaxRepeat<< ENDL;}
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////无效的排序(INT new_array [],INT NUM){
     // ARRANGE VALUES
    对于(INT X = 0; X<民; X ++){
         对于(INT Y = 0; Y< NUM-1; Y ++){
             如果(new_array [Y]≥new_array [Y + 1]){
                 INT TEMP = new_array [Y + 1];
                 new_array [Y + 1] = new_array [Y];
                 new_array [Y] =温度;
             }
         }
     }
    COUT<< 清单;
    的for(int i = 0; I<民;我++){
        COUT<< new_array [1] - ;&下; ;
    }
    COUT<< \\ n;
    中位数(new_array,NUM);
}
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////
////////////////////////////////////////////////// /////////////////////////////////


解决方案

不要忘了初始化的变量:

 浮法总= 0.0;

在C ++中,具有自动存储持续时间的变量可以保留初始化。使用这样的变量会给你不确定的行为。

I've recently created a C++ program to find the mean median and mode of an array of values. I realize this would be much better to do within a class. However, my function to generate the mean is not spitting out the right number, although I'm pretty certain the logic is fine.

Also, I was able to modify a snipbit from something I found online to create a function that generates the mode, or at least the 1st most occurring values it can find, that I was able to implement. However, I am not 100% sure of how to wrap my head around what is actually happening within the function.

A better understanding of what is happening in the mode function and what the hell is going wrong in my mean function would be greatly appreciated.

This is my code so far:

#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);
}
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////

解决方案

Don't forget to initialise your variables:

float total = 0.0f;

In C++, a variable with automatic storage duration can be left uninitialized. Using such a variable will give you undefined behaviour.

这篇关于C ++:平均中位数,众的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆