i feel that i lost my mind

0 Mahmoud Abdel Ghani · September 5, 2014
 Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power() that takes a double value for n and an int value for p, and returns the result as a double value. Use a default argument of 2 for p, so that if this argument is omitted, the number n will be squared. Write a main() function that gets val- ues from the user to test this function. .....i want to understand what he want?????i lost my mind

Post a Reply

Replies

Oldest  Newest  Rating
+2 Mathias Frits Rørvik · September 7, 2014
Seems like you are missing some core concepts. Have you tried going a few chapters back and try to do the earlier exercises? Or you could try another book, I suggest C++ Primer by Josée Lajoie and Stanley B. Lippman. 

Anyway, one possible solution would be this.
#include <iostream>

double power(double n, int p = 2)
{
    double answer = n;

    for (int i = 0; i < p - 1; i++) {
        answer = answer * n;
    } 

    return answer;
}

int main() 
{
    double a;
    int b;

    std::cout << "Enter first number: ";
    std::cin >> a;

    std::cout << "Enter second number: ";
    std::cin >> b;

    std::cout << a << " to the power of " << b << " is ";
    
    double result = power(a, b); //call power function on a and b
    std::cout << result << std::endl; //print result

    return 0;
}
0 Mahmoud Abdel Ghani · September 5, 2014
object oriented programming in c++ forth edition ..im trying to finish it and solve the exercises but i feel that my mind stop evertytime while i solve the exercises
+1 Mathias Frits Rørvik · September 5, 2014
Which book are you reading?
-1 Mahmoud Abdel Ghani · September 5, 2014
i begin to hate programming :(
+1 Franz Schmidt · September 5, 2014
He wants you to make a function which can calculate power
e.g.: n=2 p=4 => n^p = 2^4 = 2*2*2*2 =16

and this you have to make in a function with paramters (n as double parameter and p as int parameter)
the returnvalue has to be the result of the calculation in double format.

and if p value gets obmitted you should make a default value of 2 so he can calculate n^2
  • 1

C++

107,275 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator