c ++程序中的Mean,Median和Mode不正确

时间:2015-03-07 22:02:58

标签: c++

标题说。该程序基本上应该做如下;询问调查了学生调查的人数,输入学生观看的电影数量,然后找出平均值,平均值和模式。它确实使用指针和动态数组。代码如下,感谢帮助。对于输入,我使用1-10,这给我平均5.5,中位数6.5和-1模式。

编辑忘记提及这一点,但似乎我的逻辑在这里有缺陷。如果有人可以指出原因,那就太棒了......

编辑2 使用冒泡排序更新了代码,以帮助解决我遇到的一些问题。以下是更新后的代码:

#include <iostream>
#include <iomanip>
using namespace std;

double calculateMean(int *, int);
double calculateMedian(int *, int);
int calculateMode(int *, int);
void bubbleSort(int *, int);

int main()
{
    int *nums;
    int num_students;
    char repeat = ' ';

    do
    {


        cout << "Enter in how many students were surveyed: ";
        cin >> num_students;


        while (num_students < 0)
        {
            cout << "Invalid number of students!\n";
            cout << "Enter in how many students were surveyed: ";
            cin >> num_students;
        }


        nums = new int[num_students];


        for (int count = 0; count < num_students; count++)
        {
            cout << "Number of movies say by Person #" << (count + 1) << ": ";
            cin >> nums[count];

            while (nums[count] < 0)
            {
                cout << "Invalid number. Please enter in a positive number: ";
                cout << "\nNumber of movies say by Person #" << (count + 1) << ": ";
                cin >> nums[count];
            }

        }
        bubbleSort(nums, num_students);

        cout << fixed << showpoint << setprecision(1);

        cout << "\nThe mean is: ";
        cout << calculateMean(nums, num_students) << endl;

        cout << "\nThe median is: ";
        cout << calculateMedian(nums, num_students) << endl;

        cout << "\nThe mode is: ";
        cout << calculateMode(nums, num_students) << endl;

        delete[] nums;
        nums = 0;

        cout << "Do you want to go again? Y for Yes, N for No.";
        cin >> repeat;


    } while (repeat == 'Y' || repeat == 'y');
    cout << "Program ending.\n";

    return 0;
}

void bubbleSort(int *nums, int num_students)
{
    int temp;
    for (int i = 0; i < num_students; i++){
        for (int j = i + 1; j < num_students; j++){
            if (nums[i] > nums[j])
{
                temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
    }
}


double calculateMean(int *nums, int num_students)
{
    double total = 0;
    double average;

    for (int count = 0; count < num_students; count++)
    {
        total += nums[count];
    }
    average = total / num_students;
    return average;
}


double calculateMedian(int *nums, int num_students)
{
    double median = 0.0;
    cout << fixed << showpoint << setprecision(1);


    if (num_students % 2 == 0)
    {
        median = (nums[num_students / 2 - 1] + nums[(num_students / 2)]) / 2.0;
    }
    else
        median = nums[num_students / 2];

    return median;
}


int calculateMode(int *nums, int num_students)
{
    int mode = 0;
    int val = 0;
    int index;


    for (index = 0; index < num_students - 1; index++)  // note the - 1 here
    {
        if (nums[index] == nums[index + 1])
        {
            mode++;
            val = *(nums + index);
        }
    }
    if (val > 0)
        return val;
    else
        return -1;

}

2 个答案:

答案 0 :(得分:0)

calculateMode函数中的一个错误:

for (index = 0; index < num_students; index++)
{
    if (*(nums + index) == *(nums + (index + 1)))  
                                           ^^^ // this goes out of bounds

你的循环在最后一次迭代中访问最后一个学生之外的一个。做两件事会更简单:

1)使用数组语法和

2)循环1减去时间

  for (index = 0; index < num_students - 1; index++)  // note the - 1 here
    {
        if (nums[index] == nums[index + 1]) 

对于你的calculateMedian,错误是如果学生人数均匀,你将超出界限。

例如,假设num_students为2,因此您的数组只有两个元素:

median = (nums[num_students / 2] + nums[(num_students / 2) + 1]) / 2.0;

相同
median = (nums[1] + nums[2]) / 2.0;  //nums[2] is out of bounds

更正是这样做:

median = (nums[num_students / 2 - 1 ] + nums[(num_students / 2 )]) / 2.0;

答案 1 :(得分:0)

您可以看看我的程序。

#include<bits/stdc++.h>
using namespace std;

void fill_array(long int a[],long int size);
double mean(long int a[],double& sum,long int size);
double median(long int a[],long int size);
void bubble_sort(long int a[],long int size);
int mode(long int a[],long int size);

int main()
{
    long int size;
    cin >> size;
    long int a[size];
    double sum = 0.0,mn1,mn2,mn3;
    fill_array(a,size);
    mn1 = mean(a,sum,size);
    mn2 = median(a,size);
    mn3 = mode(a,size);
    cout << mn1 << endl;
    cout << mn2 << endl;
    cout << mn3;
    return 0;
}
void fill_array(long int a[],long int size)
{
    for(int i=0;i<size;i++)
    {
        cin >> a[i];
    }
}
double mean(long int a[],double& sum,long int size)
{
    for(int i=0;i<size;i++)
    {
        sum += a[i];
    }
    double mn;
    mn = sum/size;
    return mn;
}
double median(long int a[],long int size)
{
    bubble_sort(a,size);
    double md;
    if(size%2==0)
    {
        md = (a[size/2]+a[(size/2)-1])/2.0;
    }
    else
    {
        md = a[size/2];
    }
    return md;
}
void bubble_sort(long int a[],long int size)
{
    for(int i=size-1;i>0;i--)
    {
        for(int j=0;j<i;j++)
        {
            if(a[j] > a[j+1])
            {
                int temp = a[j+1];
                a[j+1] = a[j];
                a[j] = temp;
            }
        }
    }
}
int mode(long int a[],long int size)
{
    int mode=a[0], mx=1, count=1;
    for(int i=0;i<size-1;i++)
    {
        if(a[i]==a[i+1]) {
            ++count;
        }
        else
            count=1;
        if(count>mx)
        {
            mx=count;
            mode=a[i];
        }
    }
    return mode;
}