Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

CTS Set 2 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

CTS SET 2

1. 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 canuse printf
to debug your code. The submitted code should be logically/syntatically 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 function ​multiplyNumber(int a, int b, int c)​ accepts three integers a, b and c as
inputs. It is supposed to return the multiplicative product of the maximum two of three
input numbers.
The function looks fine but gives a compilation error.
Your task is to fix the program so that it passes all the test cases.

int multiplyNumber(int a, int b, int c){


int result,min,max,mid;
max = (a>b)?((a>c)?a:c):((b>c)?b:c);
min = (a>c)?((a<c)?a:c):((b<c)?b:c);
mid = (a+b+c) - (min + max);
result = (max * mid);
return result;
}

2. 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 canuse printf
to debug your code. The submitted code should be logically/syntatically 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 function ​drawPrintPattern(int n) ​is expected to print the first n (n >= 0) lines of the
pattern shown below.
For example, if n = 3, the pattern should be:
11
1111
111111

The function compiles successfully but fails to print the desired pattern due to logical
errors.
Your task is to debug the program to pass all the test cases.
void drawPattern(int n){
int i,j,print = 1;
for(i = 1;i<=n;i++){
for(j=1;j<=2*i;j++)
printf("%d",print);
printf("\n");
}
}

3. 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 canuse printf
to debug your code. The submitted code should be logically/syntatically 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 function ​sameElementCount(int *arr, int len)​ accepts an integer array arr of
length len as input and returns the number of elements in arr that are even numbers and
equal to the elements to its right.
Note: In an array, element at index i is considered to be on the left of index and to the
right of index i-1. The last element in arr does not have any element next to it, which
makes it incapable to satisfying the second condition and hence should not be counted.
The function compiles fine but fails to return the desired result for some test cases.

Your task is to fix the program so that is passes all the test cases.

int sameElementCount(int *arr,int len){


int i,count =0,j;
for(i=0;i<len-1;i++){
j=i;
if((arr[i]%2 == 0) && (arr[i] == arr[++j]))
count++;
}
return count;
}
4. 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 canuse printf
to debug your code. The submitted code should be logically/syntatically 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 function ​manchester(int *arr, int len)​ accepts an array arr of length len (len >= 0)
as an input. Each element of arr represents a bit -0 or 1. The output is an array with the
following property. For each element in the input array arr, if the bit arr[i] is the same as
arr[i-1], then the element of the output array is 0. If they are different, then its 1. For the
first bit in the input array, assume its previous bit to be 0. This encoding is stored and
returned in a new array.

For e.g. is arr is{0,1,0,0,1,1,1,0}, the function should return an array{0,1,1,0,1,0,0,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.

int* manchester(int *arr, int len){


int* res = (int*)malloc(sizeof(int)*len);
for(int i=0;i<len;i++)
res[i]=!(arr[i] == arr[i-1]);
return res;
}

5. 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 canuse printf
to debug your code. The submitted code should be logically/syntatically 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 function ​countOccurence(int *arr, int len int value) ​ is supposed to return the
count of occurence of a number value in the input array arr of length len(len >= 0)
The functino 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.
int countOccurence(int *arr, int len, int value){
int i=0,count=0;
while(i<len){
if(arr[i++] == value)
count+=1;
}
return count;
}

You might also like