Ich versuche, ein Problem zu beenden, wo ich eine Datei in das Programm lese und eine Datei mit dem Durchschnitt, Min, Max und der Anzahl, wie oft diese Zahl aufgetreten ist das Programm. Ich kann jedoch nicht herausfinden, wie man ein Array für eine doppelte Anzahl von "counts" erstellt.Wie man doppelte Zahlen in einem sortierten Array zählt
Wenn die Datei, die ich versuche zu lesen, hat in die Werte 19 5 26 5 5 19 16 8 1
,
Ich brauche die ausgegebene Datei 5---3 times; 8---1 time; 16---1 time; 19--2 times; 26--1 times
zu lesen.
Ich sortierte zuerst mein Array zu lesen 5 5 5 8 16 19 19 26
.
Unten ist mein Code mit Erklärungen, was ich tun wollte:
#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;
}
Obwohl es kompiliert, nur die Zählung zeigt „3“ s, wie unten im Bild gezeigt:
.
Ich denke, dass Sie es online machen können, indem Sie [std :: count()] (http://en.cppreference.com/w/cpp/algorithm/count), wählen Sie ein Element aus, geben Sie das Ergebnis aus und speichern Sie es, und fahren Sie bis zum Ende des Arrays fort. – Incomputable
BTW Sie sind aus Bindungen – Incomputable
Sie wollen wahrscheinlich 'std :: map' oder' std :: unordered_map' verwenden (von denen jede diese Aufgabe ziemlich trivial macht), Die offensichtliche Alternative wäre, die Eingänge in einem Array zu speichern , dann sortiere es und gehe dann durch Finden von Mismatches. –