please help to check my program

This is a discussion on please help to check my program within the C Programming forums, part of the General Programming Boards category; My program is to cal. the value of 10^9(output = 1,000,000,000) is it correct after the line 'for'? thk. Code: ...

  1. #1
    Registered User alice's Avatar
    Join Date
    Mar 2004
    Posts
    36

    please help to check my program

    My program is to cal. the value of 10^9(output = 1,000,000,000)
    is it correct after the line 'for'?

    thk.



    Code:
    #include <math.h>
    int aFunctionIterative(int a, int b) {
     
     int i, fact;
    
     for ( i=1; i<=b ;i++ )
     fact=pow(a,i);
    
     return fact;
     }
    
    void main() {
    
     int result;
     result=aFunctionIterative(10,9);
    
     printf("%d",result);
    
     getchar();
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > fact=pow(a,i);
    You don't accumulate any answer, you may as well just do
    fact=pow(a,b);
    which just matches the last iteration of the loop

    > void main()
    Read the FAQ

    > printf("%d",result);
    You also need
    #include <stdio.h>
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    um, but doesnt pow(10, 9) give the answer?

    So another way of doing it without pow() would be:

    Code:
    int aFunctionIterative(int a, int b) {
        int i, ret;
        ret = 1;
        for (i = 1; i <= b; i++)
            ret *= a;
        return ret;
    }
    
    int main(void) {
        int result;
        result = aFunctionIterative(10,9);
        printf("%d",result);
    
        getchar();
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    lost in the stack...
    Join Date
    Apr 2004
    Posts
    11
    It worked in the Dev-C++ compiler, but may I make a few suggestions?

    First off, it should be 'int main()', NOT 'void main()' .

    Secondly, put 'return 0' at the end of the main function.

    Thirdly, the getchar() is used to keep the screen up after the program runs, but if you are running windows, using 'system("PAUSE");' is a better method of keeping the screen up.

    Fourthly, it is wise to have '#include <stdio.h>' for whenever you use the printf function.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,452
    It shouldnt work as intended, lo_tek, and shouldnt compile in Dev-C++ since gcc wont accept void main(). Side note: I think Dev-C++ has a tendency to automatically include other standard libraries once one of them is used.

    Using 'return 0;' to end main() isnt absolutely necessary nowadays, though should be good practice.
    Also, on the getchar() thing, since it is more portable, why not use it?
    After all, alice may not be a Windows user....
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Using 'return 0;' to end main() isnt absolutely necessary nowadays, though should be good practice.
    Implicit return 0; from the end of main() is a C++ feature
    I think C99 also does this, but there are not too many C99 compilers around and in common use
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-09-2009, 08:45 PM
  2. Suggestions for my Check Book program
    By Nor in forum C++ Programming
    Replies: 2
    Last Post: 11-17-2008, 05:44 PM
  3. program to check hard disk transfer rate
    By shadow99er in forum C Programming
    Replies: 3
    Last Post: 03-01-2002, 04:04 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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