Thread: help involving powers and a clock?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    10

    help involving powers and a clock?

    okay, I need some help, I need to write a program where I can enter a first number, then a second one, and it raises the second one to the first one, only problem is I don't know how to do powers, if some one could write me a short quick example it would be much appreciated.
    edit: here is my code so far, but it isnt working for some reason, could be because I'm not doing the powers right

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    
    void powfun(int, int);
    int main()
    {
        int exp, fnum;
        
        cout << "Please enter an interger for an exponent" << endl;
        cin  >> exp;
        cout << "Now please enter an interger to be raised" << endl;
        cin  >> fnum;
        powfun(exp, fnum);
        system("pause");
        return 0;
    }
    
    void powfun(int exp, int fnum)
    {
            cout << fnum << "Raised to the power of " << exp << " is " << fnum ^ exp << endl;
            
            return;
            
    }

    my second problem is basically to write a program that converts time passed (since program was opened) into the equivilant hours mins and seconds, this is what i have so far and it doesn't really work. basically it kind of counts the seconds, but not exactly

    Code:
    #include <iostream.h>
    #include <time.h>
    using namespace std;
    int main ()
    {
    int sec_counter, min, hour, sec;
    for (sec_counter = 1; sec_counter <= 60; sec_counter++)
    {
    cout << "Seconds Within the for loop: " << clock()/CLOCKS_PER_SEC;
    system("pause");
    }
    system("pause");
    return 0;
    }
    thanks for any help in advance
    Last edited by rllove37; 03-12-2010 at 05:49 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Did you search the standard C++ library? You would have found the reference for pow. This link has an example for C, but for C++ you include "cmath" instead of "math.h", and the rest should be useful enough to create your C++ version of it. If you have problems compiling, explicitly link to the library. In G++, you'd pass another argument to the compiler, such as "-lm", for Link math.

    Similarly for your clock program you should have looked up the reference. This link, again, contains an (C) example. Try to understand it and modify as you require to make a C++ program that solves your given problem.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    You "#include <cmath>", because you probably want to use the "pow" function, but you never actually call that function.
    Code:
            cout << fnum << "Raised to the power of " << exp << " is " << fnum ^ exp << endl;
    The "^" is for bitwise XOR, which you probably don't want. In place of "fnum ^ exp", call the "pow" function, giving the two arguments (in the correct order of course).

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    114
    Code:
    #include<windows.h>
    #include<iostream.h>
    using namespace std;
    
    int main()
    {
         int seconds, minutes, hours;
         bool running = false;
         minutes = 0;
         hours = 0;
         while(running == false)
         {
              Sleep(100);
              seconds++;
              if(seconds == 60)
              {
                   seconds = seconds - 60;
                   minutes++;
              }
              if(minutes == 60)
              {
                   minutes = minutes - 60;
                   hours++;
              }
              if(kbhit())
              {
                  running = true;
              }
          }
           cout <<hours<<":"<<minutes<<":"<<seconds;
           Sleep(1000);
    }
    Don't know if that works, just a very basic time counter.

    And to transform variable 1 into the base and variable 2 into an exponent.
    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
       int variable1,variable2;
       variable1 = 0; variable2 = 0;
       cout <<"Input Number for variable1:";
       cin>>variable1;
       cout <<"Input Number for variable2:";
       cin>>variable2;
       while(variable2 > 0)
       {
           variable1 = variable1 *variable1;
           variable2--;
       }
       cout <<variable1;
       system("pause");
    And i didnt bother to look for power but this might work for you.
    Who needs a signature?

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    10
    i dont know how to use a pow function, we never did it in class before, ill test the clock as well

Popular pages Recent additions subscribe to a feed