Following code finds the greatest integer less than log_m y given m, y, m notequ
ID: 2246814 • Letter: F
Question
Following code finds the greatest integer less than log_m y given m, y, m notequalto 1 the floor of log_m y. For example, y=30 and m=2 should output 4. Please fill in the blanks to complete the program. int log_count (int y, int m) { ______ counter = 0: if _____ ! = 0) counter = counter + 1: return counter: } int main () { int y, m: scanf (" %d %d", &y;, &m;): int old_counter = ____: int counter = log_counter (y, m): while (counter ! = old_counter) { old_counter = counter: y = _______: counter = log_ counter (y, m): } printf("%d", counter): return 0: }Explanation / Answer
//Completed code:-
#include <stdio.h>
int log_counter(int y, int m){
if(y<m) return 0;static int counter=0; /* Fill */
if ((y/m) !=0) /* Fill */
counter=counter+1;
return counter;
}
int main()
{
int y,m;
scanf("%d,%d",&y,&m); //30,2
int old_counter=0;// fill //
int counter=log_counter(y,m);
while(counter!=old_counter){
//printf("%d,%d,%d ",y,counter,old_counter);
old_counter=counter;
y= y/m; if(y<m)break; //Fill //
counter=log_counter(y,m);
}
printf("%d",counter);
return 0;
}
Sample testcases:-
Inputs 30,2 , Result: 4
Inputs 17,2 , Result: 4
Inputs 63,2 , Result: 5
The filled code segments are marked in bold.
Summary: log_counter function is called repeatedly with successive quotients of y and m each time incrementing the value of a static variable 'counter' in log_counter by 1 and replacing the value of y by y/m. This continues till y/m becomes less than m in which case wee reach the end condition for the compuattion and print the last value of counter returned from the function call. The loop can be analyzed by uncommenting the printf function in the code.