-3

Im trying to Write a function that accepts two input parameters and computes the result of raising the value of the first input to the value of the second input, and returns the result. You are not allowed to use the multiplication operator(*)for performing multiplication, nor are you allowed to use any built-in C++ functionality that directly produces the exponentiation result.

Data Types: function works correctly for both inputs being positives, zeroes, or ones, and when the second input is negative. my return is always 0 what am I doing wrong

#include <iostream>

using namespace std;

int exp(int, int);

int main()
{
    int num1, // to store first number
        num2, // to store second number
        value = 0;

    // read numbers
    cout << "Enter first number: ";
    cin >> num1;
    cout << "Enter second number: ";
    cin >> num2;

    // call function
    exp(num1, num2);

    // print value
    cout << "The value of " << num1 << " to the " << num2 << " is: " << value << endl << endl;

    system("pause");
    return 0;
}

// function definition
int exp(int a, int b)
{
    int result = 0;

    for (int i = 0; i < a; i++)
    {
        result += b;
    }
    return result;
}
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
Shadow
  • 1
  • 4

1 Answers1

1

True, you are implementing multiplication not exponentiation, but the reason you are getting 0 everytime is that you are not storing the return value of your function call in value variable and are instead just outputing the zero everytime.

CodeoftheWarrior
  • 311
  • 2
  • 12
  • value = exp(num1, num2); – CodeoftheWarrior Mar 27 '19 at 21:11
  • I've only started c++ a couple months ago so I'm pretty green when I put in value = exp(num1, num2); I got uninitialized num2 error used I also don't know what special case is and where it is on my code – Shadow Mar 27 '19 at 22:40
  • what is meant by I implemented multiplication, not exponentiation? is that in the for loop for (int i = 0; i < a; i++) { result += b; – Shadow Mar 28 '19 at 00:26
  • I figured it out I had this value = exp(num1, num2) in the wrong place and fixed the for loop /function definition int exp(int a, int b) { int result = 0; for (int count = 0; count < a; count++) { result = result + b; } return (result); – Shadow Mar 28 '19 at 01:49