Search code examples
arrayscomparison

Check if all content of arrays are between a range of numbers, without duplicates


I need a way to see if all the numbers entered by the user are make up the numbers from 1 to 15. Wihout duplicates.

What I have so far is a program that will check if the numbers are over 15, or less then 1, but I don't have any idea of how to check every number entered into the array, and see if it's already been inputted into the array.

This is what I have so far.

#include <iostream>
using namespace std;
int main() {
    bool somethingBadHappend=false;         //booleans like this make it easy for the program to be read
    int numbers[15]{};
    cout << "enter 15 numbers:";
    for (int i = 0; i < 15; i++)
    {
        cin>>numbers[i];                    //entering them into the array
    }

 
        if (numbers[i] > 15|| numbers[i]<=0)
        {
            somethingBadHappend = true;
        }
    }

    if (somethingBadHappend)      //see the perfect use of code here?
    {
        cout<<"NOT GOOD";           // How elegant!
    }
    else
        cout<<"GOOD";

    return 0;
}

Solution

  • If i understood your question, you have to do another check into array to see the duplicate.

    #include <iostream>
    using namespace std;
    int main() {
        bool somethingBadHappend=false;         //booleans like this make it easy for the program to be read
        int numbers[15]{};
        
        cout << "enter 15 numbers:";
        for (int i = 0; i < 15; i++)
        {
            cin>>numbers[i];                    //entering them into the array
        }
    
    //to find values >15 or <1
        for (int i = 0; i < 15; i++)
        {
            if (numbers[i] > 15 || numbers[i]<=0)
            {
                somethingBadHappend = true;
            }
        }
    //to find the duplicate into array
            for (int i = 0; i < 15; i++){
                    for(int c=0; c<15; c++){
                        if(i!=c){ // check the different index
                            if(numbers[i]==numbers[c]){
                                somethingBadHappend = true; //found duplicate
                                break;
                            }
                        }
                    }
                }
    
        if (somethingBadHappend)      //see the perfect use of code here?
        {
            cout<<"NOT GOOD";           // How elegant!
        }
        else
            cout<<"GOOD";
    
        return 0;
    }