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

  1. #1
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55

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

    /*Homework2
    Wednesday, September 21st, 2011 1:02 PM
    Homework 2: The program asks user for a positve integer greater than 0 and less than 10. Program prints out:

    1. whether number is even or odd
    2. cube of number
    3. square root of number
    4. Sum of the digits
    5. factorial

    If an unsuitable number is entered, program exits only printing that an invalid number was entered.*/


    Code:
    #include <stdio.h>
    #include <math.h> /* for sqrt*/
    
    
    /* ask user to input a number between 1 and 10.
    program will check number and terminate if input invalid
    Otherwise the program will prnt out if the number is odd or even
    value cubed, sqare root of the number, sum of the digits and factoral*/
    
    int main()
    {
        int number, i, sumx, productx, cubed, sqrt, factoral;
        printf(" enter a postive number between 1 and 10:");
        scanf("%d", &number);
        /* if ((number > 0 ) && (number < 11)).....*/
        
        { number/2 = even
        printf ("the number %d is even\n");
        else
        printf ("the number %d is odd\n"); /* 1 */ 
        }
        
        
        {cubed = (number*number*number)
        printf (" the number cubed is %d\n"); /*2*/
        }
        
        {sqrt = (sqrt(number))
        printf (" the number square root is %d\n"); /*3*/
        }
        
        {sumx=0;
        for (i=1;i<=number;i=i+1) 
    
        sumx =sumx+ i;
        printf("the sum of the digits is %d\n", sumx); /* 4 */
        
        for (i=1; i<= number; i=i+1)
        productx=productx*i;
        }
        
        {factoral=0;
        for (i=1;i<=number;i=i*1) 
    
        factoral =factoral* i;
        printf("factoral of this is %d\n", factoral); /* 5 */
        
        for (i=1; i<= number; i=i*1)
        factoral=factoral*i;
        }
    
      system("PAUSE");    
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    What is your question? Does the program work as expected or do you have some problem with it, if so what kind of problem?

  3. #3
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    it doesn't work... I can get only the sum of the numbers to work if I take all the rest out. I need the program to print 5 answers for each part of the assignment. the user enters if the user enters a number 1 though 10 then the program should print out answers to each of these questions...
    1. whether number is even or odd
    2. cube of number
    3. square root of number
    4. Sum of the digits
    5. factorial

    So lets say the user types in 2 I need it to output
    2 is odd, the cube is 8 , square root is 1, sum of the digits is 3 and the factorial is 2

  4. #4
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    also dunno if this is a factor but I"m using dev C

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Start by commenting out everything but the first part (odd/even), then add them in one after one until they are all working. Looking at the first part:

    Code:
        /* if ((number > 0 ) && (number < 11)).....*/
        
        { number/2 = even
        printf ("the number %d is even\n");
        else
        printf ("the number %d is odd\n"); /* 1 */ 
        }
    That is not even going to compile. The easiest way to determine if a number is uneven is to check if it's rightmost bit is set IMO.

  6. #6
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    what does it mean "it's rightmost bit is set IMO"? I don't think we learned that........

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you have not used bitwise operators then you haven't. But actually you could leave that bit since the first requirement (checking the range of the number) is commented out. So, solve that first. I would look for numbers < 0 and > 10 instead, if 'number' pass that test, print an error message and return 0; which will end the program.

  8. #8
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    I think I get what you mean... but I'm not sure how to do it correctly.... this is my attempt.....
    Code:
    int main()
    {
        int number, i, sumx, productx, cubed, sqrt, factoral;
        printf(" enter a postive number between 1 and 10:");
        scanf("%d", &number);
        if (number > 0);
        printf (" number is not between 1 and 10\n");
        else
        if (number < 11);
        printf (" number is not between 1 and 10\n");
        
        /* if ((number > 0 ) && (number < 11)).....*/
    thanks
    -Cess

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cess View Post
    what does it mean "it's rightmost bit is set IMO"? I don't think we learned that........
    In an integer value the least significant (right most) bit is always 1 for odd numbers and 0 for even numbers... it's a side effect of computers working in binary.

    Code:
    if ((number & 1) > 0)
      printf("%d is odd",number);
    else 
      printf(%d is even", number);
    You can also use
    Code:
    if ((number % 2) > 0)
      printf("%d is odd",number);
    else 
      printf(%d is even", number);
    Subsonics gave you excellent advice about working through it one small bit at a time. That's how we "real" programmers do things... write a given function or section of code and test... work on the next, test again... until it's all working.

    The computer does things in little tiny steps, one at a time... and so should you.
    Last edited by CommonTater; 09-24-2011 at 03:54 PM.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cess View Post
    I think I get what you mean... but I'm not sure how to do it correctly.... this is my attempt.....
    Code:
    int main()
    {
        int number, i, sumx, productx, cubed, sqrt, factoral;
        printf(" enter a postive number between 1 and 10:");
        scanf("%d", &number);
        if (number > 0);
        printf (" number is not between 1 and 10\n");
        else
        if (number < 11);
        printf (" number is not between 1 and 10\n");
        
        /* if ((number > 0 ) && (number < 11)).....*/
    thanks
    -Cess
    Code:
    if ((number < 1) || (number > 10))
     { 
        printf("I specifically said from 1 to 10\n");
        exit(1); 
      }
    
      // rest of program here


    If the number is less than 1 OR the number is greater than 10 the program exits.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > printf ("the number %d is even\n");
    Same mistake as in another thread - you're not supplying a parameter to match the %d

    Mind you, you're using a gcc compiler, so perhaps you can find where in dev-c++ you set the compiler flags.

    Add -W -Wall -ansi -pedantic and then see more error messages to fix.
    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.

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by Cess View Post
    I think I get what you mean... but I'm not sure how to do it correctly.... this is my attempt.....
    Code:
    int main()
    {
        int number, i, sumx, productx, cubed, sqrt, factoral;
        printf(" enter a postive number between 1 and 10:");
        scanf("%d", &number);
        if (number > 0);
        printf (" number is not between 1 and 10\n");
        else
        if (number < 11);
        printf (" number is not between 1 and 10\n");
        
        /* if ((number > 0 ) && (number < 11)).....*/
    thanks
    -Cess
    You have the answer right at the end there, but you need to change it a bit. 'number' can not be both less than 1 and larger than 10, so use OR instead of AND, that is || so:

    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 03:57 PM. Reason: 1-10

  13. #13
    Registered User Cess's Avatar
    Join Date
    Sep 2011
    Posts
    55
    Ok tried fixing it and still getting error.... We haven't used || before so I don't really know how to use it but ya this is what I did and got an error on the return line it says....

    Code:
    #include <iostream>
    using namespace std;
    int main()
        {
         int number;
         printf(" enter a positive number between 1 and 10:");
         if( (number < 1) || (number > 10) ) {
         printf("The number is out of range. (1-10)\n");
        
    system("PAUSE");
    return 0; }

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You're going to get an error alright... you're using C++ headers and C doesn't have the first clue what "namespace" is.

    Plus your brackets are mismatched ...

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to adopt a good indentation style. Watch:
    Code:
    #include <iostream> // this is C++
    using namespace std; // so is this
    int main() // this should really be (void) instead of just ()
    {
         int number;
         printf(" enter a positive number between 1 and 10:");
         if( (number < 1) || (number > 10) ) {
             printf("The number is out of range. (1-10)\n");
        
             system("PAUSE");
             return 0; }
    // where is the matching }
    Count the pairs of braces.


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

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