Thread: Questions

  1. #1
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19

    Questions

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    
    int number, result;
    double square_number;
    
    int
    square(int number)
    {
        while(number >= 0)
        {
            square_number = pow(2,number);
        }    
    }
    
    int
    main(void)
    {
        printf("Enter an integer:");
        scanf("%d", &number);
        printf("\nNumber entered is: %d\n", number);
        result = square(number);
        
        while(number >= 0);
        {
            if(number < result)
            {
                printf("%d is a number less than the square of itself.\n", number);
                printf("Please enter another integer:\n");
                scanf("%d", &number);
            }
            else
            {
                printf("%d is a number greater than the square of itself.\n", number);
                printf("Please enter another integer:");
                scanf("%d", &number);
            }
        }
        getch();
        return (0);                  
    }
    Im a newbie at this, so please bare with me. This question relates to the main function. I have the following:

    Code:
    printf("Enter an integer:");
    scanf("%d", &number);
    printf("\nNumber entered is: %d\n", number);
    and in the if statement, i have two alternatives, which have the code similar to above:

    Code:
    printf("Please enter another integer:\n");
    scanf("%d", &number);
    From the two examples of code, which are the same actions - ie, entering a number, is this correct? Is there a simpler way, like a call function?

    Also, I have a function named square. Does it matter where I put the code? Its before the main function.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    
    int number, result;
    double square_number;  // This is not needed
    
    double
    square(int number)
    {
        // You need to return a value here... the while loop is unnecessary
        // and in fact is an endless loop for n >= 0.
        //while(number >= 0)
        //{
        //    square_number = pow(2,number);
        //}
        //Just return the result of the pow function
        return pow(2,number);
    }
    
    int
    main(void)
    {
        printf("Enter an integer:");
        scanf("%d", &number);
        printf("\nNumber entered is: %d\n", number);
        result = square(number);  // This function call should go within the while loop
        
        while(number >= 0);
        {
            result = square(number);
            if(number < result)
            {
                printf("%d is a number less than the square of itself.\n", number);
                
                // These two lines of code are duplicate in both parts of the if/else
                // when you only need to ask the user once
                printf("Please enter another integer:\n");
                scanf("%d", &number);
            }
            else
            {
                printf("%d is a number greater than the square of itself.\n", number);
                
                // Again, you don't need these two lines here
                printf("Please enter another integer:");
                scanf("%d", &number);
            }
            //Just have the code to ask user for another number in one place
            printf("Please enter another integer:");
            scanf("%d", &number);
    
        }
        getch();
        return (0);                  
    }
    Quote Originally Posted by Daveo
    Also, I have a function named square. Does it matter where I put the code? Its before the main function.
    If you have the function defined as you do, before its actual use elsewhere in the code, then you don't need a function prototype. If however you define the function later in the code after you have made a call to it somewhere prior in the source then you will need to prototype it, i.e.

    Code:
    double square(int number);  // Need this prototype
    
    int main()
        ...
        result = square(number);  // Function called before it is fully defined
        ...
    }
    
    double square(int number)  // Finally we define the function
    {
        return pow(2,number);
    }
    [edit]number and result should probably be doubles[/edit]
    Last edited by hk_mp5kpdw; 09-02-2004 at 05:50 AM.
    "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

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