vendredi 14 octobre 2022

Value of a variable is not updating inside the loop

In the following code, I am trying to count frequency of each non zero number

a[i] is actual array of index i, it is used to create array and store values. d[i] is used to create dupicate array with same values as a[i]. My intention of the code is to update freq after testing each case using nested loop but value of freq is not updating. freq value remains to be either 0 or 1. I tried to debug but still ending up with the same bug.

Some one please debug the code and fix it. Open for suggestions.

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int testcase,size;
    cin>>testcase;
    while(testcase--){
        cin>>size;
        int freq=0,total=0;
        int a[size];int d[size];
        for(int i=0;i<ns;i++){//to create array and duplicate arrray
            cin>>a[i];
            d[i]=a[i];
        }
        for(int i=0;i<ns;i++){
            if(d[i]==0 )continue;
            for(int j=0;j<ns;j++){ //duplicate array comparison
                cout<<"This is frequency"<<freq<<"This is total"<<total<<endl;

/*I used the above statement 
to check the behaviour of frequency value. Please ignore the above statement it if you want to.*/

                if(d[i]==d[j]){
                    freq=freq+1;
                    total=freq;
                    d[j]=0;
                }

            }
            d[i]=0;
            //cout<<"This is frequency"<<freq<<endl;
            freq=0;

//freq=0 is used to make value of the index position 0 if the number is checked once.


             cout<<"check frequency"<<freq<<endl;
        }


}
}

Aucun commentaire:

Enregistrer un commentaire