power calculate

This is a discussion on power calculate within the C Programming forums, part of the General Programming Boards category; Write a function that calculates a to the power b. Write another function that calls this function and print a ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    5

    power calculate

    Write a function that calculates a to the power b. Write another function that calls this function and print a table of exponantials for the given a, b values. Exponents are from 0 to a, and numbers are from 0 to b. The output is:

    http://img442.imageshack.us/img442/667/image267ym0.jpg

    it's one of my homework questions. i did all of them expect that one. can anyone help me please?

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Have you written a function that calculates the power?
    Have you written a function that calls that function?
    Do you know how to output text to the screen?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Do the first part first.

    You'll need to use loops to do both parts of the problem.

    Do you know how to compute a to the power of b on paper? If you do, then post it.

    http://www.cprogramming.com/tutorial/c/lesson3.html

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Quote Originally Posted by Elysia View Post
    Have you written a function that calculates the power?
    Have you written a function that calls that function?
    Do you know how to output text to the screen?
    on the table, left side is power (a), upside is number (b).

    power function :

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main() {
        
        int a,b,c;
        printf("first number: ");
        scanf("%d",&a);
        printf("second number: ");
        scanf("%d",&b);
        c=pow(a,b);
        printf("result: %d",c);
        getchar(); getchar();
        
    }

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    You were supposed to write the power function, were you not? Not use an existing.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Shouldn't I use that pow function ? pow(a,b) ? Do I've to write a new function ?

    Code:
    #include <stdio.h>
    
    int main() {
        
        int a, b, result;
        int c;
    
        printf("number ");
        scanf("%d", &a);
    
        printf("power ");
        scanf("%d", &b);
    
        result = 1;
        
        for (c=0; c<b; c++) {
            result *= a;
            }
            
            printf("result %d\n", result);
            
            getchar(); getchar();
            return(0);
    }

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Write a function that calculates a to the power b.
    You do read your assignments before you post them, right?

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Actually, it wouldn't be a bad idea to use the pow function as a placeholder until you write the rest of the program. Then you can make sure that your program works, and write the function that you will use afterwards.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    Quote Originally Posted by MacGyver View Post
    You do read your assignments before you post them, right?
    in the assignment paper here it's the table :

    http://img530.imageshack.us/img530/7503/gdsf1lx8.jpg

    i think, a is exponent. b is number that we'll calculate the result. ? am i wrong ? then, i did a new table in my first post. i'm working on that table for the code.

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Wouldn't b be the exponent here?

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    5
    But it says

    Exponents are from 0 to a, and numbers are from 0 to b
    We stopped solving the question, started to discuss about the question

    Then, if it's b is the exponent. Then, let's solve it with a is number, b is exponent.

    if a is 2, b is 3. The result ? 8 or 9. ?

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    In exponental notation, the big number is called the base and the small number is called the exponent. so:
    Code:
     exponent
    base
    a is the base and b is the exponent.
    Code:
     b
    a
    if a is 2, b is 3. The result ? 8 or 9. ?
    Code:
     3
    2 = 2 * 2 * 2 = ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM
  2. Power supplies and demanding processors
    By Liger86 in forum Tech Board
    Replies: 12
    Last Post: 03-17-2005, 10:56 AM
  3. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 01:47 PM
  4. x power y?
    By Luigi in forum C++ Programming
    Replies: 6
    Last Post: 04-25-2003, 08:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21