Cts Auto Fix Set 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Question:1

The function calculateMatrixSum(int ** matrix, int m,


int n) accepts a two dimensional array matrix of
dimensions m, n as input and returns the sum of
odd elements whose ith and jth index are same.
The function compiles line but falls to return the
desired result for some test cases.
PROGRAM
int calculateMatrixSum(int **matrix, int m, int n)
{
int i,j,sum=0,row=m, column=n;
if((row>0)&&(column>0))
{
for(i=0;i<row;i++)
{
sum=0;
for(j=0;j<column;j++)
{
if(i==j)
{
if(matrix[i][j]/2!=0)
sum+=matrix[i][j];
}
}
}
return sum;
}

Question:2
The method manchester (int arr) of class signal
accepts an array arr 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,
iIf 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 if 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 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;
}
Question:3
int matrixsum( int &matrix, int m, int n )
{
int i,j,sum=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
sum + =matrix(i)(j);
}
return sum;
}

Question:4
TESTCASE 1:
Input:
[2, 5, 8, 11, 3], 5
Expected Return Value:
[11, 11, 11, 11, 11]

TESTCASE 2:
Input:
[3, 2, 5, 8, 9, 11, 23, 45, 63], 9
Expected Return Value:
[63, 63, 63, 63, 63, 63, 63, 63, 63]

int* maxReplace(int *arr, int len)


{
int i;
if(len>0)
{
int max=arr[0];
for(i=0;i<len;i++)
{
if(max<arr[i])
max=arr[i];
}
}

return arr;
}
Question:5
This method countOccurance (int arr[], int value) of
class occurrence is supposed to return the count of
occurrences of a number value in the input array arr
. 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 test cases.
PROGRAM
public class Occurrence{
public static int Occurrence (int arr[], int value)
{
int i=0, count=0, len=arr.length;
while(i<len){
if(arr[i]==value)
count++;
}
return count;
}
}
Question:6
The function patternPrint(int n) supposed
to print n numberof lines in the following pattern
For n=4 the pattern should be:
1
11
111
1111
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 testcases
Program
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”);
}
}
17(1) TESTCASE 1
INPUT 1
3
EXPECTED OUTPUT VALUES
1
11
111
INPUT 2
1
EXPECTED OUTPUT VALUES
1

Question:7
The function removeElement( int *arr,int len,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

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++)
// printf("%d ",arr[i]);
// printf("\n");
rarr[i]=arr[i];
return rarr;
}
else return arr;
}

Question:8
The function replaceValues(int *arr, int len)
accepts an array arr of length len (len>0)
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 1 and in case it is even,
the elements should be replaced
by 0
PROGRAM:
int *replaceValues( int arr[],int len){
int i,j;
if(len%2==0){
for(i=0;i<=len;i=i+2)
arr[i]=0;
}
else{
for(j=0;j<=len;j=j+2)
arr[j]=1;
}
return arr;
}
}

Question:9
The function sameelementcount(int *arr,int len)accepts an
integer
array arr of length len as a input and returns the number of
elements in
an arr which are even numbers and equal to the element to its
right
int sameelementcount(int * arr, int len)
{
Int i,count = 0;
For(i=0;i<len-1;i++)
{
if((arr[i]%2==0)&&(arr[i]==arr[i++]))
count++;
}
return count;
}
}
Question:10

You might also like