2016-03-21 6 views
0

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:

output image.

+0

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

+0

BTW Sie sind aus Bindungen – Incomputable

+0

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. –

Antwort

0

Ihre Funktion int countArray(int a[], int length) hat keine Eingabe für die tatsächliche Nummer. Es zählt immer, wie oft die gleichen Zahlen in Ihrem Array hintereinander liegen. Das passiert zweimal für Fünfer und einmal für 19 => 3 mal.

Lösung:

int countArray(int a[], int length, int num) 
{ 
    int counter(0); 
    for (int i=0; i<length; i++){ 
     if (a[i]==num) //loop through array and if the number is the one you are looking for 
      counter++; 
    } 
    return counter; 
} 

und rufen Sie Funktion: countArray(nums, length, nums[i]);

1

Bevor ich Ihnen die Lösung geben, bitte einen Moment Zeit nehmen, sich daran zu erinnern, Sie sind in C-Programmierung ++, nicht C. Als solches Sie sollte Vektoren, Istream-Iteratoren und std::sort verwenden. Sie sollten auch std::map, verwenden, die zu diesem Zweck leicht erreicht:

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; 
} 

Wie kombinieren Sie dies mit Ihrem vorhandenen Code wird für den Leser als Übung. Ich schlage vor, Sie sollten über Iteratoren lesen.

0
void countArray(int a[], int length) 
{ 
    int counter(1); 
    bool flag(false); 
    //you take (i+1) index, it can go out of range 
    for (int i = 0; i < length - 1; 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 
      flag = true; 
      counter++; 
     } 
     else { 
      if (flag){ 
       cout<<a[i] << counter << endl; 
      } 
      flag = false; 
      counter = 1; 
     } 
    } 
} 

Ich habe nicht auf C für eine lange Zeit, aber ich hoffe, es wird Ihnen helfen. Dieses Verfahren wird Ihnen die Antwort ausdrucken, und Sie müssen es nur einmal anrufen.

0

Ich schlage vor, std::map zu verwenden, die die beste Lösung ist, um Ihr Problem zu lösen. Ich werde versuchen, einfach die differents Schritte zu erklären, dies zu tun:

  1. Ich halte Ihre Variablen initialisiert, zum Beispiel:

    int length = 9; 
    int nums[length] = {19, 5, 26, 5, 5, 19, 16, 8, 1}; 
    
  2. Erstellen Sie die std::map<int,int>, wo die key ( erste int) wird Ihre Nummer sein und die value ( Sekunde int) die Anzahl des Auftretens dieser Nummer im Schlüssel speichern.

    std::map<int,int> listNumber; 
    
  3. Füllen Sie Ihre Karte

    // For all numbers read in your file 
    for(int i=0; i<length; ++i) 
    { 
        // Get number value 
        int n = nums[i]; 
    
        // Find number in map 
        std::map<int, int>::iterator it = listNumber.find(n); 
    
        // Doesn't exists in map, add it with number of occurence set to 1... 
        if(it == listNumber.end()) 
        { 
         listNumber.insert(std::pair<int,int>(n,1)); 
        } 
        // ... otherwise add one to the number of occurence of this number 
        else 
        { 
         it->second = it->second+1; 
        } 
    } 
    
  4. Lesen Sie die Karte

    // Read all numbers and display the number of occurence 
    std::cout << "N" << "\t" << "Count" << std::endl; 
    for(std::map<int, int>::iterator it = listNumber.begin(); it!=listNumber.end(); ++it) 
    { 
        std::cout << it->first << "\t" << it->second << std::endl; 
    } 
    
Verwandte Themen