Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Trying to print combinations of numbers(C++)

  • 23-08-2001 2:04am
    #1
    Registered Users, Registered Users 2 Posts: 2,364 ✭✭✭


    Hi.
    I am trying to write a program to print to file all 9 digit combinations possible using the numbers 1, 2, 3, 4, 5, 6,7 8 and 9 using each number just once.
    The code below will do all the combinations but repetes numbers.
    ie. it will print 112233445, which I don't want.

    Any ideas on how to skip these numbers?
    #include <iostream>
    
    void main(void)
    {
    	cout<< "Starting calculations....\n";
    	{
    		unsigned long num=0;
    		int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0;
    
    			{
    				for(a=1;a<=9;a++)
    				for(b=1;b<=9;b++)
    				for(c=1;c<=9;c++)
    				for(d=1;d<=9;d++)
    				for(e=1;e<=9;e++)
    				for(f=1;f<=9;f++)
    				for(g=1;g<=9;g++)
    				for(h=1;h<=9;h++)
    				for(i=1;i<=9;i++)
    				for(j=1;j<=9;j++)
    			{
    			
    				cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<j<<"\n";
    				num++;
    			
    			}
    			}
    
    		cout <<"\n   "<<num;
    		
    	}
    
    }
    


Comments

  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    Right,

    I was warned before not to solve ppl's problems for them, by giving them the answer, so I'm not going to. But what I will do is give you a some good pointers. smile.gif

    Solving this problem obviously incorporates the use of an integer array. If you don't know what that is, look it up. What you do is you cycle through the array using a loop and use different pattern each time. This means using nested for loops.

    so your printing line will look like this. Actually in the below example you are just printing to the console window. Since you are not taking an object oriented approach to solving this problem we will import an ANSI C function library <stdio.h> and call the fopen() function to open a file, and the fwrite() function to write to a file. Look up how to use these functions.

    also I noticed that inside a for loop code block you are incrementing a variable (num++), you could simply use this instead
    for(j=1;j<=9;j++,num++)

    Hope this helps m8 wink.gif

    ;-phobos-)


  • Registered Users, Registered Users 2 Posts: 2,364 ✭✭✭Mr. Flibble


    This isn't homework, don't worry(who gets HW in the middle of summer anyway). Its just a problem I've being to solve to help answer an Enigma in the NewScientist magazine.


  • Registered Users, Registered Users 2 Posts: 2,364 ✭✭✭Mr. Flibble


    Thanks phobos smile.gif

    I know all(well, a bit) about arrays and fwrite etc. Do you mean for me to use an array to hold all of the combinations? I don't know if an array would be able to hold that much data(there are going to be something like 3 billion combinations) so I was going to just save the answers to a file instead.

    If I can just get it to start printing the answers on the console I'll be able to work out how to get them into a file later.

    I just don't know how to stop it printing answers with repeted digits.



  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    No, phobos means store the nos 1 to 9 in an array. In java it would be
    int arrayOfNums = new int[9];
    for (int i = 0; i &lt;=8; i++) {
         arrayOfNums[i] = i + 1;
    }
    

    And then write an algorithm to display the 9! possible arrangments of the array (362880 nos)

    When I was young, I dreamed of being a Fireman. Then I learned that they put out the fires.


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    <snip>whoops.</snip>
    You could use 9 nested for loops but there's a far more elegant way of doing it. (I think smile.gif)

    [This message has been edited by Enygma (edited 23-08-2001).]


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,364 ✭✭✭Mr. Flibble


    <font face="Verdana, Arial" size="2">
    And then write an algorithm to display the 9! possible arrangments of the array (362880 nos)
    </font>

    Easy when you know how smile.gif

    care to share? smile.gif


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    wait til sunday(after my exams), and I might start working on it, just aint got the time now. Just a thought......use a Random no. generator, i.e. for digit 1, generate a random no between 1 and 9 inclusive. then for digit 2, do the same, but if the same no. appears again, discard it and choose another. do this for all nine digits. Then repeat 362,800 times, checking each new generated no. against its predecessors, lol. That should keep your computer busy until Sunday.

    When I was young, I dreamed of being a Fireman. Then I learned that they put out the fires.


  • Closed Accounts Posts: 10 eof


    This should do it, I think.

    I'm sure there's faster ways of doing it.
    #include &lt;fstream.h&gt;
    
    /*
     * Eoin O Fiachain
     * eoin@zion.nuigalway.ie
     * 24/08/01
     */
     
    #define MAX 9
    
    void pstuff(ofstream &outfile, int level, int dest_array[], int choice_array[])
    {
    	if(level &lt; MAX)
    	{
    		int r = MAX-level;
    		for (int i=0; i&lt;r; i++)
    		{
    			  int temp = choice_array[i];
    			  dest_array[level] = choice_array[i];
    			  choice_array[i] = choice_array[r-1];
    			  pstuff( outfile, level+1, dest_array, choice_array);
    			  choice_array[i] = temp;
    		}
    
    	} else {
    
    		for (int i=0; i&lt;MAX; i++) {
    			outfile &lt;&lt; dest_array[i];
    		}
    
    		outfile &lt;&lt; "\n";
    
    	}
    
    }
    
    void main()
    {
    	int empty_array[9];
    	int choice_array[9];
    	
    	for (int i=0; i&lt;9; i++) choice_array[i] = i+1;
    	
    	ofstream outfile("list.txt");
    	pstuff(outfile, 0, empty_array, choice_array);
    	outfile.close();
    }
    



    [This message has been edited by eof (edited 24-08-2001).]


  • Closed Accounts Posts: 1,651 ✭✭✭Enygma


    Mmmm, not good, give the guy a tip or something, let him figure it out himself. Posting code is no good.

    <story>
    This kids parents make him do his homework straight after school. Anytime he gets anything wrong, they correct him, until he gets it right. The next day when he brings his homework to school his teacher marks it and he gets 100%, 10 out of 10, perfect. The teacher can't believe it. Wow, this kid can do anything.

    Two months later:
    Summer exams, the kid f*cks up, majorly. The kid doesn't have a clue. This kid is smart, this kid realises that you'll provide the answers if he keeps fscking up.
    <moral>
    Don't give people f*cking answers give them help!
    </moral>
    </story>


  • Closed Accounts Posts: 10 eof


    Respectfully, I don't think its my responsibility to decide by which process a chap learns.

    Surely thats his own responsibility?

    He asked how to do something. I gave him an example.

    While I accept your point, I can't help but feel that the Internet would be a far worse place if others started deciding what was in best interest for individuals, and restricting information on such a basis.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Well if he wanted a full solution he could of done a search on the net.

    Giving someone a full working solution to thier problem is not the correct way to teach someone. 9 times out of 10 a person will copy the code and not even bother to understand what it is doing.

    I have also seen people with clearly no concept of how to program pass thier exams, get good paying jobs by copying other peoples work.



  • Closed Accounts Posts: 10 eof


    <font face="Verdana, Arial" size="2">Giving someone a full working solution to thier problem is not the correct way to teach someone. 9 times out of 10 a person will copy the code and not even bother to understand what it is doing</font>

    Perhaps so. However, its the responsibility of the individual to not copy the code, not us. Who exactly do we think we are to say we know better than the person themselves, when we don't really know the first thing about him?

    Fair enough school children might need firmer monitoring, and guidance - but we cannot possibly determine what it suitable for an individual, from the minimal information available to us on a list.

    You claim that if Mr. Fibble wanted a solution he could search the net, but equally so, if he doesn't want a solution he can just ingore the code. Ok, the presence of the solution might tempt him slightly, but thats a small price to pay for the potential benefits from learning from it.

    All I'm saying is that we shouldn't take it upon ourselves to decide what and what is not appropriate to show others, and what is and isn't the correct method of teaching, when we really don't know much about others. The best judge is the person themselves (parents deputize somewhat in the case of young children, but by the time most ppl are programming they are far beyond that stage).

    If I posted a similar question and I got fully working source code as a reply, I'd be delighted and savour the learning opportunity. I really don't think that in such a case that the suggested recommended course of avoiding giving source, would benefit me.

    Different strokes for different folks - but let the each person decide what it wants for himself, instead of incurring the consequences of the self-appointed moral majority of boards.ie making the decision for him.

    Saying that, I do genuinely apologise to Mr. Fibble if I have set back your learning process. I can assure you that it was not my intention.


    [This message has been edited by eof (edited 25-08-2001).]


  • Closed Accounts Posts: 1,322 ✭✭✭phobos


    That is the most logical thing I've read all week. A fair arguement!

    Nice one eof

    ;-phobos-)


  • Registered Users, Registered Users 2 Posts: 2,364 ✭✭✭Mr. Flibble


    Thankyou for the code eof. I'm off to Greece in a few hours but will try it out when I get back.

    Enygme, don't worry about eof hindering my learning. I am not just asking for the code to copy it and hand up some answer sheet. I am doing this programming for my own personal enjoyment - we don't do any programming in my course in college.

    back in 10 days - I'll let u know then if I get the prog working.


Advertisement