Chapter 3: Debugging Exercise

3.1

What will happen when you execute the following code?

#include <iostream>
using namespace std;
void main()
{
     int i=0;
     i=400*400/400;
     cout<<i;
}
Answer

i = 400*400/400; Here, 400*400 = 160000 which exceeds the maximum value of int variable. So wrong output will be shown when this program will be run.
Correction :

int i = 0;

should be changed as

long int i = 0;

to see the correct output.

3.2

Identify the error in the following program.

include<iostream>
using namespace std;
void main()
{
     int num[]={1,2,3,4,5,6};
     num[1]==[1]num ? cout<<"Success" : cout<<"Error";
}
Answer

num [1] = [1] num?. You should write index number after array name but here index number is mention before array name in [1] num

So expression syntax error will be shown.

Correction : num[1] = num[1]? is the correct format.

3.3

Identify the errors in the following program.

#include <iostream>
using namespace std;  
void main() 
{
     int i=5; 
     while(i)
  { 
    switch(i) 
    {
    default: 
    case 4: 
    case 5: 
    break; 
    case 1: 
    continue; 
    case 2: 
    case 3: 
    break; 
    }
i-; 
 }
}
Answer
case 1 :
continue;

The above code will cause the following situation:
Program will be continuing while value of i is 1 and value of i is updating. So infinite loop will be created.
Correction: At last line i- should be changed as i–;

3.4

Identify the errors in the following program.

#include <iostream> 
#define pi 3.14 
using namespace std;
int squareArea(int &); 
int circleArea(int &); 
void main()
{ 
     int a-10; 
     cout << squareArea(a) << " ";
     cout « circleArea(a) « "; 
     cout « a « endl; 
 {
     int squareArea(int &a) 
  {
     return a *== a; 
  }
   int circleArea(int &r) 
  {
   return r = pi * r * r; 
  }
Answer

Assignment operator should be used in the following line:

return a *==a;

That means the above line should be changed as follows:

return a *=a;
3.5

Missing

3.6

Find errors, if any, in the following C++ statements.

  • (a) long float x;
  • (b) char *cp = vp; // vp is a void pointer
  • (c) int code = three; // three is an enumerator
  • (d) int sp = new; // allocate memory with new
  • (e) enum (green, yellow, red);
  • (f) int const sp = total;
  • (g) const int array_size;
  • (h) for (i=1; int i<10; i++) cout << i << “/n”; (i) int & number = 100; (j) float *p = new int 1101; (k) int public = 1000; (l) char name[33] = “USA”;
Answer
No. Error Correction
(a) Too many types float x; or double x;
(b) type must be matched char *cp = (char*) vp;
(c) no error
(d) syntax error int*p=new int[10];
(e) tag name missing enum color (green, yellow, red)
(f) address have to assign instead of content int const*p = &total;
(g) C++ requires a const to be initialized const int array-size = 5;
(h) undefined symbol i for(int i=1;i<10;i++)

cout<<i<<“/n”;

(i) invalid variable name int number = 100;
(j) wrong data type float *p = new float[10];
(k) keyword can not be used as a variable name int public1 = 1000;
(l) array size of char must be larger than the number of characters in the string chat name[4] = “USA”;

Next Previous