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

  1. #31
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    You need to assign the value with =, which you aren't doing, and you don't even have it compilable at the moment:
    Code:
    //cube number
    number = number * number * number;

    Quzah.
    Which is going to alter the value of number for the next function.

  2. #32
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Which is going to alter the value of number for the next function.
    That's true. I was only solving the immediate problem at hand. Your in-printf solution is more elegant.


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

  3. #33
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Cess View Post
    ok I'm getting closer... I got 1 now I'm on number 2 and I need help again Lol thank you guys for being patient and helpful to a newbie like me!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    
    int main()
    {    
         int number;
         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");
         
         /* 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 */
          number*number*number
          printf("%d is the cube of that number\n"); 
          
        
    system("PAUSE");
    return 0;
    }
    Wasn't you also suppose to quit, if a number isn't 1-10? Otherwise you are just carrying on as if nothing happened.

    Quote Originally Posted by Cess View Post
    If an unsuitable number is entered, program exits only printing that an invalid number was entered.*/
    Code:
         if( (number < 1) || (number > 10) ) {
            printf("The number is out of range. (1-10)\n");
            return 0;
         }
    Last edited by Subsonics; 09-24-2011 at 05:24 PM. Reason: In your case you might also need system() in there.

  4. #34
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    That's true. I was only solving the immediate problem at hand. Your in-printf solution is more elegant.


    Quazh.
    Be still my fluttering heart... A compliment from Quzah ... now when's the last time that happened

  5. #35
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    you are totally correct I didn't realize that thanks... I'm working on square root right now but I will go back and try and fix that.

    this is what I have for square root
    Code:
     /* square root the number #3 */
          sqrt=sqrt(number);
          printf("%d is the square root of that number\n",sqrt);

  6. #36
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cess View Post
    you are totally correct I didn't realize that thanks... I'm working on square root right now but I will go back and try and fix that.

    this is what I have for square root
    Code:
     /* square root the number #3 */
          sqrt=sqrt(number);
          printf("%d is the square root of that number\n",sqrt);
    Nope... look how I did it in message #30 ... copy the form of that, using the new math.

    Also note that sqrt() is for floating point numbers and you're working with integers.
    (You really gotta get into the habit of looking this stuff up!)

    And, you can't use function names as variables...

  7. #37
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    there is something wrong again... and I can't figure out why its doing it....

    this is my program so far....
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
     
    
    int main()
    {    
         int number, cube, sqrot ,i ,sumx ,fact ;
         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("square root of that is %d\n",sqrot);
          
          /* Sum of the digits #4 */
          sumx=0;
        for (i=1;i<=number;i=i+1)
        sumx =sumx + i;
        printf("the sum of the digits is %d\n", sumx);
    
        /* Factorial of the number #5 */
        fact=0;
        for (i=1;i<=number;i=i+1)
        fact=fact*i
        printf("the factorial of that number is %d\n",fact);
        
    system("PAUSE");
    return 0;
    }
    it works fine until I added the last part to get it to do the factoral

    Code:
      /* Factorial of the number #5 */
        fact=0;
        for (i=1;i<=number;i=i+1)
        fact=fact*i
        printf("the factorial of that number is %d\n",fact);
    when that is added it jumps back up to the sqrot and says it has an error? Grrr I will get this program right it might take me all night but ya!!!! thanks again for help.

  8. #38
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... what EXACTLY is it doing or not doing?
    What error messages are you getting?
    What warnings/errors are your compiler listing?

    All this is good information... "It doesn't work" tells us nothing.

  9. #39
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You are missing a semicolon after fact*i.

  10. #40
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    true it says
    In function 'int main()';
    line 28 waring converting int to double
    expected' before "printf"
    [build error] error 1

    I'm guessing its because I didn't use a double of the sqrt which is weird because it worked before and only stopped working when I added the factorial. Can I use int and double for the number they input? If so how do I do that correctly so it works the way I want it to?
    -thanks again Cess

  11. #41
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    lol I can't believe I missed that simple ;

    shoot
    its sayin the factorial is zero.... so I'm still doing something wrong
    I need it to give the factorial of the person's input
    Code:
     /* Factorial of the number #5 */
        fact=0;
        for (j=number;j<=number;j=j+1);
        fact=fact*j;
        printf("%d is the factorial\n",fact);
    Last edited by Cess; 09-24-2011 at 06:28 PM.

  12. #42
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You initialize fact to zero, then you multiply that with i. What happens when you multiply something with zero?

  13. #43
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    ok now I'm getting 6....I don't understand how I am spost to set it up differently
    Code:
      /* Factorial of the number #5 */
        fact=number;
        for (j=1;j<=number;j=j+1);
        fact=fact*j;
        printf("%d is the factorial\n",fact);
    and I get 3 when I entered 2 and its spost to be 2....
    Code:
     /* Factorial of the number #5 */
        fact=1;
        for (j=1;j<=number;j=j+1);
        fact=fact*j;
        printf("%d is the factorial\n",fact);

  14. #44
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It's the semicolon again, your for loop runs empty. what you have is the same as:

    Code:
    for(j = 1; j <= number; j=j+1)
         ;

  15. #45
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    I am still not understanding.....

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