Thread: please help to check my program

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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....
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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