2016-11-08 9 views
-1

私はこのプログラムの割り当てをしており、その一部はユーザーが入力した数値を超えずに(x)に行く最大電力を見つけようとしています(y)を超える。私たちはそれを関数で使用しています。これは、プログラム全体であり、それは私が最大出力y回以上の回帰で数字を上げることができます

#include <iostream> 
#include <cmath> 

using namespace std; 

// meunue where you can get your options from 
void menue() { 
    cout << "choose the following options:" << endl; 
    cout << "1) Power of x raised by y." << endl; 
    cout << "2) Find the max power a number can be raised to." << endl; 
    cout << "3) Print out a number with its digits in reversed order." << endl; 
    cout << "4) Sum of integers from 1 to n." << endl; 
    cout << "5) Product of integers from 1 to n." << endl; 
    cout << "6) Quit" << endl; 
} 

//functions for finding the power usign recursion 
int Power(int a, int b) { 
    int x = 1, i; 
    for (i = 1; i <= b; i++) { 
     if (b == 0) { 
      return Power(a, b--); 
     } 
     else { 
      x = x * a; 
     } 
    } 
    return x; 
} 

int maxpower(int n, int max_value) { 
    int temp = temp * n; 
    if (temp > max_value) 
     return 0; 
    else return maxpower(n, max_value + 1); 
} 

int reverse(int number) { 
    int lastDigit, numberOfDigits, sign = 1;//sets the sign equal to one 
    // if number is less than 0 returns 0 
    if (number < 0) { 
     return 0; 
    } 
    else 
     //if a number is under 10 than it can not be switched so you times the number  by 10 and switch it. 
    if (number < 10) 
     return number * sign; 

    lastDigit = number % 10; 
    number = number/10; 
    numberOfDigits = log10(number) + 1; 
    //recursive statement that calls the function 
    return (lastDigit * pow(10, numberOfDigits) + reverse(number)) * sign; 
} 

//finding the sum 
int sum(int n) { 
    if (n != 0) { 
     return n + sum(n - 1);//recursive statement 
    } 
    else { 
     return n; 
    } 
} 

//finding the product 
int product(int n) { 
    int temp; 
    if (n <= 1) { 
     return 1; 
    } 
    else { 
     temp = n * product(n - 1); 
     // recursive statement setting temp == to recursive statement 
     return temp;//returning temp 
    } 

} 


int main() { 
    int a; 
    int x; 
    int y; 
    int length = 0; 
    int temp; 
    int results; 
    // calls menue and get prints all the options 

    do { 
     menue(); 
     //inserts the choice 
     cin >> a; 

     cout << "you choose:" << a << endl;//prints the choice out. 
     //switch statement that will take account for the number you choose and prints the results 
     switch (a) { 
      case 1: 
       cout << "enter the number to raise" << endl; 
       cin >> x; 
       cout << " enter the power to raise to: " << endl; 
       cin >> y; 
       Power(x, y); 
       cout << "the result is:" << Power(x, y) << endl; 
       break; 

      case 2: 
       cout << "Enter the number to raise:" << endl; 
       cin >> x; 
       cout << "Enter the number not to exceed:" << endl; 
       cin >> y; 
       maxpower(x, y); 
       cout << "the result is:" << maxpower(x, y) << endl; 

       break; 

      case 3: 
       cout << " enter numbers to be reversed by: " << endl; 
       cin >> x; 
       temp = x; 
       while (temp != 0) { 
        length++; 
        temp = temp/10; 
       } 
       reverse(x); 
       cout << "the result is:" << reverse(x) << endl; 
       break; 

      case 4: 
       cout << "enter the number to sum to: " << endl; 
       cin >> x; 
       sum(x); 
       cout << "the result is:" << sum(x) << endl; 

       break; 

      case 5: 
       cout << "enter the number to multiply to:" << endl; 
       cin >> y; 
       product(y); 
       cout << "the result is:" << product(y) << endl; 
       break; 
      case 6: 
       cout << "good bye!!" << endl; 
       break; 
     } 

    } while (a != 6); 
    return 0; 
} 
+1

「int temp = temp * n;」という行は、私には間違っています。 tempという名前のintを宣言しています(値は割り当てられていません)。次に、temp =(初期化されていない値)* nと言っています。 – JGroven

+0

int temp = n * n; int power = 0; if(temp> max_value) return 0; //これは編集されたバージョンです.else if(temp

+0

上記のコメントと一致するようにコードを編集しようとしましたが、変更が単にint temp = temp * nを修正しただけではないので、私はロールバックしました – drescherjm

答えて

0

を把握しようとしていますint型のMaxPowerに(int型のx、int型のy)の関数である最大電力のために私は何を持っている、それだけで0を返す続けて、私はそれはないと思いますこの問題の再帰を使用するために必要です。さらに、再帰はループを使って解決するとオーバーヘッドが大きくなります。再帰を使用する必要がありますか?もしそうなら、この答えを無視してください:p。しかし、あなたは以下の解決策を見つけるでしょう。

#include <math.h>ビットにご注意ください。pow(base, exponent)を使用する必要があります。

また、while(true)は確かにベストプラクティスではありませんが、ループから正しく抜け出すための十分なチェックがある限り、正常です。したがって、あなたが探しているmax_iterationと実際のreturnのステートメント。

運が良かった!

出力の例として
#include <iostream> 
#include <math.h> 

int maxpower(int n, int max_value) { 
    if (n > max_value) return 0; 
    int previous, current = 1; 
    int max_iteration = 0; 

    while (true) { 
     if (max_iteration >= 1000) return -1; 
     if (pow(n, current) > max_value) { 
      return previous; 
     } 
     previous = current; 
     current++; 
     max_iteration++; 
    } 
} 

int main() { 
    int x; 
    int y; 
    int result; 

    std::cout << "Enter the base: "; 
    std::cin >> x; 

    std::cout << "Enter the max number x^pow should not exceed: "; 
    std::cin >> y; 

    result = maxpower(x, y); 

    if (result == -1) { 
     std::cout << "Max iteration reached." << std::endl; 
    } 
    else { 
     std::cout << result << " is the maximum power such that " << x << "^" << result << " does not exceed " << y << std::endl; 
    } 
    return 0; 
} 

x = 2及びy = 32ならば、プログラムは、すなわち、2^5 = 32(最大電力として5を返し、より大きくはないが、2う^ 6> 32)。

EDIT:

私はあなたのすべての機能が再帰的であることを掲示した後、私は実現するので、おそらくそれはあなたの割り当てのための要件です。とにかく、以下の再帰的なソリューションです:

int maxpower_rec_helper(int n, int power, int max_value) { 
    if (pow(n, power) > max_value) return power - 1; 
    return maxpower_rec_helper(n, power + 1, max_value); 
} 

int maxpower_rec(int n, int max_value) { 
    if (n > max_value) return 0; 
    return maxpower_rec_helper(n, 1, max_value); 
} 

あなたが最初のパワー1を与えること、およびように、あなたのmax_valueを乱さないヘルパー関数が必要になります。

return power - 1;は、上記の反復例のreturn previous;と基本的に同じです。

関連する問題