Thread: Need help for a programe in C

  1. #1
    Registered User
    Join Date
    Dec 2015

    Need help for a programe in C

    I need to write a programe that finds and prints every 4 digit numbers that doesn't contain the same numbers.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    We need you to read the forum rules -> Announcements - C Programming
    And post an actual attempt at your homework so we can give you the help you need.

    Note that this is 'need', not 'want'.
    What you seem to want is an answer on a plate. This you won't get from this site.

    "prints every 4 digit numbers that doesn't contain the same numbers."
    A development process
    Break the problem down:
    - print every 2 digit number
    - print every 2 digit number with unique digits.
    It's a much shorter list (100 vs. 10000), so it's easier to work with.

    When you understand how to solve the 2 digit problem, try the 3 digit problem (and then finally, the 4 digit problem)

    Reply with code when you get stuck.
    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.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Ok, then. This link may be of use to you.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Dec 2015
    I dont want ya to do it for me, becouse i want to understand it. The problem is that i dont know how to start. I will look the links you post, thank you. I will get back to you when i try it myself.

  5. #5
    Ticked and off
    Join Date
    Oct 2011
    La-la land
    Solving these kinds of problems always start with the question "How to split this task into steps?"

    This is a skill you can learn. It is based on reasoning (or rationality).

    In its most basic and intuitive form, you first ask yourself, "Exactly what steps would I take, if I were to accomplish this task myself?"

    It is essential to record every single step taken, no matter how intuitive or obvious. Computers are utterly, completely stupid, and need absolutely literal instructions. Your first explanation for this particular problem might be "Well, I'd just write them down", but that does not list the individual steps: exactly how you would decide what to write down.

    Myself, I'd start by considering the rules that apply to each number written. Because we cannot write say twelve as a four-digit number by writing 0012, the most significant/leftmost/thousands digit must be 1 to 9. The three others can be 0 to 9. And, finally, every digit is supposed to occur only once in each four-digit number.

    How, then, would I write down all those numbers?

    Well, I'd start by the smallest digit on the left (one, initially), and fill the three positions left with the smallest digits left unused, writing the smallest number I can that I haven't yet written:
    and so on. (The underlying problem belongs to the area of combinatorics, which tells us the total number of such four-digit numbers is 9󭘸7 = 4536, because there are nine possible leftmost digits, leaving nine possible digits in the hundreds position, eight possible digits in the tens position, and seven possible digits in the rightmost position, in each unique combination.)

    Okay, now lets back up: I said I'd pick the lowest possible digit for the leftmost position, then fill up the three leftover positions with the unused digits. We need to split this into steps, too.

    (At this point, if you have any programming experience, your intuition should say you need nested loops: the outermost for the leftmost digit, an inner loop for the digit in the hundreds position, inside that another for the digit in the tens position, and inside that another for the digit in the ones position, with each iteration of the innermost body printing out one four-digit number.)

    There are two basic ways a human might "[fill] up the three leftover positions": Either they maintain a list of digits on a separate paper, and mark off the digits they already used (but this is cumbersome), or they count from 0 to 9 for each digits position and just reject the cases where that digit already appears in the current number. The former is slow, and you end up writing a lot, but it is purely mechanical; the latter is fast, but you need to reject some candidates, and keep four separate digits in mind at all times.

    (Since there are 9000 four-digit numbers total (1000 to 9999, inclusive), and combinatorics tells us we end up printing 4536 of them, we end up rejecting less than half the candidates. Because generating the candidates is easy -- we just need some loops, no mathematics or anything --, this also means rejecting the candidates should be faster than maintaining a separate list of unused digits, because the latter is nontrivial. If the rules were such that combinatorics told us we'd end up rejecting almost all candidates, then the separate list approach would be better. I'm telling you this, because such decisions are also made based on basic reasoning. We just apply algorithm analysis to tell us how such choices affect the performance of our implementation, then decide on the merits.)

    Okay, we have the basic steps down. (I'm sure I could have written them better, above, but I don't normally do this anymore, as I've trained my brain to do this step-splitting almost automatically. It still takes effort, and is "work", as in exhausting if I go at a hard problem, but I don't need to consciously "drive" it anymore; it happens like driving a bicycle.)

    The next step is to adapt the above to procedural steps. (If we were going to use a functional language, or an object-oriented language like Python with the intent of producing a generator of such numbers, we'd adapt the above to pseudo-functional or pseudo-generator code. Usually the approach in those other languages are so abstract, so far removed from how a normal human would do it, that it's best to take the procedural approach to split the problem down, then translate it to pseudo-non-procedural code, than try to think that way from the get go. That, though, is also a skill you can learn. But, we're dealing with C here, so procedural approach it is.)

    This boils down to writing the steps in language half-way between human description and the programming language. The important bit is that we leave no detail out, nothing implicit, and leverage the structure inherent in programming languages.

    I'm not sure how to boil this part down into instructions, as it's not important by itself, really. It's more like a bridge between the fluid, fuzzy, flexible context-sensitive language humans use on one hand, and on the rigid programming languages for utterly stupid machines on the other. It is mostly useful to convey and clarify ideas. When you get more experienced, you gradually drop this stage, using it only for new, or really hard problems.

    Here's what my pseudocode (for procedural programming languages like C) would look like for this particular problem:
    Loop D1 = 1 to 9, inclusive:
        Loop D2 = 0 to 9, inclusive:
            if D2 == D1, continue (with next D2)
            Loop D3 = 0 to 9, inclusive:
                if D3 == D1, continue (with next D3)
                if D3 == D2, continue (with next D3)
                Loop D4 = 0 to 9, inclusive:
                    if D4 == D1, continue (with next D4)
                    if D4 == D2, continue (with next D4)
                    if D4 == D3, continue (with next D4)
                    print D1*1000 + D2*100 + D3*10 + D4
                End loop
            End loop
        End loop
    End loop
    Basically all procedural languages have two control statements usable in the body of the loops: break, which stops the execution of the loop body and the loop itself, continuing with the statement that follows the loop; and continue, which jumps from the current body of the loop, to the next iteration of the loop starting at the beginning of the loop body.

    Some languages allow break (also called exit in some, but in C it is break) to break out of more than one level of nested loops, but C does not. Some languages call continue cont or next. The behaviour is pretty much the same.

    In C in for loops, the continue statement jumps to the condition part (middle part) of the for statement, and if still true, the increment part (rightmost part) and the loop body. The above pseudocode uses exactly these semantics for continue.

    Because the range is stated as inclusive, the condition uses the <= operator (less than or equal to). If you use < you need to increment the limit by one, because the loop variables are integers and one is the smallest increment possible.

    With these instructions, you should have no problems in writing a C program to accomplish the task. Well, bugs and typos are always possible, and you can write the printf() statement to print each number in many different ways. (If you are unsure about that, write a small, separate test program, where the four digits are constants you decide at compile time, and the program just prints them as a four-digit number. Vary the four digits a few times to make sure it works, and you're golden.)

    If you find it hard to think in these terms, or approach problems in this rational/analytic way, maybe you could ask your mentors/teachers/professors or friends -- or maybe other members here --, for links or suggestions for guides to rational and analytical thinking and approach to problems? It's not really programming-specific, but is more like a general problem-solving approach, but without being able to do the reasoning, it is very hard to become a programmer. I think.

    Finally, some practical advice:

    Always enable your compiler warnings. When you get them, address all of them. Don't add more functionality (more code) before you've fixed all errors and warnings your compiler gives.

    Start by writing a do-nothing program (that compiles and runs without any errors). You might temporarily add a "Hello!" printout or something, to verify the program runs.

    Add only one feature at a time. In this particular case, I'd add only one nested loop at a time.

    Keep a backup copy. Learning how to use git is time well spent; not only does it allow you to easily go back to a previous version, and check exactly what differences you made to break the program between commits, but later on, it also makes it easy to share your code with others, and work on code others have published.

    Add comments to describe what your intent or purpose is for each chunk of code; not what the code does per se.

    Later, when you (or somebody else) reads the code, they can see what the code does; that part is easy, and does not really need any comments. It is the intent or purpose that is hard to divine; the reason why the code does what it does. The best-commented code tells exactly the purpose of the code in the comments. We should all strive for that.

  6. #6
    Registered User
    Join Date
    Dec 2015
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
    int main()
        int num;
        int d1, d2, d3, d4;
        for(num = 1023; num <= 9876; num++)
            d1 = num % 10;
            d2 = ( num / 10 ) % 10;
            d3 = ( num / 100 ) % 10;
            d4 = ( num / 1000 ) % 10;
            if( d1 != d2 && d1 != d3 && d1 != d4 && d2 != d3 && d2 != d4 && d3 != d4 )
                printf("%d ", num);
        getche ();
    So this is it guys thanks for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c programe
    By yousra fatima in forum C Programming
    Replies: 1
    Last Post: 11-13-2011, 05:24 AM
  2. help with a simple programe :)
    By goby in forum C Programming
    Replies: 9
    Last Post: 12-09-2010, 01:49 PM
  3. C programe Exercises?
    By RahulDhanpat in forum C Programming
    Replies: 10
    Last Post: 02-29-2008, 01:26 PM
  4. Programe error
    By colincsf in forum C Programming
    Replies: 8
    Last Post: 12-27-2002, 04:35 PM
  5. Re : Programe for transalation
    By Wah in forum C Programming
    Replies: 1
    Last Post: 01-30-2002, 11:04 AM