Question
Consider the following C code snippet for calculating
base raised to the power of exp:   #include   double power(double base, int exp) {     if (exp == 0) {       return 1;     }     // Missing handling for negative exponent     return base * power(base, exp - 1);   }   int main() {     printf("%.2f\n", power(2.0, -2));     return 0;   }   What will be the output or behavior of the program when power(2.0, -2) is called?Solution
The power function correctly handles exp == 0 and positive exp. However, it lacks a specific base case or handling for negative exponents. When power(2.0, -2) is called, exp is -2, which is not 0. The function then calls base power(base, exp - 1), which becomes 2.0Â power(2.0, -3). This process continues indefinitely, with exp becoming more and more negative (-4, -5, etc.). The exp will never reach the base case of 0, leading to infinite recursion, a stack overflow, and typically a segmentation fault. To fix this, a condition for exp < 0 should be added, typically return 1.0 / power(base, -exp);.
What is the rank of India in Global firepower Index 2023?
What specific assistance did Nepal's Prime Minister seek from China regarding the Pokhara International Airport?
Which Formula One driver won his first title at the Hungarian Grand Prix in 2024, and what notable achievement was highlighted in this race?
Rooppur Nuclear Power Plant is situated in which of the following country?
Who is the current Governor of the Reserve Bank of India (RBI) as per the latest ₹20 banknote issuance?
Which of the following places of INDIA has largest COAL reserves?
Spice Money, the rural fintech company, announced building a digital financial services platform for consumers from rural and semi-urban India in partne...
Which company has entered into a strategic partnership with Hyperface, a Credit Cards-as-a-Service (CCaaS) platform to facilitate the digital issuance o...
How much annual transit fee will Afghanistan receive from the TAPI pipeline project?
According to the new data of the central government, food processing industry has made an investment of Rs ____crore under the production-linked incenti...