Q:

Correct the errors in each of the following code fragments

0

 Correct the errors in each of the following code fragments:

(a)

if (ival1 != ival2)
 ival1 = ival2
else ival1 = ival2 = 0;

(b)

 if (ival < minval)
 minval = ival; 
occurs = 1;

(c)

if (int ival = get_value())
 cout << "ival = " << ival << endl;
if (!ival)
 cout << "ival = 0\n";

(d)

if (ival = 0)
 ival = get_value();

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

(a)

    if (ival1 != ival2)
      ival1 = ival2;  // Missing semicolon
    else ival1 = ival2 = 0;

(b)

    if (ival < minval) {  // Need a block for more than one statements
      minval = ival;
      occurs = 1;
    }

(c)

    int ival;  // Used outside the first if scope, thus defined outside
    if (ival = get_value())
      cout << "ival = " << ival << endl;
    if (!ival)
      cout << "ival = 0\n";

(d)

 if (ival == 0)  // Should be equality operator instead of assignment
      ival = get_value();

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now