4.1

Identify the error in the following program.

#include <iostream>
using namespace std; 
int fun()
{ 
     return 1;
} 
float fun() 
{
     return 10.23; 
}
void main()
{ 
     cout <<(int)fun() << ' '; 
     cout << (float)fun() << ' ';  
}
Answer

Here two function are same except return type. Function overloading can be used using different argument type but not return type.

Correction : This error can be solved as follows :

#include<iostream>
using namespace std; 
int fun()
{
    return 1;
}
float fun1()
{
    return 10.23;
}
 
void main()
{
     cout<<fun()<<"  ";
     cout<<fun1()<<"  ";
}
4.2

Identify the error in the following program.

#include <iostream>
using namespace std; 
void display(const Int constl=5)
{ 
   const int const2=5;
   int arrayl[constl]; 
   int array2[const2]; 
   for(int 1=0; i<5; 1++) 
  {  
     arrayl[i] = i; 
     array2[i] = i*10; 
     cout <<arrayl[i]<< ' ' << array2[i] << ' ' ; 
  }
} 
void main()
{ 
   display(5); 
}
Answer
#include<iostream>
using namespace std; 
void display()
{
     const int const1=5;
     const int const2=5;
     int array1[const1];
     int array2[const2];
 
       for(int i=0;i<5;i++)
       {
           array1[i]=i;
           array2[i]=i*10;
           cout<<array1[i]<<"  "<<array2[i]<<"  ";
       }
}
 
void main()
{
    display();
}
4.3

Identify the error in the following program.

#include <iostream>
using namespace std; 
int gValue=10; 
void extra() 
{
     cout << gValue << ' ';
}
void main()
{ 
    extra();
    {
    int gValue = 20; 
    cout << gValue << ' ';
    cout << : gValue << ' ';
    }
} 
Answer

Here cout << : gvalue << ” “; replace with cout <<::gvalue<< ” “;

#include <iostream>
using namespace std; 
int gValue=10; 
void extra() 
{
     cout << gValue << ‘ ‘;
}
void main()
{ 
    extra();
    {
    int gValue = 20; 
    cout << gValue << ‘ ‘;
    cout <<::gvalue<< " ";
    }
}
4.4

Find errors, if any, in the following function definition for displaying a matrix: void display(int A[][], int m, int n)

{
  for(1=0; i<m; i++)
  for(j=o; j<n; j++)
   cout<<" "<<A[i][j];
   cout<<"\n";
}
Answer

First dimension of an array may be variable but others must be constant.

Here int A [ ] [ ] replace by the following code:
int A [ ] [10];
int A[10] [10];
int A[ ] [size];
int A [size] [size];

Where const int size = 100;
any other numerical value can be assigned to size.


Next Previous