Implement a little guessing game called (for some obscure reason and Cows.\" The
ID: 3784632 • Letter: I
Question
Implement a little guessing game called (for some obscure reason and Cows." The program has a vector of four different integers in the range 0 to 9 (e.g., 1234 but not 1122) and it is the user's task to discover those numbers by repeated guesses. Say the number to be guessed is 1234 and the user guesses 1359; the response should be "1 bull and 1 cow" because the user got one digit (i right and in the right position (a bull) and one digit (3) right but in the wrong position (a cow). The guessing continues until the user gets four bulls, that is, has the four digits correct and in the correct order.Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int main()
{
srand(time(0));
vector <int> v,v1;
int r;//for storing random value
for(int i=0;;i++)
{
r=rand()%10;
int c=0;
if(i>1)
{
for(int j=0;j<v.size();j++)
{
if(v[j]==r)
{
c++; break;
}
}
}
if(c==0)
v.push_back(r);
if(v.size()==4)
break;
}
cout<<v[0]<<v[1]<<v[2]<<v[3]; exit;
int num;
for(int i=0;i<4;i++)
{
cin>>num;
v1.push_back(num);
}
int c=0,b=0;
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
if(i==j)
{
if(v1[i]==v[j])
{
b=b+1;
}
}else if(v1[i]==v[j])
c++;
}
if(c==0 && b==0)
cout<<"No bull No cow guesing wrong input "<<endl;
if(b)
cout<<"Number of bull "<<b<<endl;
if(c)
{
cout<<"Number of cow "<<c<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.