Thread: I'm new taking my 1st class, and I am having trobble with my homework.. help please!

  1. #46
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Cess View Post
    I am still not understanding.....
    A semicolon terminates a statement. Loops need a statement attached, typically, to actually do something:
    Code:
    #include<stdio.h>
    int main( void )
    {
        int x = 0;
        for( x = 0; x < 10; x++ )
            printf( "x is %d\n", x );
        return 0;
    }
    Run that. Now add a ; to the end of the line with the for loop. Run it again.

    Without a pair of braces, only the immediate statement after a loop (or an if, or an else, etc.,) is attached:
    Code:
    if( x == 0 )
        ; // this statement is attached to the if
    ; // this one is not
    Note, it doesn't matter what line you have, but what actual statement is next:
    Code:
    if( x == 0 )
    
    
    
        ; // this is attached to the if
    Code:
    if( x == 0 ); // this ; is attached to the if

    Quzah.
    Last edited by quzah; 09-24-2011 at 07:03 PM.
    Hope is the first step on the road to disappointment.

  2. #47
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Cess View Post
    I am still not understanding.....
    A for loop have these components

    Code:
    for (i = 0; i < 10; i++) {
        /* loop body */
    }
    A semicolon in there will be interpreted as being part of the loop body, i.e an empty statement.

  3. #48
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Subsonics View Post
    A for loop have these components

    Code:
    for (i = 0; i < 10; i++) {
        /* loop body */
    }
    A semicolon in there will be interpreted as being part of the loop body, i.e an empty statement.
    To illustrate one step further:
    Code:
    for (i = 0; i < 10; i++) ; {
        /* not the loop body */
    }
    That ; makes all the difference.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #49
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Cess View Post
    ok I now understand that....but I don't think that was the only thing I had wrong....

    I've tried chancing it a few times.... and I'm not getting it to give me the factorial...

    Code:
        /* Factorial of the number #5 */
           fact=number;
           for (m=0;m<=number;m=m+1)
               {
               fact=fact*m;
               printf("%d is the factorial\n",fact);
               }
    So now you are back to:
    Quote Originally Posted by Subsonics View Post
    You initialize fact to zero, then you multiply that with i. What happens when you multiply something with zero?
    It's not a race. Take your time and read what people are telling you.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #50
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok I now understand that....but I don't think that was the only thing I had wrong....

    I've tried chancing it a few times.... and I'm not getting it to give me the factorial...

    Code:
       fact=number;
           for (m=1;m<=number;m=m+1)
               {
               fact=fact*m;
               printf("%d is the factorial\n",fact);
               }
    I'm new taking my 1st class, and I am having trobble with my homework.. help please!-untitled-png
    Last edited by Cess; 09-24-2011 at 08:49 PM.

  6. #51
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's pretty obvious it's not the only thing you have wrong, when your output is "1 is the square root of...".


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #52
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    HA HA! Yeaah!! I got it to work.... thank you all for your help!!!!
    Code:
    int main()
    {    
         int number, cube, i ,sumx ,fact ,m;
         float sqrot;
         printf(" enter a positive number between 1 and 10:");
         scanf("%d",&number);
         
         if( (number < 1) || (number > 10) ) {
            printf("The number is out of range. (1-10)\n");
            return 0;
            }
         
         /* Check to see if number is odd or even #1 */
         if ((number & 1) > 0)
          printf("%d is odd\n",number);
          else 
          printf("%d is even\n", number); 
         
          /* cube the number #2 */
          cube=number*number*number;
          printf("%d is the cube of that number\n",cube); 
          
          /* square root the number #3 */
          sqrot=sqrt(number);
          printf("%12.5f is the square root of that number \n",sqrot);
          
          /* Sum of the digits #4 */
          sumx=0;
          for (i=1;i<=number;i=i+1)
          sumx =sumx + i;
          printf("%d is the sum of the digits \n", sumx);
    
          /* Factorial of the number #5 */
           fact=1;
           for (m=1;m<=number;m=m+1)
               {
               fact=fact*m;
               }
               printf("%d is the factorial\n",fact);
        
    system("PAUSE");
    return 0;
    }

  8. #53
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by quzah View Post
    It's pretty obvious it's not the only thing you have wrong, when your output is "1 is the square root of...".


    Quzah.
    You are being SO picky Quzah!

  9. #54
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    You are being SO picky Quzah!
    He paid me a compliment and even admitted i had a better solution a bit earlier... I'm thinking he's just evening up the score

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework class help
    By nofear_k11 in forum C Programming
    Replies: 2
    Last Post: 09-02-2011, 05:06 AM
  2. homework assistance wanted: using a class as a vector
    By DHart07 in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2010, 02:12 PM
  3. homework help - overloaded constructor taking char or int
    By DHart07 in forum C++ Programming
    Replies: 7
    Last Post: 10-06-2010, 02:57 AM
  4. homework help wanted - Class member functions
    By DHart07 in forum C++ Programming
    Replies: 16
    Last Post: 09-29-2010, 12:43 AM
  5. Replies: 27
    Last Post: 10-11-2006, 04:27 AM

Tags for this Thread