0

This program is to find Frequency of different elements in array using unordered map. But I am unable to debug it, as this error is occuring for the first time in my code. Please help me debug this code.


#include<iostream>
#include<unordered_map>
using namespace std;

void frequency(int arr[] , int n)
{
    unordered_map<int,int> m;
    for(int x : arr)     /* This line is showing error(for statement require a suitable begin function and none was found .)*/
        m[x]++;
    for( auto x : m)
        cout<<x.first<<" : "<<x.second;
}

int main()
{
    int n = 8;
    int arr[n] = {10,12,10,15,10,20,12,12};
    frequency(arr,n);
}
ChrisMM
  • 8,448
  • 13
  • 29
  • 48
Rishabh
  • 11
  • 4
  • 2
    Variable-length arrays on the stack are non-standard. Use a `std::array&` as the first argument type. `int arr[]` is basically “just a pointer”; a range-based `for` has no way to magically determine its size or get an iterator `.begin()` from it. Use a `std::vector` if the size needs to be dynamic (and different on each call). – Andrej Podzimek Oct 25 '22 at 11:14
  • In `frequency`, there's no way that the compiler can determine the size of `arr`. You can use a templated function, where one of the template arguments in the size, use a vector, or use `std::array`, and all would work for the kind of loop you are trying to use. Alternatively, just use a regular old `for` loop. – ChrisMM Oct 25 '22 at 11:21
  • 2
    `arr` is a pointer not an array. See dupe: [Passing array to function in C++](https://stackoverflow.com/questions/14309136/passing-arrays-to-function-in-c). – Jason Oct 25 '22 at 11:27

0 Answers0