Automata Question-4
Q4. You are required to fix all logical errors in the given code. You can click on Compile and
Run anytime to check the compilation/execution status of the program. You can use
System.out.printin 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 replaceValues(int arr[]) of class ArrayOperation accepts an array arr as an input
and returns an array of the same length.
If the length of arr is odd, all the elements of arr are supposed to be replaced by 1s and in case
it is even, the elements should be replaced by 0s.
For example: given the input array {0, 1, 2}, the function will return the array {1, 1, 1}.
The function compiles successfully but fails to return the desired result due to logical errors.
Your task is to debug the program to pass all the test cases.
1 // You can print the values to stdout for debugging
2 public class ArrayOperation
3 {
4 public static int[] replaceValues(int arr[])
5 {
6 int i, j, len = arr.length;
7 if(len%2==0)
8 {
9 for(i=0; i<=len; i++)
10 arr[i] = 0;
11 }
12 else
13 {
14 for(j=0; j<=len; j++)
15 arr[j] = 1;
16 }
17 return arr;
18 }
19 }
Output. TestCase 1:
Status:
Correct
Expected:
[0, 0, 0, 0, 0, 0]
Returned:
[0, 0, 0, 0, 0, 0]
TestCase 2:
Ststus:
Correct
Expected:
[1, 1, 1, 1, 1]
Returned:
[1, 1, 1, 1, 1]
This program was also checked on another testcases. 6 out of 6 passed.
1 // You can print the values to stdout for debugging
2 public class ArrayOperation
3 {
4 public static int[] replaceValues(int arr[])
5 {
6 int i, j, len = arr.length;
7 if(len%2 == 0)
8 {
9 for(i=0; i<len; i++)
10 arr[i] == 0;
11 }
12 else
13 {
14 for(j=0; j<len; j++)
15 arr[j] = 1;
16 }
17 return arr;
18 }
19 }
Login/Signup to comment