Ask Question
2 March, 12:09

Write a C function, unsigned int power (unsigned int x, unsigned int p), that calculates xp. Place the function in a file with a filename hw3_prob3-5. c. Include a main function that calls this function with several values to test the function. Be sure to handle the case of p = 0.

+3
Answers (1)
  1. 2 March, 14:17
    0
    unsigned int power (unsigned int x, unsigned int p)

    {

    unsigned int answer = 1;

    while (p--)

    {

    answer * = x;

    }

    return answer;

    }

    void Test (unsigned x, unsigned int p, unsigned int expected)

    {

    int answer = power (x, p);

    std::cout << x << "^" << p << "=" << answer;

    if (answer = = expected)

    {

    std::cout << " (as expected) " << std::endl;

    }

    else

    {

    std::cout << " ERROR" << std::endl;

    }

    }

    int main ()

    {

    Test (3, 0, 1);

    Test (3, 1, 3);

    Test (3, 2, 9);

    }

    Explanation:

    In case of questions, dm me!
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Write a C function, unsigned int power (unsigned int x, unsigned int p), that calculates xp. Place the function in a file with a filename ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers