Cognizant Code Debugging

Questions 1: Fix the errors, recompile and match against the output provided.

Output:
This is an "error free" program.

Given Code:

#include < stdio.h >
int main()
{
printf("This is an
 "error free" program");
return 0;
}

Explanation: In the above program, there's a syntax error on line number 4 :

printf("This is an "error free" program.");
We will have to use a backslace ( \ ) to print quote in printf.

Correct Program:
#include < stdio.h >
int main()
{
printf("This is an 
\"error free\" program");
return 0;
}

Questions 2: Complete the code to display the factorial value of the given number as shown in Sample Ouptut:

Output:
Factorial of 5 is 120

Given Code:

#include < stdio.h >
int main(){
  int num,fact;
  scanf("%d",&num);
  // User Code
  printf("Factorial of %d
   is %d",num,fact);
  return 0;
}

Explanation: In the above program, you will have to write your own logic to complete the code.

Correct Program:
#include < stdio.h >
int main(){
 int num,fact;
 scanf("%d",&num);
 // User Code
 fact = 1;
 int temp = num;
 while( temp!=0 )
 {
   fact = fact * temp;
   temp--;
}
 printf("Factorial of %d
  is %d",num,fact);
 return 0;
}


Questions 3: Check whether the given program can find greatest of 3 numbers.

Input: 2 8 1
Output: 8

Given Code:

#include < stdio.h >
  int main(){
  int n1,n2,n3;
  scanf("%d %d %d",&n1,&n2,&n3);
  if(n1>n2) && (n1>n3)
    printf("%d",n1);
  elseif ( n2>n3)
    printf("%d",n2);
  else
    printf("%d",n3);
  return 0;
}     

Explanation: In the above program, there are some syntax error :

Error:
if(n1>n2) && (n1>n3)
elseif

Correct Program:
#include < stdio.h >
int main(){
  int n1,n2,n3;
  scanf("%d %d %d",&n1,&n2,&n3);
  if ( (n1>n2) && (n1>n3) )
   printf("%d",n1);
  else if( n2>n3)
   printf("%d",n2);
  else
   printf("%d",n3);
  return 0;
}     

Questions 4: The function patternPrint(int n) is supposed to print n number of lines in the following pattern:
for n=4 pattern should be
1
1 1
1 1 1
1 1 1 1

The function compiles successfully but fails to return the desired results due to logical errors. Your task is to debug the program to pass all the test cases.


Given Code:

void patternPrint(int n)
{
 int print=1,i,j;
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=i;j++)
  {
  printf("%d",print);
  printf("\n");
  }
 }
}     

Explanation: In this function, there is no syntax error but there's a logical error.
printf("\n"); should be used in outer for loop.

Correct Program:
void patternPrint(int n)
{
 int print=1,i,j;
 for(i=1;i<=n;i++)
 {
  for(j=1;j<=i;j++)
  {
  printf("%d",print);
  }
  printf("\n");
 }
}


Questions 5: Correct the syntax error in the given code without modifying the logic to get the below output.

Output:
Choice is 1

Given Code:

#include < stdio.h >
  int main(){
  float x=1.1;
  switch(x){
   case1: printf("Choice is 1");
    break;
   default: printf("Invalid Choice");
    break;
   }
return 0;
}        


Explanation: In the above code there are some sytax error. float cannot be used with swtich. We need to replace float with int and assign x=1.

Correct Code:
 #include < stdio.h >
int main(){
 int x=1;
 switch(x){
  case1: printf("Choice is 1");
   break;
  default: printf("Invalid Choice");
   break;
 }
 return 0;
}        

Questions 6: Debug and fix the code so that it can print the below pattern.

Pattern:

2 4
2 4
2 4
2 4

Given Code:

#include < stdio.h >
int main()
{
 for(int i=1;i <= 4 ;i++)
 {
  for(  z = 1 ; z <= 4 ; z++)
  {
  if(z % 3 == 0)
  {
  printf("%d ",z);
  }
  else
  {
  printf(" ");
  }
  }
   printf("\n");
 }
 return 0;
}


Explanation: In the inner loop variable z is not declared. And inside loop there is one if condition so if you write it as if (z % 2 == 0) then this code will execute successfully and will print the above given pattern.

Error :- Logical and syntax error

Correct Code:
#include < stdio.h >
int main()
{
 for(int i=1;i <= 4;i++)
 {
  for(int z=1;z <= 4;z++)
  {
  if(z % 2 == 0)
  {
   printf("%d ",z);
  }
  else
  {
  printf(" ");
  }
  }
 printf("\n");
 }
 return 0;
}      

Questions 7: Debug and fix the code so that it can print the below pattern.

Pattern:

*
 *
  *
   *

Given Code:
#include < stdio.h >
int main()
{
 for(int i=1;i <= 4;i--)
 {
 for(int j=1;j <= 4; j++)
 {
  if(i != j)
  {
  printf("*");
  }
  else
  {
  printf(" ");
  }
 }
printf("\n");
}
return 0;
}


Explanation: In the outer loop value of i is decrementing in every iteration. So there value of i should increment in every iteration so write there i++. And in inner loop there is if condition write that condition as if( i == j). After making these two change this code will run successfully and will print the given pattern.

Error :- Logical error

Correct Code:
#include < stdio.h >
int main()
{
 for(int i = 1 ; i <= 4 ; i++)
 {
 for(int j = 1 ; j <= 4 ; j++)
 {
  if(i == j)
  {
  printf("*");
  }
  else
  {
  printf(" ");
  }
  }
printf("\n");
 }
 return 0;
}

Questions 8: Debug and fix the below code so that it can print 0 as an output.

Given Code:

#include < stdio.h >
int main()
{
  int z = 8 = 9 = 7 ;
  printf("%d",z);
  return 0;
}


Explanation: At the first line there is assignment operator which will generate error so instead of that statement there should be int z = 8 == 9 == 7; So first 8 will be comapared with 9 so 8 is not equal to 9 so it is false . so false means 0 in C language. Now this 0 will be comopared with 7. Now also 0 is not equal to 7 so this is false and we will get 0 , that 0 will be assigned into z. And at next line it will print the value of z that is 0.'

Error :- Syntax error

Correct Code:
#include < stdio.h >
int main()
{
 int z = 8 == 9 == 7 ;
 printf("%d",z);
 return 0;
}  

Questions 9: Debug and fix the code so that it can print the below pattern.

Pattern:

****
 **
**** 

Given Code:
#include < stdio.h >
int main()
{
 int x = 1;
 switch(x)
 {
 case 1 :
  printf("****");
  printf("\n");
  break;
 case 2:
  printf(" ** ");
  printf("\n");
 case 3 :
  printf("****");
  printf("\n");
 default :
  printf("****");
  break;
  }
  return 0;
}  


Explanation: Remove break from the case 1 and add a break in case 3 then it will successfully print the above given pattern.

Error :- Logical error

Correct Code:
#include < stdio.h >
int main()
{
 int x = 1;
 switch(x)
 {
  case 1 :
   printf("****");
   printf("\n");
  case 2:
   printf(" ** ");
   printf("\n");
  case 3 :
   printf("****");
   printf("\n");
   break;
  default :
   printf("****");
   break;
  }
  return 0;
}      

Questions 10: Debug and fix the below code so that it can print the below output.

Output:
9 8 7 6 5 4 3 2 1 0

Given Code:

#include < stdio.h >  
int main()
{
 int x = 10;
 do
 {
 printf("%d ",x);
 x--;
 }while(x >= 0);
 return 0;
}    


Explanation: inside the loop decrement the value of x before printf statement.So x-- should be at first line inside the loop. Change condition of while and write it as while( x > 0). AFter making these two changes the above code will give you the desired output.

Error :- Logical error

Correct Code:
#include < stdio.h >
int main()
{
 int x = 10;
 do
 {
  x--;
  printf("%d ",x);
 }while(x > 0);
 return 0;
}    

If you find any question wrong, please send your solution to [email protected]


More Questions Will Be Uploaded Soon............