Ask a questionAsk a question
 

AnswerHelp with function

  • Wednesday, November 04, 2009 5:17 AMcarl89_P Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi im trying to write pow function, i know that i can use <math.h> at top of my code but the deal is i cannot use that in my code so i need to write a function  for pow and its get call in int main(), its taking value from user and givinf out put help,plz
    thz

Answers

  • Wednesday, November 04, 2009 3:20 PMcarl89_P Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi AS u said i know how to write it by pencil,

    pow(x,y)
    X^y = answer

    2^3 = 8

    but in c++ we cannot use "  ^ " sign so is there anything that can replace that " ^ " in c++.
    thx
    • Marked As Answer bycarl89_P Wednesday, November 04, 2009 7:10 PM
    •  

All Replies

  • Wednesday, November 04, 2009 5:37 AMBrian MuthMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I hope you are aware we are not here to give answers to homework assignments. However, I'm guessing the teacher wants you to use other functions in place of pow(). exp() and ln() come to mind.
  • Wednesday, November 04, 2009 5:41 AMScott McPhillipsMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Do you know how to do it with a pencil?  That is the first step.  You can't write code without an algorithm.

    Tell us or show us what you have so far.  And ask a specific question.



  • Wednesday, November 04, 2009 3:20 PMcarl89_P Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi AS u said i know how to write it by pencil,

    pow(x,y)
    X^y = answer

    2^3 = 8

    but in c++ we cannot use "  ^ " sign so is there anything that can replace that " ^ " in c++.
    thx
    • Marked As Answer bycarl89_P Wednesday, November 04, 2009 7:10 PM
    •  
  • Wednesday, November 04, 2009 3:45 PMjinzai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    ...and that is how it is done. If what you wrote does not make it any clearer, perhaps you should write it out using words, and not math symbols.

    So, what does pow do? It takes two arguments, one is the number in question, the other is going to be the number of times that you multiply the first number by itself to achieve the desired result. Loops work well for this. You also need to return a result.

    So, you have a basic for loop to write, if you choose to do it that way. You will need to choose an appropriately large return type, because you are writing a function that could potentially return a very large integer. You will need to accept two integers as parameters.
    • Proposed As Answer byKEERV Wednesday, November 11, 2009 9:18 AM
    •