Thread: 2 questions...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Exclamation 2 questions...

    First off: does anybody know how to multiply a number to a power (other than using a for loop)?


    But more importantly: I have source code for a prgram but it is written in C i want to compile the code in C++ (the code is written not on a computer) What are the major differences and how do i convert them? I tried finding some source in C++ but i couldn't. Any help would be greatly appreciated! Thanks in advance!

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I can answer one of your two questions:


    the cheap and easy way
    Code:
    #include<cmath>
    
    int answer = pow(number, exponent);


    recursively
    Code:
    double power( double base, int exp)  
    {
          if (base == 0)  //base is zero, and exp should be positive
    
               return 0;
    
          else if (exp == 0)
    
               return 1;
    
          else if (n > 0)  
           
               return base * power(base, exp - 1);
    
          else   //base is nonzero, and exp is negative
    
               return  1/power(base, -exp);
    
    }
    Last edited by The Brain; 01-04-2005 at 09:00 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    1: you can use the pow function.

    2: A c++ compiler can pretty much compile all c source there are some random things that c++ will complain about c. I don't know them but I am sure someone here does
    Woop?

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    First off: Thanks for your guys help

    But i really want to understand the code (it is a complicated GOST encryption algorithm) which is why i want it in C++. I can figure out most of the C code but alot is eluding me. Like there are no header files and stuff like that. Thanks again!

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I have source code for a prgram but it is written in C i want to compile the code in C++
    If it's written in C, it's written in C. If you want to understand it, compiling it for C++ isn't going to do one bit of difference, because you still won't understand the code.

    If the unreadability of the code is due to lack of headers etc, then it's poorly written code, and the unreadability has nothing to do with the language.

    >>but alot is eluding me
    Whatever you don't get, try and figure out; when you get stuck, ask for help.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by The Brain
    recursively
    Code:
    double power( double base, int exp)  
    {
          if (base == 0)  //base is zero, and exp should be positive
    
               return 0;
    
          else if (exp == 0)
    
               return 1;
    
          else if (n > 0)  
           
               return base * power(base, exp - 1);
    
          else   //base is nonzero, and exp is negative
    
               return  1/power(base, -exp);
    
    }

    What is n?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    oops..
    Code:
    n == exp
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    double pow( double base, double exp ); // Valid C and Valid C++

    pow() uses type double. So, you can raise a number to a non-integer. i.e. 2 to the 2.5 power. (You can't do that with the loop or recursion method.) This also means that you might have to typecast if you're not working with doubles.

    Always use the fully-debugged built-in standard functions, unless you have a really good reason not to!

    If your source is written in standard C, It should compile fine on a C++ compiler. 99% of C is valid C++ too. That is, C is mostly a subset of C++.

    However, if your source contains non-standard code, it won't compile. Compare all of the header files used in the code to a list of standard C++ headers. (I use dinkumware.com as a standard reference.) If all of the headers are standard, you should be OK. If your source includes something non-standard like graphics.h, you'd need the original compiler for-which the program was written.
    Last edited by DougDbug; 01-05-2005 at 07:37 PM.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You didnt help at all!
    Assuming this was from Junior, was a negative rep, and wasn't sarcastic:

    If you clarified your question, you might have gotten a more helpful answer.

    >>I have source code for a prgram but it is written in C i want to compile the code in C++
    Very well, somebody already told you that C can be generally compiled as C++. I simply added that compiling will do nothing toward 'converting' the source code to C++

    >>What are the major differences and how do i convert them?
    To this, my very valid reply was that if there are incompatibilities then it is nigh impossible to 'translate' the C code into C++ without knowing anything about what it is, what the errors are, etc. And if it compiles fine but you simply do not understand what it does, then that indicates that you do not know enough C++ to understand it - because apparently it is valid C++ as well.

    >>I tried finding some source in C++ but i couldn't.
    Very good.

    >>Like there are no header files and stuff like that.
    As I tried to explain, headers are in C too. That code is very likely valid C++ as well, so a lack of headers has nothing to do with the language. It's simply that whoever wrote the code did a bad job and made it really messy and unreadable.

    >>I can figure out most of the C code but alot is eluding me.
    And to this I replied, figure out what you can and we'll help with the rest. Was that not fair of me? You have not shown one byte of code, and yet you expect us to somehow magically come up with an answer for your unanswerable nonsensical question. Show some code and we'll help you understand it.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    I didnt say i wanted your help with specifics at all. I just wanted to know some of the major differences that's all. The program is way over Your head anyway mathematics wise. It is the former USSR GOST 28147-89.

    P.S. - You really look like a big game hunter with your little Chipmunk Avatar, i use them for target practice with my BB Gun. Your a big man now go out and get a real gun and do some real hunting you Wannabe Hunter!

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>But i really want to understand the code
    Good for you.

    >>how do i convert them?
    Convert what?

    >>I didnt say i wanted your help with specifics at all.
    I apologize. I was under the distinct impression otherwise.

    >>The program is way over Your head anyway mathematics wise.
    Very likely so, but then it would seem likely that your own chances of understanding it are very slim as well. And you don't need to capitalize 'Your' when referring to me. I'm not God.

    You really look like a big game hunter with your little Chipmunk Avatar, i use them for target practice with my BB Gun. Your a big man now go out and get a real gun and do some real hunting you Wannabe Hunter!
    I'm a computer nerd who happened to pick the name Hunter2 a long time ago, and who happened to take a picture of a ground squirrel in a no-hunting national park and use it as my avatar, while in highschool.

    May the gurus red-square you to oblivion; I came with the intention to help, and I now wash my hands clean of this whole bloody affair.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    First off: thanks for everybody's help in this affair. I got it all figured out (i think).

    Back to Hunter2 (although he doesnt have the guts to kill something)

    I congratulate you on admitting to being a computer nerd. I however am not. I program every now and then to keep me busy. I could care less how many people red-box me, i dont care. I dont mind starting a little trouble here and there, it makes things interesting. By saying Your i meant to emphasize the word. I do not consider you anything near that of a god. The reason i picked on your name was 1. You ........ed me of with your post and 2 your avatar and name ........ed me off becasue im actually a real hunter. I go OUTSIDE instead of staying in on a computer all day and getting paler and paler. Once a year i go out hunting for deer season and hike 5 miles up a mountain with a 20 pound backpack and a 10 pound rifle. I also go Pheasant hunting, turkey hunting, etc. I am also a freshman in high schoo (15 years old). So let me give YOU (emphasis; not Worship) some advice: go outside, jog, play sports, go to the gym, etc. get a real life meet a nice girl, etc. etc. You will never be happy if you sit on a computer all day, you may move up in your career but you'll die alone and no one will care about you.

    ...Once again thank you for all of your time your help was greatly appreciated.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    This is done
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM