0

I have entered 4 numbers into an array using

for (int i=0;i<4;i++)
{
    cin >> choice[i];
}

I need to check whether i have entered numbers 1,3,4,6(in any order) into the array

eg:-

if choice[0] == 1 && choice[1] == 3 && choice[2] == 4 && choice[3] == 6

else if ........ 1,3,6,4

else if..........1,6,3,4

else if.......1,6,4,3

else if......1,4,6,3

else if......1,4,3,6

....
....

else if.....6,4,3,1

this type of checking makes my code too big.

Please help me with an alternative way

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198

3 Answers3

5

Use std::is_permutation:

int choice[4];

for (int i=0;i<4;i++)
{
    cin >> choice[i];
}

std::cout << std::is_permutation(std::begin(choice), std::end(choice), std::vector<int>{1, 3, 4, 6}.begin()) << std::endl;
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • Isn't it an issue here with the lifetime of the temporary `std::vector` used as 3rd argument? – Felix Glas Dec 09 '14 at 12:26
  • @Snps Nope, in the same way that the `i` in `for(int i = 0; i < size; ++i)` will last the duration of the `for` loop because it is in scope, so the anonymous `std::vector` will last the duration of `std::is_permutation` because it will be in scope. – Jonathan Mee Dec 09 '14 at 12:30
  • But the only thing passed to the function scope of `std::is_permutation` is a copy of the iterator. The temporary is created at the call site, right? – Felix Glas Dec 09 '14 at 12:33
  • @Snps The `std::vector` was created in the scope of the `std::is_permutation` call and cannot be freed until that scope closes, which it does when `std::is_permutation` returns. – Jonathan Mee Dec 09 '14 at 12:35
  • [Seems you're right](http://ideone.com/oEK2IE), guess I have to read up some on evaluation order during function calls! :) – Felix Glas Dec 09 '14 at 12:42
  • @Snps I tried to find an answer on StackOverflow that explains this, but I couldn't. I think it would make a great question. If you write it up please post the link here, otherwise I'll go ahead and write the question up. – Jonathan Mee Dec 09 '14 at 12:46
3
bool count1=false;
bool count2=false;
bool count3=false;
bool count4=false;

for (int i=0;i<4;i++)
{
cin >> choice[i];
if(choice[i]==1) count1==true;
else if(choice[i]==3) count2=true;
else if(choice[i]==4) count3=true;
else if(choice[i]==6)  count4=true;
}

if(count1 && count2 && count3 && count4) cout<<endl<<"yes, it is!";
Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78
0

You may use something like:

std::vector<int> choice(4);
for (int i = 0; i < 4; ++i)
{
    std::cin >> choice[i];
}

std::sort(std::begin(choice), std::end(choice));
if (choice == std::vector<int>{1, 3, 4, 6}) { // 1, 3, 4, 6 should be sorted.
    // equal
}

Live example.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • Sorry i'm an absolute beginner in using vectors.i ran your code.It seems i have many errors in my code. #include #include #include #include #include #include using namespace std; int main() { std::vector choice(4); for (int i = 0; i < 4; ++i) { std::cin >> choice[i]; } std::sort(std::begin(choice), std::end(choice)); if (choice == std::vector{1, 3, 4, 6}) { // 1, 3, 4, 6 should be sorted. // equal } } – Vishnu Rajendran Dec 10 '14 at 05:52
  • Here's the output... error C2039: 'sort' : is not a member of 'std' error C3861: 'sort': identifier not found error C2143: syntax error : missing ')' before '{' error C2275: 'std::vector<_Ty>' : illegal use of this type as an expression 1> with 1> [ 1> _Ty=int 1> ] error C2143: syntax error : missing ';' before '}' error C2059: syntax error : ')' – Vishnu Rajendran Dec 10 '14 at 05:58
  • [Live example](https://ideone.com/V6q8PV) works. You have to include `` and compile with C++11 (`-std=c++11` for gcc/clang). – Jarod42 Dec 10 '14 at 07:57