Function middle – CTS PATTERN
Find the middle digit(s) of an integer - return the middle digit if odd length, or the two middle digits if even length
Understand the Problem
Problem Statement
Fix the logical error in the given middle(int num) function implementation.
The function middle(int num) accepts an integer num as input. If the count of digits in the integer num is odd, then the function returns the middle digit of the integer num. If the count is even, the function returns the two digits in the middle of the integer num.
A helper function findTenPowVal(int N) is available that returns the value of 10N.
The provided implementation compiles successfully but fails to return the desired result due to a logical error. Your task is to fix the program so that it passes all test cases.
Constraints
- The input number can be any integer (positive, negative, or zero)
- The function should handle numbers with 1 to 10 digits
- For negative numbers, the sign should be ignored when finding middle digits
- The helper function
findTenPowVal(N)returns 10N where N is a non-negative integer - The function must return an integer representing the middle digit(s)
Examples
123453The number 12345 has 5 digits (odd). The middle digit is 3 (at position 3).
12345634The number 123456 has 6 digits (even). The two middle digits are 3 and 4, forming the number 34.
77The number 7 has 1 digit (odd). The middle digit is 7 itself.
-123453For negative numbers, we ignore the sign. The digits are 12345, so the middle digit is 3.
Solution
#include <stdio.h>
#include <stdlib.h>
// Helper function (assumed to be provided)
int findTenPowVal(int N) {
int result = 1;
for(int i = 0; i < N; i++) {
result *= 10;
}
return result;
}
// Fixed implementation
int middle(int num) {
// Handle negative numbers by taking absolute value
if(num < 0) {
num = -num;
}
int count = 0;
int temp = num;
// Count the number of digits
while(temp > 0) {
count++;
temp /= 10;
}
if(count % 2 == 1) {
// Odd number of digits: return middle digit
num = num / findTenPowVal(count / 2);
return num % 10;
} else {
// Even number of digits: return two middle digits
// Fix: use (count-2)/2 instead of count/2
num = num / findTenPowVal((count - 2) / 2);
return num % 100;
}
}
// Test function
int main() {
printf("middle(12345) = %d\n", middle(12345)); // Expected: 3
printf("middle(123456) = %d\n", middle(123456)); // Expected: 34
printf("middle(7) = %d\n", middle(7)); // Expected: 7
printf("middle(-12345) = %d\n", middle(-12345)); // Expected: 3
return 0;
}Step-by-step explanation:
- Handle negative numbers: Convert negative input to positive using
abs()or manual negation - Count digits: Use a while loop to count digits by repeatedly dividing by 10
- Odd case: For odd digit count, divide by 10^(count/2) to remove right digits, then mod 10 to get leftmost digit
- Even case: For even digit count, divide by 10^((count-2)/2) to remove right digits, then mod 100 to get leftmost 2 digits
- Key fix: The original error was using
count/2instead of(count-2)/2for the even case
Example walkthrough for 123456:
- Count = 6 (even)
- Divide by 10^((6-2)/2) = 10^2 = 100: 123456 ÷ 100 = 1234
- Take modulo 100: 1234 % 100 = 34 ✓