1. Write a function bool yes( ) which reads a string from the keyboard and retur
ID: 3564320 • Letter: 1
Question
1. Write a function bool yes( ) which reads a string from the keyboard and returns true if the string begins with 'Y' or 'y'; otherwise it returns false. Now write a main to play a simplified version of "Twenty Questions" which asks only three yes/no questions and calls yes( ) to get the user's answers and deduce which of eight items matches. A sample run should look like this:
Are you thinking of something that is alive? No
Are you thinking of a Sesame Street character? Yup
Are you thinking of something that is green? yessiree
You must be thinking of Kermit.
Choose your own three questions and something that matches each of the eight possible combinations.
float a, b, c, x1, x2, determinant, realPart, imaginaryPart;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
determinant = b*b - 4 * a*c;
if (determinant > 0) {
x1 = (-b + sqrt(determinant)) / (2 * a);
x2 = (-b - sqrt(determinant)) / (2 * a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (determinant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(determinant)) / (2 * a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b / (2 * a);
imaginaryPart = sqrt(-determinant) / (2 * a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
That's the code the needs to be modified for the following question.
2.
a=0, b=0, c=0.In this case say that x can be any number (real or complex).
a=0, b=0, c dosen't = zero, Say the equation is inconsistent
a=0.In this case only print the one root and the residual.
a dosen't = 0, b=0, c=0 In this case only print the one root and the residual.
If none of these special cases applies, proceed as in problem 1 which is coded above this question.
Explanation / Answer
bool yes(){
char arr[5];
int i;
for (i=1;i<5;i++){
cin>> arr[i];
}
if(arr[0] == 'y' || arr[0]=='Y'){
return true;
}else{
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.