I want to write a function that displays the frequency of occurrence of an element entered by the user in a stored array. The array always has the following elements: 4, 2, 7, 10, 9, 7, 6, 10, -8, 7, 9.
well i wrote this one but i have a problem with it ..

#include <iostream>
using namespace std;
void freq(const int []);
int main()
{
  int var=0;
  int array[]={4, 2, 7, 10, 9, 7, 6, 10, -8, 7, 9};
  cout<<"your numbers."<<endl;
  for(int i=0;i<11;i++)
    cout<<array[i]<<" , ";
  cout<<endl;

  cout<<"enter a number : "
  for(int x=0;x<11;x++)
    cout<<freq(array)<<endl;
  return 0;
}

void freq(const int array[])
{
    int count=0;
    int variable=0;
    for(int k=0;k<11;k++){
        for(int j=0;j<11;j++){
           if(j!=k){
              if(array[k]==array[j]){
                 variable=array[k];
                 count++;
              }
           }
        }
    }
   cout<<"the number "<<variable<<" occurred "<<count<<" times in your array."<<endl;
   cout<<"press any key to continue.";
}

What is your fnuction freq meant to do? I note that you ask the user to enter a value, but you don't actually take any input from them.

i have array that always has the previous elements .. i want from this function to count how much every element occurred freq for frequency .. and in the main function user will enter any element from these then it will reply with the number and its occurrence

Here's a way.

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

int main()
{
int user_number;
  int var=0;
  int array[]={4, 2, 7, 10, 9, 7, 6, 10, -8, 7, 9};
  cout<<"your numbers."<<endl;
  for(int i=0;i<11;i++)
    cout<<array[i]<<" , ";
  cout<<endl;


  std::map<int,int> valueFrequencyMap;

  for (int i=0;i<11;i++)
  {
    if (valueFrequencyMap.find(array[i]) != valueFrequencyMap.end())
    {
      valueFrequencyMap[array[i]] = valueFrequencyMap[array[i]]+1;
    }
    else
    {
      valueFrequencyMap[array[i]] = 1;
    }
  }

  for (auto it = valueFrequencyMap.begin(); it != valueFrequencyMap.end(); ++it)
  {
    cout << it->first << " : " << it->second << endl;
  }

  cout<<"enter a number : ";
  cin >> user_number;

  if (valueFrequencyMap.find(user_number) !=  valueFrequencyMap.end())
  {
    cout << "Value " << user_number << " has frequency " << valueFrequencyMap[user_number] << endl;
  }
  else
  {
      cout << "Value " << user_number << " has frequency " << 0 << endl;
  }
  return 0;
}

Here are some things I noticed:

First off, you say you have a problem but you don't itemize what the problem is. That's like taking your car to a mechanic and saying,"Fix it it's broken.".

In line 15, you're trying to get the output from a function that returns void.

Your freq function doesn't accept the input you're supposed to get from the user. Instead it uses an internal variable, which will only get set to the last element of the array.

When you're accepting array's into a function, you should either upgrade to a c++11 style array(it has a size function) or also accept the size of the array as well. Using magic numbers(11) restricts the functionality of the function. It won't work right with an array of a different size.

Instead of telling the user what the frequency is from the function, it is a better division of responsibility to return the frequency and let main report it to the user.

Put all together it could look something like this:

int freq(const int intputArray[], size_t size, const int targetNum)
{
    int count = 0;

    for (int k = 0; k < size; k++) {
        if (intputArray[k] == targetNum)
        {
            count++;
        }
    }
    return count;
}
const int ARRAY_SIZE = 11;
int main()
{
    int userInput = 0;
    int data[] = { 4, 2, 7, 10, 9, 7, 6, 10, -8, 7, 9 };
    cout << "your numbers." << endl;
    for (int i = 0; i < ARRAY_SIZE; i++)
    {
        cout << data[i] << " , ";
    }
    cout << '\n';
    cout << "enter a number : ";
    cin >> userInput;
    cout << "Your number("<< userInput << ") appeared "<< freq(data, ARRAY_SIZE,userInput) << " times\n";
    return 0;
}

use switches
e.g

int freq1,freq2.....freqN;

switch (num)
{
case 1:
{
freq1++;
break;
}
case 2:
.
.
.
}

thanks all for helping :)

Please remember to mark the post solved if your question is answered Thank You

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.