HELP!

Discussion in 'C Programming' started by luis guballo, Aug 2, 2013.

  1. luis guballo

    luis guballo Guest

    Please edit the codes the I've written here for me to have the correct answer I need. Anyways this program aims to give all the factors of a given positive integer. Example, I entered 6. Then it should output 1,2,3 as its factors.

    Here's the code:

    #include <stdio.h>
    #include <conio.h>
    int main ()
    {
    int num, rem, factor = 1, ctr = 1;
    printf("Enter a number: ");
    scanf("%d", &num);
    if (num < 0 )
    printf("The number you have entered is invalid");
    if (num == 0)
    printf("The factor is %d", factor);
    else
    rem = num % ctr;
    if (rem = 0)
    for(ctr = 1; ctr <= num; ctr++)
    {
    factor = num / ctr;
    printf("The factor: %d", factor);
    }
    else
    ctr = ctr + 1;
    getch();
    return 0;
    }
     
    luis guballo, Aug 2, 2013
    #1
    1. Advertisements

  2. luis guballo

    Eric Sosman Guest

    I'll help with your homework, but I won't write it for you.
    Get rid of this; it's not C.
    Check the value returned by scanf(), which tells you how
    many numbers it managed to convert. For example, if the user
    responds to "Enter a number: " by typing "forty-two", scanf()
    will not understand it and will return 0 to tell you that it
    couldn't convert anything. You shouldn't go on to check `num'
    until you're sure a value has actually been stored in it.

    (General note: Although scanf() looks fairly straightforward,
    it is startlingly difficult to use for interactive input. Use
    it in "toy" programs like this one, but don't expect to be able
    to go much further with it.)
    ... and after this test, the program runs happily onward
    with the invalid `num'. Perhaps you should halt the program
    here if the test fails.
    Here's where the serious difficulty begins. It looks like
    you want to test whether `num' is divisible by `ctr', and keep
    on trying successively larger `ctr' values. But the divisibility
    check is outside the loop that increases `ctr', so it's only made
    for the very first `ctr' value (and always says "Yes, 1 is a
    factor." You need to reorganize the code so that it checks each
    new `ctr' value for divisibility as it's encountered.
    Here's another problem: The `=' and `==' operators mean
    different things. As written, this line sets `rem' to 0 and
    then says "The test always comes out false."
    Since this loop never executes (because of the faulty `if'
    above) you haven't seen that it doesn't work. I'm not sure just
    what you want it to do; perhaps it's a confused attempt at a
    divisibility test.
    Get rid of this; it's not C.
    General outline of a solution to your homework problem:

    Read a value
    if (reading was unsuccessful) {
    emit a complaint
    } else if (the value is invalid) {
    emit a complaint
    } else {
    for (each candidate divisor) {
    if (it divides the value evenly) {
    print it
    }
    }
    }
     
    Eric Sosman, Aug 2, 2013
    #2
    1. Advertisements

  3. luis guballo

    osmium Guest

    I suspect that what your instructor really wants is *prime* factors,
    although your summary does not say that. 4 is a factor of 20, but not a
    prime factor.
     
    osmium, Aug 2, 2013
    #3
  4. Perhaps, but showing all the factors of a number is a reasonable exercise.
     
    Keith Thompson, Aug 2, 2013
    #4
  5. Sure it is. It's just not *portable* C.

    [...]
    Sure it is. It's just not *portable* C.

    The getch() function, declared in <conio.h>, is a Windows-specific
    (also MS-DOS) that prompts the user to type <Enter> to continue.
    It's convenient for certain Windows-based development environments
    in which the easiest way to execute a program after you've compiled
    and linked it creates a new terminal window that vanishes when the
    program finishes. The output of a simple portable "Hello, world"
    program, when executed in such an environment, will appear and vanish
    so quickly you probably won't be able to read it. The "getch()"
    call is a workaround for that.

    The environment *should* provide a way to keep the window open after
    the program terminates, so the program doesn't have to take any
    special action. Unfortunately, as far as I know, typical Windows
    development environments don't do this. (I could easily be mistaken
    on this point.)

    An alternative is to execute the program from a command prompt rather
    than from within the IDE, but that's annoying; you have to figure
    out where the IDE decided to put the executable, and then you have
    to switch back and forth between the IDE and the command prompt.

    Or you can call the standard getchar() rather than getch(), but (a) that
    doesn't show a prompt, and (b) like calling getch(), it makes the
    program wait for input even when it's not necessary.
     
    Keith Thompson, Aug 2, 2013
    #5
  6. Maybe he is trying to find perfect numbers...

    --
    "The smart way to keep people passive and obedient is to strictly limit the
    spectrum of acceptable opinion, but allow very lively debate within that
    spectrum...."

    - Noam Chomsky, The Common Good -
     
    Kenny McCormack, Aug 2, 2013
    #6
  7. I believe you're right. Apparently it reads a single character from the
    keyboard without echoing it. I confused it with system("pause"), which
    is often used on Windows for a similar purpose.

    Thanks for pointing out my mistake -- but telling us what it actually
    does would have been even more useful.

    (There's another function of the same name that's part of curses.)

    A simple and portable getchar() call would serve the same purpose,
    except that it typically requires you to type Enter, since stdin is
    usually line-buffered.
     
    Keith Thompson, Aug 2, 2013
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.