Automata _code_Fix
Automata _code_Fix
#include <iostream>
int main() {
int starting_point,
ending_point, arr[20],
length, j;
for (j = 0; j <=
length; j++) {
if (arr[j] >=
starting_point && arr[j]
<= ending_point) {
return 0;}
Input: x = 2.00000, n =
10
Expected Output:
1024.00000
double myPow(double
x, int n)
return (x,n);
bool isUgly(int n) {
if(n==0)return
false;
While
n/=2;(n%2==0)
While
n/=3;(n%3==0)
whilen/=5;(n%5==0)
return n==1;
Void
binarytointeger(number
)
{
// Type your code
here
}
void main()
{
int num;
cin>>num;
binarytointeger(num);
}
print from 1 to n.
The function/method
main compiles
unsuccessfully due to
syntactical error , your
task is to debug the
program
is to debug the program
so that it passes all test
case
Int main(){
Int n;
cin<<n;
for(int
i==1;i>=n;i++);{
cout>>i;
}
}
#include
Int main(){
Int n;
Scanf(%f,”&n”);
Printf(%f,”n”);
}
int fib(int n) {
if(n<=1) return n;
int prev2 = 0,
prev1 = 1, cur;
for(int
i=2;i<=n;i++){
//write your code
here.
}
return cur;
}
9. Review the code for int binarysearch(int https://prepinsta.com/
syntax errors and arr[], int n, int k) { amcat/automata-fix/
logical mistakes for int start = 0;
binary search. int end = n - 1;
Statement:Given two
non-decreasingly sorted
integer arrays, nums1
and nums2, which are
already sorted in
non-decreasing order.
Additionally, you are
given two integers, m
and n, representing the
number of elements in
nums1 and nums2,
respectively. void merge(int
nums1[], int m, int
nums2[], int n) {
Your task is to merge int j = 0;
the elements from for(int i = m; i < m +
nums1 and nums2 into n; i++) { // Fixed loop
a single array and store condition
the result in nums1. nums1[i] =
The total number of nums2[j]; // Copy
elements in nums1 elements from nums2
should be m + n. to nums1
j++;
void merge(int }
nums1[], int m, int sort(nums1, nums1 +
nums2[], int n) { m + n); // Sort the final
int j = 0; merged array
for(int i = m; i < }
(); i++) {
//Write your code
here.
}
sort(nums1, nums1
+ m + n);
}
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;
}
}
}
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++)
{
cout<<inputList[i];
}
}
The function
replaceElements
compiles successfully
but fails to get the
desired result for some
test cases due to
incorrect
implementation of the
function. Your task is to
fix the code so that it
passes all the test cases
void
replaceElements(int
size, int *arr)
{
int i,j;
int sum=0;
for (i=0;i<size;i++)
{
sum+=arr[i];
}
if(size % 2 ==
0)//error in this line
{
i=0;
while(i<size)
{
arr[i] = 0;
i += 1;
}
}
else
{
j=1;
while(j<size)
{
arr[j]=1;
j+=1;
}
}
#include return 0;
}
int main()
char arr[20];
scanf("%s",arr);
len = strIn(arr);
for(x=0;x<len;x++)
for(y=x+1;y<len;y++)
if(arr[x]==arr[y]
&& flag == 0)
printf("%c",arr[y])
flag = 1;
break;
}
return 0;
1. Length }
2. Input number
3. Element
Output format:
Example:
Input:
8 12342562 2
Output:
int sum = 0;
if(arr[i]==value)
sum += value;
return sum;
return 0;
}
Two inputs One for the
number of times energy
was recorded as
‘total_reading’ and
second the reading of
the energy used or
produced ‘arr’
Output format:
Example:
Input:
1 4 -5 6 7 -4 4 -1
Output:
12
#include <stdio.h>
int main()
int total_reading, i,
arr[20];
cin>>total_reading;
for(i=0;
i<=total_reading; i++)
cin>>arr[i]);
int sum = 0;
for(i =
0;i<total_reading;i++){
sum+=arr[i];
cout<<sum;
return 0;
Output:
55
#include <stdio.h>
int main()
{
int val1 = 0, val2 = 1,
val3 = 1;
int number, i;
cin>>number);
for(i=2; i<=number;
i++)
val1 = val2;
val2 = val3;
cout<<&val3;
return 0;
if (flag == 1)
{
s = s + i;
}
}
function /method will return s;
return 8 i.e 1+3+4.
Function/method
compiles successfully
but fails to return the
desired result for
some/all cases due to
incorrect
implementation. Your
task is to debug the
code so that it passes all
test cases.
Example
Input
6 30 50
29 38 12 48 39 55
Output
Explanation :
if (flag == -1)
{
s = s + i;
}
}
return s;
}
Input
5435436789
Output
Explanation:
The repeated digits in
the data are 5,4 and 3.
So, the security key is 7
which is the length of
the string after
removing duplicates.
Output
19
Explanation
int numOfChem,
energy[100], i, j, result,
temp;
for (i = 0; i <
numOfChem; i++)
{
for (j = i + 1; j <
numOfChem + 1; j++)
{
if (energy[i] <=
energy[j])
{
temp =
energy[i];
energy[i] =
energy[j];
energy[j] =
temp;
}
}
}
result =
energy[numOfChem -
1] +
energy[numOfChem -
2];
return result;
}
9 -3 8 -6 -7 8 10
Output
19
Explanation
int numOfChem,
energy[100], i, j, result,
temp;
for (i = 0; i <
numOfChem; i++)
{
for (j = i + 1; j <
numOfChem + 1; j++)
{
if (energy[i] <=
energy[j])
{
temp =
energy[i];
energy[i] =
energy[j];
energy[j] =
temp;
}
}
}
result =
energy[numOfChem -
1] +
energy[numOfChem -
2];
return result;
}
23. Function/method int nthFib () https://prepinsta.com/
nthFib accepts single { dxc/automata-fix-que
argument-num, an int a = 0; stions-and-answers/#
integer representing int b = 1;
number. for example int c = 1;
num=9 int num, i;
Example
Input
Output
21
Explanation:
int nthFib ()
{
int a = 0;
int b = 1;
int c = 1;
int num, i;
Test Case 1:
Input:
23
1 3 5 7 9 11 13 15 17
19 21 23 25 27 29 31
33 35 37 39 41 43 45
Test Case 2:
Input:
14
0 2 4 6 8 10 12 14 16
18 20 22 24 26
int i, print = 0;
if (num / 2 == 0)
{
print = 0;
for (i = 0; i <=
num; i++)
{
cout<<print;
}
}
else
{
print = 1;
for (i = 0; i <=
num; i++)
{
cout<< print;
}
}
}
Test cases:
TestCase 1:
Input:
6 12 18 24 30 36 42
48 57 60 66 72 78 84
90 96 102 108 114 120
TestCase 2:
Input:
0 0 0 0 0 0 0 0 0 0
0000000000
The function/Method
compiles successfully
but fails to return the
desired result for some
test cases due to
incorrect
implementation of the
function/method
sameOddElementCount
. Your task is to fix the
code so that it passes all
the test cases.
Test Case 1:
Input :
11
[1, 5, 5, 2, 2, 7,7, 8, 6
,6, 9, 10]
Test Case 1:
Input :
int
sameOddElementCount
(int size, int *inputList)
{
int i, count = 0;
for (i = 1; i < size - 1;
i++)
{
if ((inputList[i] % 2
== 0) && (inputList[i]
== inputList[i--]))
count++;
}
return count;
}
29. The function https://prepinsta.com/
binarySearch(int* dxc/automata-fix-que
arr,int len, int target) stions-and-answers/#
performs the binary
search algorithm to
look for an element
targetin the input array
arrof length len. In
case it is found the int binarySearch (int
function returns the *ar, int len, int target)
index of targetin arr {
and returns a-1 if not
found. int lo = 0, hi = len;
while (lo <= hi)
The function works {
seemingly well but int mid = (lo + hi) /
goes into an infinite 2;
loop for some test if (ar[mid] ==
cases. target)
{
Your task is to fix the return mid;
program so that it }
passes all the test cases. else
{
Note: If there is a Time if (ar[mid] <
limit exceeded error, it target)
can be due to an infinite {
loop. lo = mid;
}
Test Cases: else
{
TestCase 1: hi = mid - 1;
}
Input: }
}
[0 , 2 , 3 , 4 , 5, 6 , 7 , return -1;
8] , 8 , 4 }
Expected Return Value:
TestCase 2:
Input:
[2 , 3],2,3
return 0;
int right_side_sum(int
a[], int n, int idx) {
int sum = 0;
Problem: Find the for(int i = idx + 1; i <
index of the n; i++) {
equilibrium element in sum += a[i];
an array. An }
equilibrium element is return sum;
one where the sum of }
all elements on its left
equals the sum on its int
right. Example: For findEquilibriumIndex(i
array a[] = {1, 2, 3, 4, nt a[], int n) {
3, 3}, the equilibrium for(int i = 0; i < n;
element is 4 because 1 i++) {
+ 2 + 3 = 3 + 3. if(left_side_sum(a,
n, i) ==
Code Template: right_side_sum(a, n, i))
{
#include <iostream> return i;
}
using namespace std;
}
int left_side_sum(int return -1;
a[], int n, int idx); }
int right_side_sum(int
a[], int n, int idx);
int
findEquilibriumIndex(i
nt a[], int n);
int main() {
int a[10], n, i;
cin >> n;
for(i = 0; i < n; i++)
{
cin >> a[i];
}
int equiIndex =
findEquilibriumIndex(a
, n);
if(equiIndex != -1) {
cout <<
a[equiIndex];
}
return 0;
}
Hence overflow
condition is reached.
Now, we can check for
the conditions. When
Queue Full :(
REAR+1)%n =
(4+1)%5 = 0 FRONT is
also 0.Hence ( REAR +
1 ) %n is equal to
FRONT.
int i=0,j=0,temp=0;
for(i=0;i<len;i++)
{ for(j=i+1;j<len;j++)
if(arr[i]>arr[j])
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}}}
return arr;
int findMaxElement(int
len1, int* arr1, int len2,
int* arr2)
}
37. The function/method https://www.scribd.co
countElement returns There are a few syntax m/document/5139392
the number of elements errors in the given C++ 70/5-6075729976089
in the input list arr code: 379287
which are greater than
twice the input number 1. The for loop
K. The function/method has incorrect
countElement accepts syntax:
three arguments - size,
an integer representing ○ It
the size of the input list, should
numK, an integer use
representing the input semicol
number K and ons (;)
inputList, a list of instead
integers. The of
function/method comma
compiles s (,).
unsuccessfully due to
syntactical error. Your 2.The condition
task is to fix the code so inside the if
that it passes all the test statement does
cases. not correctly
// You can print the check for
values to stdout for numbers
debugging greater than
twice numK.
using namespace std;
○ It
int countElement(int should
size, int numK, int be
*inputList) inputLi
st[i] >
{ 2*
numK.
int i,count=0;
using namespace std;
for(int i=0,i<size,i++)
int countElement(int
{ size, int numK, int
*inputList)
if(inputList[i]>numK)
{
count+=1;
int count = 0;
}
for (int i = 0; i < size;
return count; i++) // Corrected loop
syntax
} {
if (inputList[i] > 2
* numK) // Fixed
condition
count++;
return count;
}
38. The function/method https://www.scribd.co
arrayReverse modifies m/document/5139392
the input list by 70/5-6075729976089
reversing its element using namespace std; 379287
The function/method
arrayReverse accepts void arrayReverse(int
two arguments - len, an len, int* arr) {
integer representing the int temp;
length of the list and for (int i = 0; i < len /
arr, list of integers 2; i++) { // Fix loop
representing the input condition
list, respectively. For temp = arr[i];
example, if the input arr[i] = arr[len - i -
list arr is {20 30 10 40 1]; // Corrected index
50}, the for swapping
function/method is arr[len - i - 1] =
supposed to print {50 temp;
40 10 30 20}. The }
function/method }
arrayReverse compiles
successfully but fails to
get the desired result
for some test cases due
to logical errors. Your
task is to fix the code so
that it passes all the test
cases.
void arrayReverse(int
len, int* arr)
int i, temp,
originalLen=len;
for(i=0;i<=originalLen/
2;i++)
temp = arr[len-1];
arr[len-1] = arr[i];
arr[i] = temp;
len =len-1;
return 0;
}
Explanation:
○ In C++,
you
cannot
assign
one
charact
er array
to
another
using
=.
○ Strings
in C++
are
stored
as
arrays,
and
arrays
do not
support
assign
ment.
○ Solutio
n: Use
strcpy()
from
<cstrin
g> to
copy
string
content
s.
cout<<*ptr; return 0;
}
return 0;
cout<<factorial(5); Explanation:
return 0; Incorrect Recursive
Call: factorial(n--)
}
● Problem: n--
passes n first,
then
decrements it
after the
function call.
● This leads to
infinite
recursion and
stack overflow.
● Solution: Use
factorial(n - 1)
instead.
};
int main() {
Stack s;
s.push(10);
s.push(20);
s.push(30);
s.display();
return 0;
}
45. Fix Logical Error in #include <iostream> https://www.hackerea
Stack Using STL #include <stack> rth.com/practice/note
using namespace std; s/stacks-and-queues/
#include <iostream>
int main() {
#include <stack> stack<int> s;
s.push(5);
using namespace std; s.push(10);
s.push(15);
int main() {
cout << "Top
stack<int> s; element: " << s.top()
<< endl;
s.push(5);
// Pop elements only
s.push(10); if the stack is not empty
while (!s.empty()) {
s.push(15); s.pop();
}
cout << "Top
element: " << s.top()
if (!s.empty()) {
<< endl;
cout << "Stack is
not empty\n";
s.pop();
cout << "Top
element: " << s.top()
s.pop();
<< endl; // Safe to
s.pop(); access top
} else {
s.pop(); // Error: cout << "Stack is
Extra pop() call empty\n";
}
if (!s.empty())
return 0;
cout << "Stack is }
not empty\n";
else
return 0;
}
Expected Output:
Top element: 15
Stack is empty
}
return str; // Error:
Returning string in void
function
int main() {
string input =
"ENGINEER";
cout <<
reverseString(input); //
Error: Calling void
function in cout
return 0;
Expected Output:
Reversed
string:REENIGNE
void display() {
if (front == -1) {
return;
};
int main() {
CircularQueue cq;
cq.enqueue(10);
cq.enqueue(20);
cq.enqueue(30);
cq.enqueue(40);
cq.enqueue(50); //
Should show "Queue
Overflow"
cq.display();
return 0;
Expected Output:
Queue Overflow
Dequeued: 10
20 30 40 50
q.pop();
q.pop();
q.pop(); // Error:
Extra pop() call
if (!q.empty())
else
return 0;
Expected Output:
Front element: 5
Queue is empty
49. #include <iostream>
Problem Statement: using namespace std;
void void
insertAtEnd(int val) { deleteNode(int val) {
if (head ==
Node* NULL) {
newNode = new cout << "List
Node(val); is empty\n";
return;
}
if (head == Node* temp =
NULL) head;
Node* prev =
head = NULL;
newNode;
if (temp->data
else { == val) {
head =
Node* temp temp->next; // Fix:
= head; Assignment instead of
comparison
while delete temp;
(temp->next != NULL) return;
}
temp =
temp->next; while (temp !=
NULL && temp->data
temp->next = != val) {
newNode; prev = temp;
temp =
} temp->next;
}
}
if (temp ==
void deleteNode(int
NULL) {
val) {
cout <<
"Element not found\n";
if (head ==
return;
NULL) {
}
cout << "List
prev->next =
is empty\n";
temp->next; // Fix:
return; Assignment instead of
comparison
} delete temp;
}
Node* temp =
head; void display() {
Node* temp =
Node* prev = head;
NULL; while (temp !=
NULL) {
if (temp->data cout <<
== val) { temp->data << " ";
temp =
head == temp->next;
temp->next; // Error: }
Should be assignment, cout << endl;
not comparison }
};
delete temp;
int main() {
return; LinkedList list;
list.insertAtEnd(10);
} list.insertAtEnd(20);
list.insertAtEnd(30);
while (temp != list.deleteNode(20);
NULL && temp->data list.display();
!= val) { return 0;
}
prev = temp;
temp =
temp->next;
if (temp ==
NULL) {
cout <<
"Element not found\n";
return;
prev->next ==
temp->next; // Error:
Should be assignment,
not comparison
delete temp;
void display() {
Node* temp =
head;
while (temp !=
NULL) {
cout <<
temp->data << " ";
temp =
temp->next;
};
int main() {
LinkedList list;
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.deleteNode(20);
list.display();
return 0;
Expected Output:
10 30
return s.top();
int main() {
string expr =
"ab+c*";
cout << "Infix
Expression: " <<
postfixToInfix(expr) <<
endl;
return 0;
Expected Output:
Infix Expression:
((a+b)*c)