Automata Fix Questions
Automata Fix Questions
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(“%d\n”, i);
i–;
}
return 0;
}
Input: 4
Output: Infinite loop
Input: 20
Output: -2102132736
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
5) Fix the error, recompile and match against the output provided.
int main(void)
{
printf(“This is a \”buggy” program\n”);
return 0;
}
Corrected program:
int main(void)
{
printf(“This is a \”buggy\” program\n”);
return 0;
}
Answer:
void binarytodecimal(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;
}
7) Print the prime numbers from an array up to given value n by using 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(“%d\n”, arr[i]);
}
return 0;
}
Answer:
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;
}
8. Find the syntax error in the below code without modifying the logic.
#include
int main()
{
float x = 1.1;
switch (x)
{
case 1: printf("Choice is 1");
break;
default: printf("Invalid choice");
break;
}
return 0;
}
Answer:
#include
int main()
{
int x = 1;
switch (x)
{
case 1: printf("Choice is 1");
break;
default: printf("Invalid choice");
break;
}
return 0;
}
The expression used in the switch must be an integral type (int, char, and enum). Any other
type of expression is not allowed.
void main () {
int i, j, n = 5;
for(i=1; i<n; i++)
{
for(j=i;j<n;j++);
{
printf("%d", i);
}
printf("\n");
}
}
Solution:
void main () {
int i, j, n = 5;
for(j=i;j<n;j++)
{
printf("%d", i);
printf("\n");
we use a semicolon in C statement to tell the compiler where’s the end of our statement.
Second for loop executes one time.
Find the index of equilibrium element in the given array. In an array equilibrium element is
the one where the sum of all the elements to the left side is equal to the sum of all the
elements in the right side.
Return Value:
2) In case there is more than one equilibrium element, return the element with least index
value.
You are required to complete the given code by reusing the existing function. You can click
on Compile & run anytime to check the compilation/execution status of the program you can
use printf to debug your code. The submitted code should be logically/syntactically correct
and pass all the test cases.
Test Case:
a[] = {1,2,3,4,3,3}. 4 is the equilibrium element since its left side sum (1+2+3) is equal to its
right side sum (3+3)
#include <stdio.h>
// Return the sum of elements from index 0 to (idx - 1)
int sum = 0, i;
sum += a[i];
return sum;
int sum = 0, i;
sum += a[i];
return sum;
}
int main() {
//code
int a[10], n, i;
scanf("%d", &n);
scanf("%d", &a[i]);
if(equiIndex != -1) {
printf("%d", a[equiIndex]);
}
return 0;
Solution:
int sum = 0, i;
sum += a[i];
return sum;
int sum = 0, i;
for(i = idx + 1; i < n; i++)
sum += a[i];
return sum;
int i;
return i;
}
return -1;
int main() {
//code
int a[10], n, i;
scanf("%d", &n);
scanf("%d", &a[i]);
if(equiIndex != -1) {
printf("%d", a[equiIndex]);
return 0;
}