lundi 30 janvier 2017

Checking numbers in an array

I want to write a function that returns true if and only if in an array t of size n i have every single number from 1,2, ... n

My idea was to:

bool g(int* t, int n){
    for(int i=0;i<n;i++){
        for(int j=n;j>0;j--){
        if(t[i]==n)
          break;
        else
          return false;
        }

    }
    return true;
}

However this for int t[6]={6,2,3,4,5,1}; returns zero which is wrong as all number from 1 to 6 are in an array.

How this could be improved? Furthermore how this question could be improved?

Aucun commentaire:

Enregistrer un commentaire