我正在尝试解决一个问题,在该程序中,我将文件读入程序并输出一个文件,该文件包含平均值,最小值,最大值以及该数字在程序中出现的次数。但是,我无法弄清楚如何为重复的“计数”创建数组。

如果我尝试读取的文件具有值19 5 26 5 5 19 16 8 1

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

我首先对数组进行排序以读取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”,如下图所示:

最佳答案

在给您解决方案之前,请花一点时间记住,您使用的是C ++而不是C语言。因此,您应该使用向量,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/

10-13 21:54