Amcat Automata Fix Basic Programming

Amcat Automata Fix Basic Programming



Amcat Automata Fix Basic Programming


In this post, you will find questions & answers related to Amcat automata Fix basic programming which is easy and very important for the Amcat test you can't afford to miss it if you want to score well.


1)  Find out the syntax 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;
}
Ans.

#include <stdio.h>
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 different form of expression isn't allowed.

2) Complete the below code by making the reuse of the existing functionality.


Find the index of the 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 on the right side.


Return Value:

1) Return -1 if no equilibrium element is found

2) In case there is more than one equilibrium element, return the element with the least index value.


You are supposed 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.


Code approach For the question:


Correct the given implementation.

 Do not modify the approach.


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 left_side_sum(int a[], int n, int idx)
{
    int sum = 0, i;
    for(i = 0; i < idx; i++)
    {
        sum += a[i];
    }
    return sum;
}

// Return the sum of elements from index (idx + 1) to (n - 1)
int right_side_sum(int a[], int n, int idx)
{
    int sum = 0, i;
    for(i = idx + 1; i < n; i++)
    {
        sum += a[i];
    }

    return sum;
}

// returns -1 if no equilibrium index found
int findEquilibriumIndex(int a[], int n)
{
    // Type your code here
}

int main() {
              //code
   int a[10], n, i;

   // get the elements count
   scanf("%d", &n);

   // get the array elements
   for(i=0; i<n; i++)
   {
       scanf("%d", &a[i]);
   }

   int equiIndex = findEquilibriumIndex(a, n);
   if(equiIndex != -1) {
       printf("%d", a[equiIndex]);
   }
   return 0;
} 


Ans.

// Return the sum of elements from index 0 to (idx - 1)
int left_side_sum(int a[], int n, int idx)
{
    int sum = 0, i;
    for(i = 0; i < idx; i++)
    {
        sum += a[i];
    }

    return sum;
}

// Return the sum of elements from index (idx + 1) to (n - 1)
int right_side_sum(int a[], int n, int idx)
{
    int sum = 0, i;
    for(i = idx + 1; i < n; i++)
    {
        sum += a[i];
    }

    return sum;
}

// returns -1 if no equilibrium index found
int findEquilibriumIndex(int a[], int n)
{
    // Type your code here
    int i;
    for(i = 0; i < n; i++)
    {
        if(left_side_sum(a, n, i) == right_side_sum(a, n, i))
        {
            return i;
        }
    }
   
    return -1;
}

int main() {
              //code
   int a[10], n, i;
   // get the elements count
   scanf("%d", &n);
   // get the array elements
   for(i=0; i<n; i++)
   {
       scanf("%d", &a[i]);
   }

   int equiIndex = findEquilibriumIndex(a, n);
   if(equiIndex != -1) {
       printf("%d", a[equiIndex]);
   }

   return 0;
}

3) Find the error in the below code.


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

Ans.

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


A semicolon in the C statement is used to tell the compiler where's the end of the statement. Second, for loop executes one time.


4) Correct the below code.



void sortArray(int len, int *arr)
{
    int i, max, location, temp, j,k;
    if(len/2 == 0)//error in this line
    {
    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;
    }
    }
    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;
    }
    }
}


Ans.


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;
    }
    }
}

5) Correct the below code.


void maxReplace(int size, int *inputList)
{
    int i,sum=0;
    for(i=0;i<size;i++)
    {
         sum += inputList[i];
    }
        for(i=0;i<size;i++)
        {
            sum = inputList[i];//error in this line
        }
    for(i=0;i<size;i++)
    {
        printf("%d ",inputList[i]);
    }
}

Ans.


void maxReplace(int size, int *inputList)
{
    int i,sum=0;
    for(i=0;i<size;i++)
    {
         sum += inputList[i];
    }
        for(i=0;i<size;i++)
        {
            inputList[i]=sum;
        }
    for(i=0;i<size;i++)
    {
        printf("%d ",inputList[i]);
    }
}


6) Check for syntax errors and correct the error to get the proper 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

Wrong 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;
}

Ans.

#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;
}



No comments:

For Query and doubts!

Powered by Blogger.