Selasa, 28 Februari 2012

Find Errors


1. What is wrong with this code:

cout<<”Enter n:”;

cin>>n;

if (n<0)


     cout<<”That is negative. Try again”<<endl;

cin>>n;

else

cout<<”ok n=”<<n<<endl;

Answer:
There is more than one statement between the if clause and the else clause. They need to be made into a compound statement by enclosing them in braces { }.

cout<<”Enter n:”;

cin>>n;

if (n<0)

             
{
 cout<<”That is negative. Try again”<<endl;

      cin>>n;
  }else

cout<<”ok n=”<<n<<endl;
2.      What is wrong with this code:
if(x = 0) cout<<x<<”=0\n”;
else cout<<x<<”!=0\n”;

Answer:
if (x = = 0)cout<<x<<"=0\n";
3.       What is wrong with this code:
if (x < y < z) cout<<x<<”<”<<y<<”<”<<z<<endl; 

Answer:
if (x < y && y <z) cout<<x<<”<”<<y<<”<”<<z<<endl;

Rabu, 22 Februari 2012

Logical Expression

1. Construct a logical expression to represent each of the following conditions:
a. n is between 0 and 7 but not equal to 3;
b. n is between 0 and 7 but not even;
c. n is divisible by 3 but not by 30;

2. Construct a logical expression to represent each of the following conditions:
a. score is greater than or equal to 80 but less than 90;
b. answer is either ‘N’ or ‘n’;
c. n is even but not 8;


Solve the answer, and please email the answer to me at aminahpolimas@yahoo.com.
Or you may post your answer in comments.
Good Luck!!