mardi 27 octobre 2020

How do I get this array frequency program to accept user input?

I'm not even entirely sure I am coding this problem correctly, arrays confuse me. I got it down to where no errors were being reported but for some reason, I can't think of a way to make the program search for user input to find out how many times the inputted item (ie. 14) occurs.

CODE:

#include <iterator>
#include <algorithm>
using namespace std;

int countOccur(int values[], int n, int m) 
{ 
    int count = 0; 
    for (int i=0; i < count; i++) 
       if (values[i] == m, n)  
          count++; 
    return count; 
} 

int main() {
    // DO NOT MODIFY THIS CODE!!!
    int m, n;
    cin >> m;
    cin >> n;
    int values[m][n];

    for (int i = 0; i < m; i++) {
        copy_n(istream_iterator<int>(cin), n, values[i]);
            // END DO NOT MODIFY
    // TODO Read in value to find
    // TODO Find the number of occurrences of that value within the 2dArray `values'
    /* 2dArray `values' has dimensions `m' and `n'
     * Ex.
     * m = 2
     * n = 3
     * values = {
     *     {15, 20, 18},
     *     {20, 32, 54}
     * }
     *
     * If the value `20' were entered
     * Your program should produce the output 2
     */
        cout << fixed << "Occurences: " << countOccur(values[i], m,n) << endl;
    }
return 0;
}
     

Aucun commentaire:

Enregistrer un commentaire