jeudi 3 juin 2021

Frequency array of c++

#include <iostream>

using namespace std;
int main()
{
    int arr,i=0,frequancy,y,X,l;
    
    int V[arr];
    cout<<"Enter the size of an array: ";
    cin>>arr;
    
    cout<<"Enter all "<<arr<<" elements in array: ";
    for(i=0;i<=arr-1;i++)
    cin>>V[i];
    
    int a[arr]={0};
    
    cout<<"Frequency of all "<<arr<<" elements in array:"<<endl;
        
    for(i=0;i<arr;i++)
    {
        int count=0;
        if (a[i]!=1)
        {
            for (int j=0;j<arr;j++)
            {
                if(V[i]==V[j])
                {
                    count++;
                    a[j]=1;
                }
            }
            if (count>1||count==1)
            {
                cout<<V[i]<<" occurs "<<count<<" times "<<endl;
            }
        }
    }
return 0;
}

//this is the answer
Enter the size of an array: 9
Enter all 9 elements in array: 1
1
2
3
1
5
3
12
1
Frequency of all 9 elements in array:
1 occurs 4 times
2 occurs 1 times
3 occurs 2 times
5 occurs 1 times
12 occurs 1 times

So there are a few questions that I don't understand that well and I need someone to explain what is going on with it, please.

  1. I don't understand why we have to initialize this(int a[arr]={0}) to 0, I asked my prof about it be he said it has to be like that. which doesn't answer my question:'C.

  2. I have a problem with the bottom part of the code, I kinda understand a bit but if someone can help me that would help a lot Thank you!!!!

Aucun commentaire:

Enregistrer un commentaire