Thursday 1 October 2015

Useful Programs for debugging

1.#include <stdio.h>
  #include <stdlib.h>

main()
{
   int dividend = 20;
   int divisor = 0;
   int quotient;
 
   if( divisor == 0){
      fprintf("Division by zero! Exiting...\n");
      exit(-1);
   }
   quotient = dividend / divisor;
   fprintf("Value of quotient : %d\n", quotient );

   exit(0);
}
ANS: check the prototype of fprintf. It should be fprintf(stderr,"division by zero exiting...\n");

2.#include<stdio.h>
  #include<conio.h>
  void main()
  {
char ch;
clrscr();
printf("A.Paper presentation\nB.Web designing\nC.Debugging\n");
printf("Enter your choice");
scanf("%c",&ch);
switch(ch)
{
case 'A'|'a':
ppt();
break;
case 'B'|'b':
web();
break;
case 'C'|'c':
debug();
break;
default:
printf("Invalid choice");
break;
}
void ppt()
{
printf("\nEnrolled in paper presentation");
printf("\nRegistered successfully");
}
void web()
{
printf("\nEnrolled in Web designing");
printf("\nRegistered successfully");
}
void debug()
{
printf("\nEnrolled in debugging");
printf("\nRegistered successfully");
}
getch();
  }
ANS: Nested function are not allowed

3.#include<stdio.h>
int main()
{

 int num,digit,r=0;
 char ch[1000];
 printf("Enter Number/Digit : ");
 scanf("%d", &num);
 while(num)
 {
  digit=num%10;
  num=num/10;
  switch(digit)
  {
    case 0: ch[r++]="Zero"; break;
    case 1: ch[r++]="One"; break;
    case 2: ch[r++]="Two"; break;
    case 3: ch[r++]="Three"; break;
    case 4: ch[r++]="Four"; break;
    case 5: ch[r++]="Five"; break;
    case 6: ch[r++]="Six"; break;
    case 7: ch[r++]="Seven"; break;
    case 8: ch[r++]="Eight"; break;
    case 9: ch[r++]="Nine"; break;
    default:
    exit(0);
  }
 }
 printf("Your Entered Number In Word : ");
 for(c=r; c>=0; c--)
    printf("%s", ch[c]);
 getch();
 return 0;
}
ANS: In switch case, it points to a memory location, hence the variable "ch" must be declared as pointer.

4.
#include<iostream.h>
#include<conio.h>
class Sample
{
public:
int a;
cout<<"Enter any number";
cin>>a;
public:
static void number()
{

if(a%3==0)
{
cout<<"FIZZ";
}
else if(a%5==0)
{
cout<<"BUZZ";
}
else if((a%3==0)&&(a%5==0))
{
cout<<"FIZZBUZZ";
}
}
};
void main()
{
Sample s;
s.number();
}

ANS: static function can be called without an object.

5.#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include <mem.h>
void main( )
{
      char arr1 = "Computer" ;
      char arr2 = "ComputerScience" ;
      int c ;
      c = memcmp ( arr1, arr2, sizeof ( arr1 ) ) ;
      if ( c == 0 )
      printf ( "\nStrings arr1 and arr2 compared using memcmp are identical" ) ;
      else
      printf ( "\nStrings arr1 and arr2 compared using memcmp are not identical") ;
      c = memicmp ( arr1, arr2, sizeof ( arr1 ) ) ;
      if ( c == 0 )
      printf ( "\nStrings arr1 and arr2 compared using memicmp are identical" );
      else
      printf ( "\nStrings arr1 and arr2 compared using memicmp are notidentical" ) ;
      getch();    
}

ANS: When we use memcmp & memicmp functions it checks the memory location, hence the variables provided in that functions should be a pointer variable.

6.#include<iostream.h>
#include<string.h>
#include<conio.h>
class abseg
{
public:
int a,b;
void welcome();
void getdata();
virtual void show()=0;
};
void abseg::welcome()
{
cout<<"WELCOME";
}
void abseg::getdata()
{
cout<<"Enter the values for a and b";
cin>>a>>b;
}
void abseg::show()
{
cout<<"your lucky number is "<<a+b<<a-b<<a*b<<a/b;
}
void main()
{
abseg a;
welcome();
getdata();
show();
getch();
}

ANS: We cannot create an instance for abstract class

7.#include<iostream.h>
#include<conio.h>
void main()
{
void test();
test();
}
void test()
{
static int y=100;
class sample
{
public:
int x;
static void print()
{
cout<<"X="<<x<<"Y="<<y;
}
};
sample s;
s.x=500;
s.print();
getch();
}

ANS: Static variable should be accessed with the class name and static funciton should be accessed without using the object.