Join Regular Classroom : Visit ClassroomTech

Automata Fixing

Problem 41:
The methods GetDigitSum(intarr[]) of class DigitSum accepts an integers array arr it is supposed to calculate the sum of digits of the even of the smallest elements in the input array it returns 1 if the calculated sum is even and returns 0 otherwise. However there is a compliation error in the code your task is to fix it so that the program works for all the input values Note
The methods getdigitSum uses another method getSum(int sum) which returns the sum of the digits of the input number num

Given Code:

int getDigitSum(int arr[i])
{
int result,len=arr.length;
for(int i=0;min=arr[0];i<len;i++)
{
if(arr[i]<min)
min=arr[i];
}
results=getSum(min)
if(results%2==0)
return 1;
else
min==arr[j];
}
int getSum(int num)
{
//WRITE YOUR CODE HERE
}

Solution in C

int getDigitSum(int arr[i])
{
int result,len=arr.length;
int min;
for(int i=0;min=arr[0];i<len;i++)
{
if(arr[i]<min)
min=arr[i];
}
results=getSum(min)
if(results%2==0)
return 1;
else
return 0;
}
int getSum(int num)
{
//WRITE YOUR CODE HERE
int rem,sum=0;
while(num)
{
rem=num%10;
sum+=rem;
num/=10;
}
return sum;
}

Problem 42 :
Code Approach: For this question, you will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods. Lisa always forgets her birthday which is on 5Th july In order to help her we have function CheckBirthDay(char *month,int day) which takes day and month as inputs and returns 1 if its her birthday and returns a 0 otherwise The function compiles fine but to return desired results for some cases Your task to fix the code so but that it passes at test cases 15(1).

TestCase 1;
Input
July 13
Expected Return Value:
0

TestCase 2:
Input
April 3
Expected Return Value:
0

Given Code:

int checkBirthday(char* month,int day)
{
if(strcmp(month,"july") || (day =5))
return 1;
else
return 0;
}
int main()
{
char inp[]="july";
int day=5;
if(checkBirthday(inp,day)==1)
printf("Yes");
else
printf("No");
return 0 ;
}

Solution in C

int checkBirthday(char* month,int day)
{
if(strcmp(month,”july”)==0 && (day ==5))
return 1;
else
return 0;
}
int main()
{
char inp[]=”july”;
int day=5;
if(checkBirthday(inp,day)==1)
printf(“Yes”);
else
printf(“No”);
return 0 ;
}

Problem 43 :
Matrix Sum
Given Code:

int MatrixSum(int m, int n, int mat[m][n])
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum+ =mat[i] (j);
}
}
return sum;
}

Solution in C

int MatrixSum(int m, int n, int mat[m][n])
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
sum+=mat[i][j];
}
}
return sum;
}

Problem 44 :
Find the number of occurrences of a given value in the array.
Given Code:

#include
int occurrence(int *arr, int len,int value)
{
int i=0,count=0;
while(i<len)
{
if(arr[i]==value)
count++;
}
return count;
}

Solution in C

#include
int occurrence(int *arr, int len,int value)
{
int i=0,count=0;
while(i<len)
{
if(arr[i]==value)
count++;
i++;
}
return count;
}

Problem 45 :
The function patternPrint(int n) supposed to print n number of lines in the following pattern For n=4 the pattern should be:
1
1 1
1 1 1
1 1 1 1
The function complies 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(int i=0;i<n;i++)
{
for(j=0;j<=i;j++)
{
printf("%d",print);
}
}
print("n");
}
}

Solution in C

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

Problem 46 :
The function removeElement(int *arr,intlen,int index)takes an array arr of length len as an input. It is supposed to return an array len-1 after removing the integer at the given index in the input arrayarr. If the given index is out of bounds, then this function should return the input array arr. The function compiles successfully but fails to return the desired result due to logical errors */

Solution in C

int* removeelement( int *arr, int len, int index)
{
int i,j;
if(index<len)
{
for(i=index;i<len-1;i++)
{
arr[i]=arr[i+1];
}
int *rarr =(int*)malloc(sizeof(int)*(len-1));
for(i=0;i<len-1;i++)
rarr[i]=arr[i];
return rarr;
}
else
return arr;
}

Problem 47:
Replace a given array with zeros and ones depending on the even or odd criteria of the array length. /
/WRITE DOWN YOUR CODE HERE

int *replaceValues(int *arr, int len)
{
int i;
for(i=0;i<len;i++)
arr[i]=len%2;
return arr;
}
Question: 22 Selection Sort
Given Code:
int * sortArray(int *arr, int len)
{
int x=0,y=0,n=len;
int index_of_min, temp;
for(x=0;x<n;x++)
{
index_of_min=x;
for(y=x;y<n;y++)
{
if(arr[index_of_min]>arr[y])
{
index_of_min=y;
}
}
temp = arr[x];
arr[x] = arr[index_of_min];
arr[index_of_min] = temp;
}
return arr;
}

Solution in C

int * sortArray(int *arr, int len)
{
int x=0,y=0,n=len;
int index_of_min, temp;
for(x=0;x<n;x++)
{
index_of_min=x;
for(y=x;y<n;y++)
{
if(arr[index_of_min]>arr[x])
{
index_of_min=y;
}
}
temp = arr[x];
arr[x] = arr[index_of_min];
arr[index_of_min] = temp;
}
return arr;
}

Problem 48:
You are required to fix all logical errors in the given code. You can click on Compile &Run anytime to check the compilation/execution status of the program. You can use System.out.println to debug your code. The submitted code should be logically/syntactically correct and pass all testcases. Do not write the main() function as it is not required.

Code Approach: For this question, you will need to correct the given implementation. We do not expect you to modify the approach or incorporate any additional library methods. The method deleteDuplicate(intarr[]) of classDistinctArray takes an array as an input it is supposed to remove duplicates integers from the input array arr such that for each distinct integer the first occurrence is retained and all the duplicates elements following it are removed for Example given input array (2,3,2,2,5,6,6,7) the expected output is (2,3,5,6,7). The function complies successfully but fails to return the desired results due to logical errors Your task is debug the program to pass all the test cases.
Given Code:

int* deleteDuplicate (int *arr, int len)
{
int count=0,p,i,j,k=0,originalLength=len;
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(arr[j]==arr[i])
{
arr[k]=arr[k+1];
}
len=len-1;
count=count+1;
j=i;
}
}
return arr;
}

Solution in C

int* deleteDuplicate (int *arr, int len)
{
int count=0,p,i,j,k=0,originalLength=len;
for(i=0;i<len;i++)
{
for(j=i+1;j<len;j++)
{
if(arr[j]==arr[i])
{
for(k=j;k<len-1;k++)
arr[k]=arr[k+1];
}
len=len-1;
count=count+1;
j=i;
}
}
return arr;
}
Categories
Pages
Recent Posts