Join Regular Classroom : Visit ClassroomTech

Problem 31:
Input:
5
Expected output:
a
ab
abc
abcd
abcde
testcase 2:
input:
1
Output:
A

#include
void printcharecterpattern( int num)
{
int i,j,value=1;
char ch = 'a';
char print = ch;
for(int i=1;i<=num;i++,printf("n"))
{
ch=print;
for(j=1;j<=i;j++)
printf("%c",ch++);
}
}
int main()
{
int num;
scanf("%d",&num);
printcharecterpattern(num);
}

Solution in C

#include<stdio.h>
void printcharecterpattern( int num)
{
int i,j,value=1;
char ch = ‘a’;
char print = ch;
for(i=1;i<=num;i++,printf(“n”))
{
ch=print;
for(j=1;j<=i;j++)
printf(“%c”,ch++);
}
}

Problem 32:
You are required to fix all logical errors in the given code. You can click on Compile ,Run anytime to check the compilation/execution status of the program. You can use System.out.println 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 notexpect you to modify the approach or incorporate any additional library methods. The method printColor(intnum) of the class Color is supposed to print names of color according to giveninput numbers num When the values of num equal 1,2,3,4 the function prints “Red”, ”Black”, ”White”, ”Green” respectively for any other values of num it should print “No color”. The method complies fine but fails to return the desired results for some cases. Your task is to fix code so that it passes all the testcases.

int printcolor(int num)
{
switch (num)
{
case 1:
printf("red");
case 2:
printf("black");
case 3:
printf("white");
case 4:
printf("green");
default:
printf("no color");
break;
}
}

Solution in C

int printcolor(int num)
{
switch (num)
{
case 1:
printf("red");
case 2:
printf("black");
case 3:
printf("wint printcolor(int num)
{
switch (num)
{
case 1:
printf(“red”);
break;
case 2:
printf(“black”);
break;
case 3:
printf(“white”);
break;
case 4:
printf(“green”);
break;
default:
printf(“no color”);
break;
}
}hite");
case 4:
printf("green");
default:
printf("no color");
break;
}
}

Problem 33:
The method printpattern(int) of class drawpattern is expected to expected to print the first n (n> 0) Lines of the pattern TESTCASES
TestCase 1
Input:
4
Expected Return value:
11
1111
111111
11111111
TestCase 2:
Input:
1
Expected Return Value:
11
Given Code:

int printpattern(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”);
}

Solution in C

int printpattern(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”);
}
}

Problem 34:
Multiply the middle number with maximum of three numbers.

TESTCASE 1
Input
5,7,4
Expected return value:
35

TESTCASE 2
Input
11,12,13
Expected return value:
156

Given Code:

#include
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<b)?((a<c)?a:c((b<c)?b:c);
mid=(a+b+c)-(min+max);
result=(max*mid);
return result;
}

Solution in C

#include<stdio.h>
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<b)?((a<c)?a:c):((b<c)?b:c);
//max= (a>b) ? ((a>c)?a:c) : ((b>c)?b:c);
//min= (a<b) ? ((a<c)?a:c) : ((b<c)?b:c);
mid=(a+b+c)-(min+max);
result=(max* mid);
return result;
}

Problem 35:
You are required to fix all logical errors in the given code. You can click on Compile & Run anytime to check the compilation/execution status of the program. You can use System.out.println to debug your code. The submitted code should be logically/syntactically correct and pass all test cases. 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
sortArray(int * arr,intlen) accepts an integer array arr of length (len>0) as an input and perform an in place sort operation on it. The function is expected to return the input array
sorted in descending order The function compiles 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
TESTCASE 1:
Input:
[23, 12, 14, 24, 21], 5
Expected Return Value:
[24, 23, 21, 14, 12]
TESTCASE 2:
Input:
[1, 1, 1, 1, 1], 5
Expected Return Value:
[1, 1, 1, 1, 1]

Given Code:

int *sortArray(int *arr,int *len)
{
int i=0,j=0,temp=0,index=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;
}
}
}

Solution in C

int *sortArray(int *arr, int len)
{
int i=0,j=0,temp=0,index=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;
}

Problem 36:
You are required to complete the given code by reusing existing functions. click on the helper code tab to find out the details of functions/classes provided for reuse you can click on compile & run anytime to check the compilation /execution status of the program you can use System.out.println to debug your code The submitted code should be logically/syntactically
correct and pass all testcase . . 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 countElement(int *arr, int len, int n) is supposed to return the numbers of elements in the inputs array arr of length len, which are greater than twice of the input number n The function looks fine but given a compilation error Your task is to fix the program so that it passes all the testcases.

TESTCASE 1:
Input:
[-2, -4, -3, -5, -6, -7, -8], 7, 3
Expected Return Value:
0

TESTCASE 2:
Input:
[22, 55, 66, 33, 44, 77], 6,13
Expected Return Value:
5
PROGRAM:
Given Code:

int countElement(int arr, int len, int n)
{
inti,count=0;
for(int i=0;i<len;i++)
{
if(arr[i]>2n)
{
count=-1;
}
}
return count;
}

Solution in C

int countElement(int arr, int len, int n)
{
int i,count=0;
for(i=0;i<len;i++)
{
if(arr[i]>2*n)
{
//count=-1;
count+=1;
}
}
return count;
}

Problem 37:
The method countdigit(int sum) of class digits is supposed to return the value remainder when the input arguments num(num>0) is divided by the number of digits in num.
Given Code:

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

Solution in C

countdigit(int sum)
{
int count=0,safe;
safe=num;
while(num)
{
num=num/10;
count++;
}
num=safe;
return (num%count);
}

Problem 38:
The Function reverseArray(intarr[])of class sort Array arr of an arguments For example, if the input array arr is {20,30,10,40,50} the function is expected to return{50,40,10,30,20}. The function compiles successfully but fails to return the desired result due to logical errors.
Given Code:

int arrayReverse(int *arr,int len)
{
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+=1;
}
return arr;
}

Solution in C

int arrayReverse(int *arr,int len)
{
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+=1;
len-=1;
}
return arr;
}

Problem 39:
Given Code:

char checkGrade(int score)
{
if(score<=60)
return 'D';
else if((61<=score)&&(score<=75))
return 'C';
else if((76<=score)&&(score<=90))
return 'B';
else
return 'A';
}
int main()
{
int score;
scanf(“%d”,&score);
printf(“%c”, checkGrade(score));
return 0;
}

Solution in C

char checkGrade(int score)
{
if(score<=60)
return ‘D’;
else if((61>=score)&&(score<=75))
return ‘C’;
else if((76>=score)&&(score<=90))
return ‘B’;
else
return ‘A’;
}
int main()
{
int score;
scanf(“%d”,&score);
printf(“%c”, checkGrade(score));
return 0;
}

Problem 40:
The function getarraysum(int * arr, int len)is supported to calculate and return the sum of elements of the input array arr of length len(len>0) The function compiles successfully but fails to return the desired result due to logical errors.
Given Code:

int getarraysum(int *arr, int len)
{
int sum = 0;
for(i=0;i<len;i-i-1)
{
sum = arr[i];
}
return sum;
}

Solution in C

int getarraysum(int *arr, int len)
{
int sum = 0;
for(i=0;i<len;i++)
{
sum+ = arr[i];
}
return sum;
}
Categories
Pages
Recent Posts