c++ - 如何计算排序数组中的重复数字

标签 c++ arrays for-loop

我试图解决一个问题,我将一个文件读入程序并输出一个文件,其中包含平均值、最小值、最大值以及该数字在程序中出现的次数。但是,我不知道如何为重复的“计数”数创建一个数组。

如果我尝试读入的文件的值为 19 5 26 5 5 19 16 8 1

我需要输出的文件读取5---3次; 8---1次; 16---1次; 19--2次; 26--1次

我首先将我的数组排序为 5 5 5 8 16 19 19 26

下面是我的代码,解释了我正在尝试做的事情:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

double averageArray(int a[], int length); // user defined function to get average
int maxval(int a[], int length); //user defined function to get max val
int minval(int a[], int length); //user defined function to get min val
void bsort(int arr[], int length);// udf to sort array from min to max
int countArray(int a[], int length); //attempt to create a function to get the number of occurrences of a number that is duplicated

int main()
{
    char infilename[16];
    int nums[50];//newly created array to read in the numbers from the file
    int length(0);//array has a length defined by "length"
    ifstream fin;
    ofstream fout;


    cout << "Please enter an input file name: ";
    cin >> infilename;
    cout << endl;

    fin.open(infilename); 
    if (fin.fail())
    {
        cerr << "The file " << infilename << " can't be open!"<<endl;
        return 1;
    }


    cout<<"The output to the file statistics.txt should be as follows: "<<endl;
    fout.open("statistics.txt");
    fout<<"N"<<"\t"<<"Count"<<endl;
    cout<<"N"<<"\t"<<"Count"<<endl;


    while (fin >> nums[length]) 
        length++;

    bsort(nums, length);
    for (int i=0; i<length; i++) {
        if (nums[i]==nums[i-1]) {
            continue;
        }
        cout<<nums[i]<<"\t"<<countArray(nums,length)<<endl;
        fout<<nums[i]<<"\t"<<endl;
    }

    cout << "\nAverage: " << averageArray(nums,length) << endl;
    cout << "Max: "<< maxval(nums,length)<<endl;
    cout << "Min: "<< minval(nums,length)<<endl;


    fin.close();


    return 0;
}



double averageArray (int a[], int length)
{
    double result(0);

    for (int i = 0; i < length ; i++)
        result += a[i];
    return result/length;
}

int maxval(int a[], int length) 
{

    int max(0);

    for (int i=1; i<length; i++)
    {
        if (a[i]>max)
            max=a[i];
    }
    return max;
}

int minval(int a[], int length) 
{

    int min(100);

    for (int i=1; i<length; i++)
    {
        if (a[i]<min)
            min=a[i];
    }
    return min;
}

void bsort(int a[], int length)
{
    for (int i=length-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 countArray(int a[], int length)
{
    int counter(0);
    for (int i=0; i<length; i++){
        if (a[i]==a[i+1]) //loop through array and if the number after is the same as the previous number, then count one
        counter++;
    }
    return counter;
}

虽然它编译了,但计数只显示“3”,如下图所示:

output image .

最佳答案

在我给出解决方案之前,请花点时间记住,您是在用 C++ 编程,而不是 C。因此,您应该使用 vector 、istream 迭代器和 std::sort。您还应该使用 std::map,它可以轻松实现此目的:

template <typename It>
std::map<int, int> count_occurrences(It it, It end)
{
  std::map<int, int> output;
  while (it != end) output[*it++]++;
  return output;
}

如何将它与您现有的代码结合起来作为练习留给读者。我建议您应该阅读有关迭代器的内容。

关于c++ - 如何计算排序数组中的重复数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36142551/

相关文章:

javascript - mongodb 用变量更新数组

javascript - 理解 Javascript 中的 for 循环

C++ Clean String to int 转换

c++ - 如果表达式的求值需要对引用求值,为什么表达式不是 "core constant expression"?

php - 如何从 PHP 中的括号之间获取所有内容?

python - 在 Python 中仅使用 While 循环打印星号箭头

PHP foreach循环遍历多维数组

c++ - Boost.Asio IPv6 为什么绑定(bind)错误?

c++ - 在 gdb 中打印有关文件流对象的详细信息 - C++

javascript - 将 8 添加到我的随机生成的数组中