Problem 21:
The function findMaxElement(int *arr1,int len1,int *arr2,int len2) accepts twointeger arrays arr1,arr2 of length len1,len2 respectively.It is supposed to return the largest element in both the input arrays. Another function sortArray(int *arr,int len) sorts the input array arr of length lenin ascending order and returns the sorted array. Your task is to use sortArray(int *arr,int len) function and complete the codein findMaxElement(int *arr1,int len1,int *arr2,int len2) so that it passes all testcases.
int *sortArray(int len,int *arr)
{
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)
{
//write code here
}
Solution in C
int *sortArray(int len,int *arr)
{
int i=0,j=0,temp=0; f
or(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)
{
int sort_array1[len1]=sortArray(int len1,int *arr1);
int sort_array2[len2]=sortArray(int len2,int *arr2);
if(sort_array1[len1-1]>sort_array2[len2-1])
{
printf(“%d”,sort_array1[len1-1]);
}
else
{
printf(“%d”,sort_array2[len2-1]);
}
}
Problem 22:
The function allExponent(int base,int exponent)accepts two integers base and exponent as inputs.it is supposed to calculate and return of exponentation of base raised to power exponent for all input values. The function positiveExponent(int base,int exponent) will return the exponentiation value if exponent value is positive. try to complete the code such that allExponent should return value if exponent value is negative.
float allExponent( int base,int exponent )
{
float res=1;
if(exponent >=0)
{
Res = (float)positiveExponent(base,exponent)
}
Else
{
//write ur code here
}
return res;
}
Solution in C
float allExponent( int base,int exponent )
{
float res=1;
if(exponent >=0)
{
res = (float)positiveExponent(base,exponent);
}
else
{
//write ur code here
res = 1/((float)positiveExponent(base,-(exponent)));
}
return res;
}
int positiveExponent(int base,int exponent)
{
int sol=pow(base,exponent);
return sol;
}
Problem 23:
You are given a predefined structure Point and related functions. The function/method isTriangle which accepts three points P1,P2,P3 as inputs and checks whether the given three points form a triangle. If they form a triangle the function return 1 else 0.
typedef struct point
{
int X; int Y;
}Point;
double Point_calculateDistance(Point *point1,Point *point2)
{
Point_calculateDistance(P1,P2);
}
isTriangle(Point *P1,Point *P2,Point *P3)
{
//write code
}
Solution in C
int isTriangle(Point *P1,Point *P2,Point *P3)
{
int a=P1.X*(P2.Y-P3.Y)+P2.X*(P3.Y-P1.Y)+P3.X*(P1.Y-P2.Y);
if(a==0)
return 0;
else
return 1;
}
Problem 24:
Charlie has a magic mirror. The mirror shows right rotated versions of a given word. To generate different right-rotations of a word, write the word in a circle in clockwise order, then start reading from any given character in clockwise order till you have covered all the characters.
For example: In the word “sample”, if we start with ‘p’, we get the right rotated word as “plesam”. There are six such right rotations of “sample” including itself. The inputs to the function isSameRef 鈥宭 ection consists of two strings, word1 and word2. The function returns 1 if word1 and word2 are right rotations of the same word and -1 if they are not. Both word 1 and word2 will strictly contain characters between ‘a’-‘z’ (lower case letters). –>Useful commands:
strlen() is used to calculate the length of the string. The statement -int len = strlen(str)
returns the lenght of the srting str.
using namespace std;
int isSameReflection(char *word1,char *word2)
{
//write code here
int size1=strlen(word1);
int size2=strlen(word2);
char *temp;
void *ptr;
if(size1!=size2)
return -1;
temp[0]='';
strcat(temp,word1);//concat empty string with word1(sample)
strcat(temp,word1);//concat sample along with sample again(samplesample)
//temp=samplesample
word2=zlesam
ptr=strstr(temp,word2);//word2 is there in temp and return index of first char of word2 in temp, if word2 is not a substring it will return null.
if(ptr!=null)
return 1;
else
return -1;
}
Solution in C
Problem 25:
You are given a predefined structure Point and related functions. The function/method isRightTriangle which accepts three points P1,P2,P3 as inputs and checks whether the given three points form a right angled triangle. If they form a right triangle the function return 1 else 0
typedef struct point
{
int X; int Y;
}Point;
double Point_calculateDistance(Point *point1,Point *point2)
{
//returns the distance between pont 1, point2.It can be called as
Point_calculateDistance(P1,P2);
}
Solution in C
int isRightTriangle(Point *P1,Point *P2,Point *P3)
{
double d1,d2,d3;
d1=Point_calculateDistance(P1,P2);
d2=Point_calculateDistance(P2,P3);
d3=Point_calculateDistance(P3,P1);
if(pow(d1,2)+pow(d2,2)==pow(d3,2)|| pow(d2,2)+pow(d3,2)==pow(d1,2)|| pow(d3,2)+pow(d1,2)==pow(d2,2))
{
return 1;
}
else
return 0;
}
Problem 26:
The function/method median accepts two arguments-size and inputList,an integer representing the length of a list and a list of integers respectively. It is supposed to calculate and return an integer representing the median of elements in the input list. However the function/method median works only for odd-length lists because of incomplete code.
you must complete the code for even-length as well.
int quick_select(int*inputList,int start_index,int end_index,int median_order)
{
//It calculate the median value
}
Solution in C
float median(int size,int *inputList)
{
int start_index=0;
int end_index=size-1;
float res=-1;
if(size%2!=0)
{
int med_1=size/2;
int med_2=(size/2)+1;
res=((float)quick_select(inputList,start_index,end_index,med_1));
}
else
{
//enter your code here
res=((float)quick_select(inputList,start_index,end_index,med_1)+(float)quick_select(inputList,star t_index,end_index,med_2))/2;
}
return res;
}
Compilation error:
Problem 27:
The function/method matrixSum returns an integer representing the sum of elements of the input matrix. The function/method matrixSum accepts three arguments- rows, an integer representing the number of rows ,columns an integer representing number of columns, matrix is going to be the input matrix. The function/method matrixSum has some compilation error. Rectify the error and get the desired output:
int matrixSum(int rows,int columns, int **matrix)
{
int i,j,sum=0;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++){
sum+=matrix(i)(j);
}
}
return sum;
}
Solution in C
int matrixSum(int rows,int columns, int **matrix)
{
int i,j,sum=0;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++){
sum+=matrix[i][j];
}
}
return sum;
}
Problem 28:
The function/method countElement return an integer representing the number of elements in the input list which are greater than twice the input number K. The function/method accepts three arguments-size, an integer representing the size of input list, numK an integer representing the input number K and inputList, a list of integers representing the input list, respectively.
The function/method countElement compiles unsuccessfully due to systematical error. Fix the code so that it passes all test cases.
int countElement(int size,int num,int *inputList)
{
int i,co-unt=0;
for(i=0;i<size;i++)
{
if(inputList[i]>2numK)
{
co-unt+=1;
}
}
return co-unt;
}
Solution in C
int countElement(int size,int num,int *inputList)
{
int i,count=0;
for(i=0;i<size;i++)
{
if(inputList[i]>2*numK)
{
count+=1;
}
}
return count;
}
Problem 29:
The function/method deleteNonRepeat accpets two arguments-size, an integer representing the size of the list and inputList, representing the list of integers. It removes the non-repeated integres from the inputList and prints the remaining repeated integers seperated by space from inputList. If all the elements are unique then the function should not print anything.
For example, for the given inputList{2,3,2,2,5,6,6,7}, th expected output is 2 2 2 6 6 . It compiles unsuccessfully due to syntactical error. Debug the code.
void deleteNonRepeat(int size,int *inputList)
{
int count=0,i,j;
int counter[size];
for(i=0;i<size;i++)
{
counter[i]=1;
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(inputList[i]==inputList[j])
{
counter[i]++;
counter[j]++;
}
}
}
for(i=0;i<size;i++)
if(counter[i]>1)
count++;
j=0;
for(i=0;i<size;i++)
if(counter[i]>1)
printf("%d",inputList[]);
}
Solution in C
void deleteNonRepeat(int size,int *inputList)
{
int count=0,i,j;
int counter[size];
for(i=0;i<size;i++)
{
counter[i]=1;
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(inputList[i]==inputList[j])
{
counter[i]++;
counter[j]++;
}
}
}
for(i=0;i<size;i++)
if(counter[i]>1)
count++;
j=0;
for(i=0;i<size;i++)
if(counter[i]>1)
printf(“%d”,inputList[i]);
}
Problem 30:
The function/method multiplyNumber returns an integer representing the multiplicative product of the maximum two of three numbers. It accepts three integers- numA, numB, and NumC, representing the input numbers. It compiles unsuccessfully due to syntactical error. Debug the code.
int multiplyNumber(int numA,int numB,int numC)
{
int result,min,max,mid;
max=(numA>numB)?numA>numC)?numA:numc):((numB>numC)?numB:numC);
min=(numA<numB)?((numA<numC)?numA:numc):((numB<numC)?numB:numC);
mid=(numA+numB+numC)-(min+max);
result=(max*int mid);
return result;
}
Solution in C
int multiplyNumber(int numA,int numB,int numC)
{
int result,min,max,mid;
max=(numA>numB)?((numA>numC)?numA:numc):((numB>numC)?numB:numC);
min=(numA<numB)?((numA<numC)?numA:numc):((numB<numC)?numB:numC);
mid=(numA+numB+numC)-(min+max);
result=(max* mid);
return result;
}