Ask Question
28 April, 19:21

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.

+1
Answers (1)
  1. 28 April, 19:43
    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?
Get 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 & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers