Join Regular Classroom : Visit ClassroomTech

Problem 1 :

The function/method count Digits return an integer representing the remainder when the given  number is divided by the number of digits in it.
The function/method count Digits accepts an argument-num,an integer representing the given  number.
The function/method countDigits compiles code successfully but fails to get the desirable result  for some test cases due to logical errors. Your task is to fix the code so that it passes all the test cases.
Test case 1:
Input:  782
Expected return value: 2 
Test case 2:
Input:  21340 
Expected return value: 0

int countDigits(int num)
{
int count=0; while(num!=0)
{
num=num/10; count++;
}
return (num%count);
}

Solution in C

int countDigits(int num)
{
int count=0,temp=num;
while(temp!=0)
{
temp=temp/10;
count++;
}
return (num%count);
}

Problem 2 :

The function/method printCharacterPattern accepts an integer num. It is supposed to print the first num(0<=num<=26) lines of the pattern as shown below:
For example,if num=4, the pattern is
a
ab
abc
abcd

The function/method printCharacterPattern compiles successfully but fails to get the desired output. Fix the logical errors.

void printCharacterPattern(int num)
{
int i,j;
char ch='a';
char print;
for(i=0;i<num;i++)
{
print=ch;
for(j=0;j<=i;j++)
{
printf("%c",ch++);
}
printf("\n");
}
}

Solution in C

void printCharacterPattern(int num)
{
int i,j;
char ch='a';
char print;
for(i=0;i<num;i++)
{
print=ch;
for(j=0;j<=i;j++)
{
printf("%c",ch++);
}
printf("\n");
}
}

Probelm 3:

The function/method removeElement prints space separated integer that remains after removing the integer at the given index from the input list.

The function/method removeElement accepts three arguments, size an integer representing the sizeof input list, indexValue an integer representing given index, and inputList, a list of integers representing the input list.

The function/method removeElement compiles code successfully but fails to get the desired result for some test cases due to incorrect implementation of function/method removeElement. Your task is to fix the code so that it passes all the test cases.
Note: zero based indexing is followed to access list elements
Test case 1:
Status: wrong Expected:
1 2 3 5 6 7 8 9

Returned
1 2 3 4 4 6 6 8

Test case 2:

Status: correct Expected:
11 23 12 34 54 32

Returned
11 23 12 34 54 32

void removeElement(int size,int indexValue,int *inputList)
{
int i,j;
if(indexValue<size)
{
for(i=indexValue;i<size;i++)
{
inputList[i]=inputList[i++];
}
for(i=0;i<size-1;i++)
{
printf("%d",inputList[i]);
}
}
else
{
for(i=0;i<size;i++)
{
printf("%d",inputList[i]);
}

Solution in C

void removeElement(int size,int indexValue,int *inputList)
{
int i,j;
if(indexValue<size)
{
for(i=indexValue;i<size;i++)
{
inputList[i]=inputList[i+1];//1 2 3 4 5 6 7 8 9
}
for(i=0;i<size-1;i++)
{
printf("%d",inputList[i]);
}
}
else
{
for(i=0;i<size;i++)
{
printf("%d",inputList[i]);
}
}
}

Probelm 4:

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 successfully but fails to return the desired result for some test cases.

int calculateMatrixSum(int rows,int columns,int ** matrix))
{
int i,j,sum=0;
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;
}
else
return sum;
}

Solution in C

int calculateMatrixSum(int rows,int columns,int ** matrix))
{
int i,j,sum=0;
if((row>0)&&(column>0))
{
for(i=0;i<row;i++)
{
for(j=0;j<column;j++){
if(i==j)
{
if(matrix[i][j]%2!=0)
sum+=matrix[i][j];
}
}
}
return sum;
}
else
return sum;
}

Problem 5:

The function/method sortArray modify the input list by sorting its elements in descending order. It accepts two arguments-len,representing the length of the list and arr, a list of integers representing the input list respectively.
It compiles successfully but fails for some test case. Fix the code.

void sortArray(int len,int *arr)
{
int i,max,location,j,temp;
for(i=0;i<len;i++)
{
max=arr[i];
location=i;
for(j=i;j<len;j++)
{
if(max>arr[j])
{
max=arr[j];
location=j;
}
}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
}

Solution in C

void sortArray(int len,int *arr)
{
int i,max,location,j,temp;
for(i=0;i<len;i++)
{
max=arr[i];
location=i;
for(j=i;j<len;j++)
{
if(max<arr[j])
{
max=arr[j];
location=j;
}
}
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
for(i=0;i<len;i++)
{
printf("%d ",arr[i]);
}
}

Problem 6:

The function/method replace Values is modifying the input list in such a way – if the length of the input list is odd, then all the elements of the input list 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 list[0,1,2], the function will modify the input list like [1 1 1].It accepts two arguments size, an integer representing the size of input listand inputlist , a list of integers.
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.

void replaceValues(int size,int *inputList)
{
int i,j;
if(size%2==0)
{
i=0;
while(i<size)
{
inputList[i]=0;
i+=2;
}
}
else
{
j=0;
while(j<size)
{
inputList[j]=1;
j+=2;
}
}
}

Solution in C

void replaceValues(int size,int *inputList)
{
int i,j;
if(size%2==0)
{
i=0;
while(i<size)
{
inputList[i]=0;
i+=1;
}
}
else
{
j=0;
while(j<size)
{
inputList[j]=1;
j+=1;
}
}
}

Problem 7:

The function productMatrix 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 fails to return the desired result for some test cases.

int productMatrix(int rows,int columns,int **matrix)
{
int result=0;
for(i=0;i<row;i++)
{
for(j=0;j<column;j++){
if((i==j)||matrix[i][j]%2!=0)
result*=matrix[i][j];
}
}
if(result<=1)
return 0;
else
return result;
}

Solution in C

int productMatrix(int rows,int columns,int **matrix)
{
int result=0,flag=0;
for(i=0;i<row;i++)
{
for(j=0;j<column;j++){
if((i==j) && matrix[i][j]%2!=0)
{
result*=matrix[i][j];
flag=1;
}
}
}
if(flag==0)
return 0;
else
return result;
}

Problem 8:

The function maxReplace is supposed to replace every element of the input array arr with the maximum element of arr.
The function complies unsuccessfullydue to compilation errors. Your task is to debug the program to pass all the test cases.

void maxReplace(int size,int &inputList)
{
int i; if(size>0)
{
int max=inputList[0];
for(i=0;i<size;i++)
{
if(max<inputList[i])
{
max=inputList[i];
}
}
for(i=0;i<size,i++)
{
inputList[i]=max
printf("%d",inputList[i]);
}
}

Solution in C

void maxReplace(int size,int *inputList)
{
int i,max; if(size>0)
{
max=inputList[0];
for(i=0;i<size;i++)
{
if(max<inputList[i])
{
max=inputList[i];
}
}
for(i=0;i<size;i++)
{
inputList[i]=max;
printf("%d",inputList[i]);
}
}

Problem 9:

The function/method sumElement return an integer representing the sum of the elements in the input array that are greater than twice the input number K and present at the even index. It accepts three aguments-size of array, numK representing input number and inputarray list of integers. It compiles successfully but fails to return the desirable result.

int sumElement(int size, int numK,int *inputArray)
{
int i,sum=0;
for(i=0;i<size;i++)
{
if(inputArray[i]>2*numK && i/2==0)
{
sum=inputArray[i];
}
}
return sum;
}

Solution in C

int sumElement(int size, int numK,int *inputArray)
{
int i,sum=0;  for(i=0;i<size;i++)
{
if(inputArray[i]>2*numK && i%2==0)
{
sum+=inputArray[i];
}
}
return sum;
}

Problem 10:

The function/method manchester: for each element in the input array arr, a counter is incremented if the bit arr[i] is same as arr[i-1]. Then the increment counter value is added to the output array to store the result.

If the bit arr[i] and arr[i-1] are different, then 0 is added to output array. for the first bit in the input array, assume its previous bit to be 0.
For example if arr is {0,1,0,0,1,1,1,0}, then it should print 1 0 0 2 0 3 4 0. It accepts two arguments-size and arr-list of integers. Each element represents a bit 0 or 1. It compiles successfully but fails to print the desirable result. Fix the code.

void manchester(int size,int *arr)
{
bool result;
int *res=(int *)malloc(sizeof(int)*size);
int count=0;
for(int i=0;i<size;i++)
{
if(i==0)
result=(arr[i]==0);
else
result=(arr[i]==arr[i-1]);
res[i]=(result)?0:(++count);
}
for(int i=0;i<size;i++)
{
printf("%d",res[i]);
}
}

Solution in C

void manchester(int size,int *arr)
{
bool result;  int res[size];  int count=0;
for(int i=0;i<size;i++)
{
if(i==0)
result=(arr[i]==1);
else
result=!(arr[i]==arr[i-1]);
res[i]=(result)?0:(++count);
}
for(int i=0;i<size;i++)
{
printf("%d ",res[i]);
}
}
Recent Posts