Automata Fi Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Automata Fix Questions

Dr. Rajesh Kumar Panda


1) Check for syntax error/ logical error and correct the error to get
the desired output.
Given n, print from n to 0
int main()
{
int n;
scanf("%d", &n);
unsigned int i = n;
while(i >= 0)
{
printf("%dn", i);
i--;
}
return 0;
}
Correct Code:

#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i = n;
while(i >= 0)
{
printf("%d ", i);
i--;
}
return 0;
}
• Output: Infinite loop
• Answer: Error - Logical error
• unsigned int i = n; unsigned integer ranges
from 0 to 4294967295 , which will be taken in
the cyclic order.So it will keep repeating in a
cyclic way. The loop will never be terminated.
So it should be written as int i = n;

2) Find the output
#include<stdio.h>
int main()
{
unsigned int x=5;
if(x>-1)
printf("Hello");
else
printf("Ok");
}
3) Print the sum of fibonacci series nth term.
Series : 0, 1, 1, 2, 3, 5, 8, 13
Check syntax/logical error for the code
Example
Input
8
Output
33
Explanation:
The sequence generated by the system will be
0, 1, 1, 2, 3, 5, 8, 13
The sum till 8th term will be 33.
Incorrect Code:
#include <stdio.h>
int main()
{
int a=0,b=1,c,n,i=2,sum=0;
printf("n : ");
scanf("%d",&n);
while(i<=n)
{
c=a+b;
a=b;
b=c;
sum=sum+c;
i++;
}
printf("%d",sum);
return 0;
}
Correct Code:
#include <stdio.h>
int main()
{
int a=0,b=1,c,n,i=2,sum=1;
printf("n : ");
scanf("%d",&n);
while(i<n)
{
c=a+b;
a=b;
b=c;
sum=sum+c;
i++;
}
printf("%d",sum);
return 0;
}
Q4. You have to find the security key from the data
transmitted.
Input
The input consists of an integer data, representing
the data to be transmitted.
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
Incorrect Code:
#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;
}
Correct 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;
}
5) Fix the error, recompile and match against the
output provided.
Output : Welcome to “KIIT”
Incorrect Code
#include<stdio.h>
int main()
{
printf("Welcome to \“KIIT");
return 0;
}
Corrected Code
#include <stdio.h>
int main(void)
{
printf("Welcome to \“KIIT\"");
return 0;
}
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);
printf("Largest number among %d, %d and %d is %d.", a, b, c,
max_num);
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);

printf("Largest number among %d, %d and %d is %d.", a, b, c,


max_num);
return 0;
}
7) Check whether the below program print the below pattern
Input
enter number of rows : 4
Output
1
22
333
4444
4444
333
22
1
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;
}
8) Check for syntax error/ logical error and correct
the error to get the desired output.
Given n, print 0 to n by identifying whether the
n is even or odd.
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;
}
9) Find the error in the below code without modifying the
logic.
#include<stdio.h>
int main()
{
float x = 1.1;
switch (x)
{
case 1: printf("Choice is 1");
break;
default: printf("Invalid choice");
break;
}
return 0;
}
#include<stdio.h>
int main()
{
int x = 1;
switch (x)
{
case 1: printf("Choice is 1");
break;
default: printf("Invalid choice");
break;
}
return 0;
}
10) Find the logical error in the below code.
#include<stdio.h>
void main ()
{ int i, j, n = 5;
for(i=1; i<n; i++)
{
for(j=i;j<n;j++);
{
printf("%d", i);
}
printf("\n");
}
}
#include<stdio.h>
void main ()
{ int i, j, n = 5;
for(i=1; i<n; i++)
{
for(j=i;j<n;j++)
{
printf("%d", i);
}
printf("\n");
}
}
11) Code reuse: Convert Binary to Decimal by using
the existing function.
int binarytodecimal(number)
{
// Type your code here
}
void main()
{
int num;
scanf("%d", &num);
printf(%d, binarytodecimal(num);
}
int binarytodecimal(number)
{
int dval=0, base=1, rem;
while(number > 0)
{
rem = number % 10;
dval = dval + rem * base;
number = number / 10;
base = base * 2;
}
return dval;
}
12) 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; }
int isprime(int num)
{
int i;
int isprime = 1;
for(i = 2; i <= num / 2; i++)
{
if(num % i == 0)
{
isprime = 0;
break;
}
}
return isprime;
}

You might also like