Thread: Help with homework please

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    Help with homework please

    I need help with below problem. Can somebody help me please ?

    Thank you
    Val

    The Problem
    Your program will prompt the user for a single positive integer greater than 1. If the user does not enter a valid integer, then your program should continue to reprompt the user for a value until a valid integer is entered. Once the integer is read in, your program will print out the following information about the integer:

    1) A list of each of the positive factors of the given integer.
    2) The number of factors the given integer has.
    3) The sum of the factors of the integer.
    4) The product of the factors of the integer.

    The key idea which will help solve this problem is attempting to divide the given integer by each integer in between 1 and itself. If a particular division works "perfectly," then the value you have tried is a factor of the given integer. (Note: This description is intentionally vague so that you have to determine the specifics of the method of solution.)


    Restrictions
    Name the file you create and turn in numbers.c. Although you may use other compilers, your program must compile and run using gcc. If you use your olympus account to work on this assignment, please follow the steps shown in class to create, compile, and test your program. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. You should also include comments throughout your code, when appropriate. If you have any questions about this, please see a TA.

    Chance for Extra Credit
    There is a relationship between the integer entered by the user, the number of factors that integer has and the product of those factors. If you can determine this relationship, you will be eligible for some extra credit for this assignment. Please put your answer in a comment right after your header comment in your program. (Note: If you need to specify a power in the text of this comment, use the ^ symbol. For example, (x+y)^z stands for the quantity of x plus y raised to the z power. If you have any questions about the extra credit, please ask your instructor or TA.)

    Input Specification
    The value the user enters will be an integer. It will be your job to check to see whether this integer is greater than or equal to two or not. If it is, your program should proceed. If it is not, your program should reprompt the user for a value until one greater than or equal to two is entered. You are guaranteed that the integer entered by the user will be such that the product of the factors of the integer will NOT cause an overflow problem for the int data type. In particular, none of the values your program will have to print out will be more than 231-1.

    Output Specification
    Your output should follow the specification below:

    Here is a list of the positive factors of X:
    A B C D

    The number of positive factors of X is Y.
    The sum of the positive factors of X is Z.
    The product of the positive factors of X is W.

    Deliverables
    A single source file named numbers.c turned in through WebCT.

    Output Samples
    Here are three sample outputs of running the program. Note that this set of tests is NOT a comprensive test. You should test your program with different data than is shown here based on the specifications given. The user input is given in italics while the program output is in bold.

    Output Sample #1
    Enter a positive integer greater than one.
    -2
    Sorry, that input is not valid.
    Enter a positive integer greater than one.
    -100
    Sorry, that input is not valid.
    Enter a positive integer greater than one.
    9

    Here is a list of the positive factors of 9:
    1 3 9

    The number of positive factors of 9 is 3.
    The sum of the positive factors of 9 is 13.
    The product of the positive factors of 9 is 27.



    Output Sample #2
    Enter a positive integer greater than one.
    12

    Here is a list of the positive factors of 12:
    1 2 3 4 6 12

    The number of positive factors of 12 is 6.
    The sum of the positive factors of 12 is 28.
    The product of the positive factors of 12 is 1728.

    Output Sample #3
    Enter a positive integer greater than one.
    100

    Here is a list of the positive factors of 100:
    1 2 4 5 10 20 25 50 100

    The number of positive factors of 100 is 9.
    The sum of the positive factors of 100 is 217.
    The product of the positive factors of 100 is 1000000000.

  2. #2
    root
    Join Date
    Sep 2003
    Posts
    232
    What have you tried so far?
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    to twm:

    I can ask the user to input a positive integer greater than 1 but this is as far as I could get. I have no idea how to set up the loops or anything like that.

    Val

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Are you two in the same class?

    Either way, you have to try on your own first, then post your code here when you get stuck and someone will guide you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    root
    Join Date
    Sep 2003
    Posts
    232
    >I have no idea how to set up the loops or anything like that.
    There are relics of the past that can help you in this area. They're called 'Books' and they act like a repository of information. They seem to be categorized by topic (such as Programming, History, etc.), then further categorized by arbitrary names referred to as 'Titles' and then even further by the one that collected the information called an 'Author'. I like to collect these 'Books' and glean information from them like how to use loops and things like that. Though they're hard to get used to, you have to actually grasp a thin rectangle of paper (how primitive!) and manually move your hand to the other side, taking the sheet of paper with you so that you can view the other side and the page that the sheet was covering. It takes some effort to figure out how they work, but I rather like them. They're easier to transport than even the smallest laptop and most of them don't even weight as much. What won't they think of next, eh?

    Warning: 'Books' are hard to grep, and cut/paste doesn't work nearly as well as you would hope.
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    Two of the main loop styles for your level:
    
    do{} while() - do the code then check to see if condition is true
    
    while(){} - check the condition and if true do the code

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by twm

    Warning: 'Books' are hard to grep, and cut/paste doesn't work nearly as well as you would hope.
    Hey, I know the feeling of frustration from not being able to grep a book...

  8. #8
    Registered User
    Join Date
    Sep 2003
    Posts
    22

    Help with home work

    You guys are right by saying, " show us what you've done so far?" but at the same time, i'd implore you to stop insulting the intelligence of most of us, the newbies. We are here to learn from you, the better programmers. Kindly point out what need to be done. E.g "Well you need to try something first and then we'll see where things are going wrong" rather than " Go and read some books!

    Thanks to all of you for your wonderful support ad patience.
    M

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    Thank you dalguy. You really understood my message. I need some guidance in solving this project not somebody to do it for me.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>i'd implore you to stop insulting the intelligence of most of us
    Insulting isn't my intent, but it's difficult to answer questions that are along the lines of "please help me", with no detail of what help is actually required.

    Anyway:
    >>Your program will prompt the user for a single positive integer greater than 1.<<
    printf() a message, then get a number from the user. Stick that section of code in a loop, and repeat until you've got a number you consider to be valid.

    When you've done that, use more loops, and basic maths operators to work out those sums.

    What more help are you looking for at this stage?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User
    Join Date
    Sep 2003
    Posts
    22

    Help me please!

    Hammer,
    Your intention is quite clear, you are very willing to help and you are ready to see that the person who need help make enough effort.
    Vleonte, i hope with the simple instructions given you'd accomplish the task, you might wanna look into any book that you are reading for related examples.

    Basically the algorithm is the most important, take time to form the algorithm for what you want to do, then transformig that into codes won't be too difficult.
    Good luck.
    M

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    Thank you dalguy2004 & hammer . I will try to put it together somehow and I will post the result so you can see what I did.

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    10

    Help pleeeeaaaaase

    The Problem
    Your program will prompt the user for a single positive integer greater than 1. If the user does not enter a valid integer, then your program should continue to reprompt the user for a value until a valid integer is entered. Once the integer is read in, your program will print out the following information about the integer:

    1) A list of each of the positive factors of the given integer.
    2) The number of factors the given integer has.
    3) The sum of the factors of the integer.
    4) The product of the factors of the integer.

    The key idea which will help solve this problem is attempting to divide the given integer by each integer in between 1 and itself. If a particular division works "perfectly," then the value you have tried is a factor of the given integer. (Note: This description is intentionally vague so that you have to determine the specifics of the method of solution.)


    Restrictions
    Name the file you create and turn in numbers.c. Although you may use other compilers, your program must compile and run using gcc. If you use your olympus account to work on this assignment, please follow the steps shown in class to create, compile, and test your program. Your program should include a header comment with the following information: your name, course number, section number, assignment title, and date. You should also include comments throughout your code, when appropriate.

    Chance for Extra Credit
    There is a relationship between the integer entered by the user, the number of factors that integer has and the product of those factors. If you can determine this relationship, you will be eligible for some extra credit for this assignment. Please put your answer in a comment right after your header comment in your program. (Note: If you need to specify a power in the text of this comment, use the ^ symbol. For example, (x+y)^z stands for the quantity of x plus y raised to the z power. If you have any questions about the extra credit, please ask your instructor or TA.)

    Input Specification
    The value the user enters will be an integer. It will be your job to check to see whether this integer is greater than or equal to two or not. If it is, your program should proceed. If it is not, your program should reprompt the user for a value until one greater than or equal to two is entered. You are guaranteed that the integer entered by the user will be such that the product of the factors of the integer will NOT cause an overflow problem for the int data type. In particular, none of the values your program will have to print out will be more than 231-1.

    Output Specification
    Your output should follow the specification below:

    Here is a list of the positive factors of X:
    A B C D

    The number of positive factors of X is Y.
    The sum of the positive factors of X is Z.
    The product of the positive factors of X is W.

    Deliverables
    A single source file named numbers.c turned in through WebCT.

    Output Samples
    Here are three sample outputs of running the program. Note that this set of tests is NOT a comprensive test. You should test your program with different data than is shown here based on the specifications given. The user input is given in italics while the program output is in bold.

    Output Sample #1
    Enter a positive integer greater than one.
    -2
    Sorry, that input is not valid.
    Enter a positive integer greater than one.
    -100
    Sorry, that input is not valid.
    Enter a positive integer greater than one.
    9

    Here is a list of the positive factors of 9:
    1 3 9

    The number of positive factors of 9 is 3.
    The sum of the positive factors of 9 is 13.
    The product of the positive factors of 9 is 27.



    Output Sample #2
    Enter a positive integer greater than one.
    12

    Here is a list of the positive factors of 12:
    1 2 3 4 6 12

    The number of positive factors of 12 is 6.
    The sum of the positive factors of 12 is 28.
    The product of the positive factors of 12 is 1728.

    Output Sample #3
    Enter a positive integer greater than one.
    100

    Here is a list of the positive factors of 100:
    1 2 4 5 10 20 25 50 100

    The number of positive factors of 100 is 9.
    The sum of the positive factors of 100 is 217.
    The product of the positive factors of 100 is 1000000000.


    This is what I wrote so far. I do not know how to set up to count the number of factors. Can somebody look over my code and tell me if I have any mistakes please. The code is attached.

    Thank you

  14. #14
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    First off you must remember indentation. It is so important.
    Code:
    #include < stdio.h >
    int main ()
    {
      int N, a, f, sum, prod;
      printf (" Enter a positive integer greater than one:\n");
      scanf ("%d", &N);
    
      if ( N < 2)
        {
        printf (" Please enter a positive integer greater than one:\n");
        }
      else
        {
        for ( a >= 1 && a =< N; N % a == 0; a++)
        f= N % a;
        printf (" Here is the list of the positive factors of %d : %d ", N, f);
        }
    
      for ( sum = 0; sum = sum + f, sum++ )
        {
        printf ("The sum of the positive factors of %d is %d .\n", N, sum);
        }
    
      do
        {
        prod = 1;
        prod = prod * f;
        }while ( prod <= (2^31-1));
    
      printf ( "The product of the positive factors of %d is %d.\n", N, prod);
    
    return 0;
    }
    Ok now to tear it apart

    Code:
    #include < stdio.h >
    my compiler yelled at me for the spaces. needs to be <stdio.h>

    Code:
     printf (" Enter a positive integer greater than one:\n");
      scanf ("%d", &N);
    
      if ( N < 2)
        {
        printf (" Please enter a positive integer greater than one:\n");
        }
      else
        {
        for ( a >= 1 && a =< N; N % a == 0; a++)
          {
          f= N % a;
          }
        printf (" Here is the list of the positive factors of %d : %d ", N, f);
        }
    Put the scanf and the if statement in a do while loop. That way you can force the user to input a valid number. Take the stuff in the else and put it after the loop since you will then know the number is valid.

    Code:
    for ( a >= 1 && a =< N; N % a == 0; a++)
    This is horribily wrong. The first part of the for loop is assigning a value and is not a conditional statement.

    Code:
        for ( a >= 1 && a =< N; N % a == 0; a++)
          {
          f= N % a;
          }
    The for loop is assigning the reminder of the integer divison to f, but it does nothing with it, why?

    Code:
      for ( sum = 0; sum = sum + f, sum++ )
    while this might be syntaxally correct it will give you an infinate loop. How can sum ever equal itself plus another number? Remember it reevaulates the left and right value of the conditional statement on every iteration.

    Code:
      do
        {
        prod = 1;
        prod = prod * f;
        }while ( prod <= (2^31-1));
    Why are you making the program do unnessary math? Since (2^31)-1 is a constent why not just put the value in yourself? (2147483647)

    One thing I forgot to add: Try compiling your program and running it, lots of answers can be found that way.
    Last edited by Thantos; 10-12-2003 at 11:13 AM.

  15. #15
    Registered User
    Join Date
    Oct 2003
    Posts
    10
    But how do I make the program to count. Can somebody outline exactly how exactly to change it.


    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum C Programming
    Replies: 10
    Last Post: 09-27-2001, 04:49 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM