Debuggathon
Debuggathon
Check for syntax error/ logical error and correct the error to get the desired
output.
The function sortString modifies the input list by sorting its elements depending
upon the length of the array, i.e; if the length of the array is even, then the
elements are arranged in the ascending order, and if the length of the array is
odd, then the elements are arranged in the descending order
The function sortString accepts two arguments – len representing the length of the
string, and arr a list of characters, representing the input list respectively.
The function sortString compiles successfully but fails to get the desired results
for some test cases due to logical errors. Your task is to fix the code, so that it
passess all the test cases
Incorrect Code
}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}
else
{
for(i=0;i<len;i++)
{
max=arr[i];
location = i;
for(j=i;j<len;j++) if(max>arr[j])//error in this line
{
max=arr[j];
location = j;
}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}
}
Correct Code
void sortArray(int len, int *arr)
{
int i, max, location, temp, j,k;
if(len%2 == 0)
{
for(i=0;i<len;i++)
{
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max>arr[j])
{
max=arr[j];
location = j;
}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}
else
{
for(i=0;i<len;i++)
{
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max<arr[j])
{
max=arr[j];
location = j;
}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}
}
Question : 2
Check for syntax error/ logical error and correct the error to get the desired
output.
The function maxReplace print space separated integers representing the input list
after replacing all the elements of the input list with the sum of all the element
of the input list.
The function maxReplace accepts two arguments – size an integer representing the
size of the input list and inputList, a list of integers representing the input
list respectively.
The function replaceElements is modifying the input list in such a way – if the sum
of all the elements of the input list is odd, then all the elements of the input
list are supposed to be replaced by 1s, and in case if the sum of all the elements
of the input list is even, then the elements should be replaced by 0s.
For example, given the input list [1,2,3,4,5], the function will modify the input
list like [1, 1, 1, 1, 1]
The function replaceElements compiles successfully but fails to get the desired
result for some test cases due to incorrect implementation of the function. Your
task is to fix the code so that it passes all the test cases
}
}
else
{
j=1;
while(j<size)
{
arr[j]=1;
j+=1;
}
}
Corrected Code
}
}
else
{
j=1;
while(j<size)
{
arr[j]=1;
j+=1;
}
}
Question : 4
Check for syntax error/ logical error and correct the error to get the desired
output.
Test Case : 1
n : 10
Output
0 2 4 6 8 10
Test Case : 2
n : 9
Output
1 3 5 7 9
Incorrect Code
#include <stdio.h>
int main()
{
int n, i;
printf("n : ");
scanf("%d",*n);
if(n%2=0)
{
for(i=0;i<n:i=i+2)
{
printf("%d ",i);
}
}
else
{
for(i=1;i<n;i=i+2)
{
printf("%d ",i);
}
}
return 0;
}
Corrected Code
#include <stdio.h>
int main()
{
int n, i;
printf("n : ");
scanf("%d",&n);
if(n%2==0)
{
for(i=0;i<=n;i=i+2)
{
printf("%d ",i);
}
}
else
{
for(i=1;i<=n;i=i+2)
{
printf("%d ",i);
}
}
return 0;
}
Question : 5
Check whether the below program print the below pattern
Input
Output
22
333
4444
4444
333
22
Incorrect Code
#include <stdio.h>
int main()
{
int i,j,n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
{
for(j=1;j<=i;j=j+1)
{
printf("%d",i);
}
printf("\n");
}
for(i=n;i>=1;i=i-1)
{
for(j=1;j<=i;j=j-1)
{
printf("%d",i);
}
}
return 0;
}
Corrected Code
#include <stdio.h>
int main()
{
int i,j,n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
{
for(j=1;j<=i;j=j+1)
{
printf("%d",i);
}
printf("\n");
}
for(i=n;i>=1;i=i-1)
{
for(j=1;j<=i;j=j+1)
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
Question : 6
Check for syntax/logical error and correct the code for desired output.
In the code you need to find the greatest among three numbers.
Incorrect Code
#include <stdio.h>
int main()
{
int a, b, c, max_num;
printf("Enter the three numbers\n");
printf("First: ");
scanf("%d",&a);
printf("Second: ");
scanf("%d",&b);
printf("Third: ");
scanf("%d",&c);
max_num = (a > b) ? (a > c ? a : c) ? (b > c ? b : c);
return 0;
}
Corrected Code
#include <stdio.h>
int main()
{
int a, b, c, max_num;
printf("Enter the three numbers\n");
printf("First: ");
scanf("%d",&a);
printf("Second: ");
scanf("%d",&b);
printf("Third: ");
scanf("%d",&c);
max_num = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
return 0;
}
Question : 7
Fix the error, recompile and match against the output provided.
Incorrect Code
#include<stdio.h>
int main(void)
{
printf("Welcome to \"prepinsta");
return 0;
}
Corrected Code
#include <stdio.h>
int main(void)
{
printf("Welcome to \"prepinsta\"");
return 0;
}
Print the prime numbers from an array up to given value n by using existing
function.
Problem: Write a program in C to display the table of a number and print the sum
of all the multiples in it.
Test Cases:
TestCase 1:
Input:
275
TestCase 2:
Input:
12
12, 24, 36, 48, 60, 72, 84, 96, 108, 120
660
#include
int main()
{
// write your code here
}
Corrected Code
#include <stdio.h>
int main()
{
int n, i, value=0, sum=0;
printf("Enter number : ",n);
scanf("%d",&n);
printf("\nsum : %d",sum);
return 0;
}
Question : 10
Question
You have to find the security key from the data transmitted.
Input
Output
Print an integer representing the security key for the given data.
Example
Input
578378923
Output
783
Explanation
The repeated digits in the data are 7, 8 and 3. So, the security key is 783
#include <stdio.h>
#include <string.h>
int main()
{
char a[50];
int i, j, len, count=0;
scanf("%s",a);
strlen(a);
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(a[i]=a[j])
{
printf("%c",a[i]);
break;
}
}
}
return 0;
}
Corrected Code
#include <stdio.h>
#include <string.h>
int main()
{
char a[50];
int i, j, len, count=0;
scanf("%s",a);
len = strlen(a);
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(a[i]==a[j])
{
printf("%c",a[i]);
break;
}
}
}
return 0;
}
Question : 11
function/method compiles successfully but fails to return the desired result for
some/all cases due to incorrect implementation. Your task is to correct the code so
that it passes all test cases.
function/method compiles successfully but fails to return the desired result for
some/all cases due to incorrect implementation. Your task is to correct the code so
that it passes all test cases.
int main()
{
int n;
scanf("%d", &n);
unsigned int i = n;
while(i >= 0)
{
printf("%dn", i);
i--;
}
return 0;
}
Input: 4
unsigned int i = n; unsigned integer ranges from 0 to 65535, which will be taken in
the cyclic order.Soi-- will keep repeating in a cyclic way. The loop will never be
terminated. So it should be written as int i = n;
question 14
int main()
{
long int fact = 1, n, i;
scanf("%d", &n);
Input: 20
Output: -2102132736
The fact and n are declared d as long int, so in scanf and printf%ld should be used
in place of %d.
question 15
Check whether the below program print the below pattern
1111
222
33
void main()
{
int i, j, n;
scanf("%d", &n);
for(i = 1; i<=n; i++)
{
for(j = 1; j<=n; j++)
{
printf("%d", i);
}
printf("\n");
}
}
Input: 3
Output:
111
222
333
The inner for loop has to be written in this way: for(j = i-1; j<n+1; j++)
question 16
Find the greatest of three numbers.
int main()
{
int num1, num2, num3;
scanf("%d %d %d", &num1,&num2,&num3);
if (num1 > num2) && (num1 > num3)
{
printf("%d", num1);
}
elseif(num2>num3)
{
printf("%d", num2)
}
else
{
printf("%d", num3);
}
return 0;
}
if (num1 > num2) && (num1 > num3) it has to be written as if ((num1 > num2) &&
(num1 > num3)) and this line else if (num2>num3) should be rewritten as else if
(num2>num3)
question 17
Fix the error, recompile and match against the output provided.
int main(void)
{
printf("This is a "buggy" programn");
return 0;
}
Corrected program:
int main(void)
return 0;
question 18
Code reuse: Convert Binary to Decimal by using the existing function.
voidbinarytodecimal(number)
{
// Type your code here
}
void main()
{
int num;
scanf("%d", &num);
printf(%d, binarytodecimal(num);
}
Answer:
voidbinarytodecimal(number)
{
int dval=0, base=1, rem;
while(number > 0)
{
rem = number % 10;
dval = dval + rem * base;
num = number / 10;
base = base * 2;
}
return dval;
}
question 19
Print the prime numbers from an array up to a given value n by using the existing
function.
int isprime(int num)
{
// type your code here
}
int main()
{
int n, m, arr[100], size=0, i;
scanf("%d", &n);
for(m = 2; m <= n; m++)
{
if(isprime(m))
{
arr[size++]= m;
}
}
for(i = 0; i < size; i++)
{
printf("%dn", arr[i]);
}
return 0;
}
Answer:
question 20
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 :
Correct Program:
#include < stdio.h >
int main()
{
printf("This is an
\"error free\" program");
return 0;
}
question 21
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:
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;
}
program 22
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:
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.
Correct Code:
#include < stdio.h >
int main()
{
int x = 10;
do
{
x--;
printf("%d ",x);
}while(x > 0);
return 0;
}
program 23
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.
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;
}
program 24
Debug and fix the below code so that it can print 0 as an output.
Given Code:
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;
}
program 25
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;
}
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;
}