Thread: Combination Crash

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    His initial question says permutations. Some basic logic says combination. If he is using it for something like lottery numbers, permutations would ONLY be relevant for calculating the odds of a given sequence occuring, however using some basic statistics can resolve finding the odds without physically generating each permutation then evaluating it. I hate to speak on the OP's behalf, but I think the OP could produce vastly more optimal code by just doing combos.

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    22
    Thanks Master...... Wow, I sound like a Kung Fu dub. Being a bud of a master actually gives me some confindence. It sounds sarcastic but I'm serious. I guess my mum was right I really should have fixed up. If I did I wouldn't be single now. Anyways, please forgive me for this and notice that this time the lowest number is 0 and not 1.

    [code]
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    using namespace std;
    int main () {
    int P [] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int ka [56] = {};
    int L, H, z, f, y, x, p, k, a, c;
    string line;
    z=10; p = 0;
    ofstream myfile ("Factorial.txt"); if (myfile.is_open())
    cout << "Please type the the highest number: ";cin >> L; ++L;
    loop: y=z; while (P [z] != L) {
    H = 0; a = z; c = (z-1); f = 55;

    ka[1] = (P[1]-P[0]);
    ka[2] = (P[2]-P[0]);
    ka[3] = (P[3]-P[0]);
    ka[4] = (P[4]-P[0]);
    ka[5] = (P[5]-P[0]);
    ka[6] = (P[6]-P[0]);
    ka[7] = (P[7]-P[0]);
    ka[8] = (P[8]-P[0]);
    ka[9] = (P[9]-P[0]);
    ka[10] = (P[10]-P[0]);

    ka[11] = (P[2]-P[1]);
    ka[12] = (P[3]-P[1]);
    ka[13] = (P[4]-P[1]);
    ka[14] = (P[5]-P[1]);
    ka[15] = (P[6]-P[1]);
    ka[16] = (P[7]-P[1]);
    ka[17] = (P[8]-P[1]);
    ka[18] = (P[9]-P[1]);
    ka[19] = (P[10]-P[1]);

    ka[20] = (P[3]-P[2]);
    ka[21] = (P[4]-P[2]);
    ka[22] = (P[5]-P[2]);
    ka[23] = (P[6]-P[2]);
    ka[24] = (P[7]-P[2]);
    ka[25] = (P[8]-P[2]);
    ka[26] = (P[9]-P[2]);
    ka[27] = (P[10]-P[2]);

    ka[28] = (P[4]-P[3]);
    ka[29] = (P[5]-P[3]);
    ka[30] = (P[6]-P[3]);
    ka[31] = (P[7]-P[3]);
    ka[32] = (P[8]-P[3]);
    ka[33] = (P[9]-P[3]);
    ka[34] = (P[10]-P[3]);

    ka[35] = (P[5]-P[4]);
    ka[36] = (P[6]-P[4]);
    ka[37] = (P[7]-P[4]);
    ka[38] = (P[8]-P[4]);
    ka[39] = (P[9]-P[4]);
    ka[40] = (P[10]-P[4]);

    ka[41] = (P[6]-P[5]);
    ka[42] = (P[7]-P[5]);
    ka[43] = (P[8]-P[5]);
    ka[44] = (P[9]-P[5]);
    ka[45] = (P[10]-P[5]);

    ka[46] = (P[7]-P[6]);
    ka[47] = (P[8]-P[6]);
    ka[48] = (P[9]-P[6]);
    ka[49] = (P[10]-P[6]);

    ka[50] = (P[8]-P[7]);
    ka[51] = (P[9]-P[7]);
    ka[52] = (P[10]-P[7]);

    ka[53] = (P[9]-P[8]);
    ka[54] = (P[10]-P[8]);

    ka[55] = (P[10]-P[9]);


    while ( ka[f] != 0 ) { if (ka[f] == 0) {++H, --f;} else --f; }

    while ( c != -1) { if (P[a] <= P[c]) {++H, --a, --c;} else {--a, --c;} }

    if (H == 0) {myfile << P[0] << "\t" << P[1] << "\t" << P[2] << "\t" << P[3] << "\t" << P[4] << "\t" << P[5] << "\t" << P[6] << "\t" << P[7] << "\t" << P[8] << "\t" << P[9] << "\t" << P[10] << "\n";};
    if (k != 0) {++P [z], p = 0;} else p = 0; }; while (y != 0)
    if ( P [y] != L) goto endloop; else P [y]=0, --y, ++P [y]; goto endloop;
    endloop: while (P[0] != L) {goto loop;} myfile.close(); return 0;}
    [\code]

    The problem shows here. If I enter 12 this happens

    Code:
    0	1	2	3	4	5	6	7	8	9	10
    0	1	2	3	4	5	6	7	8	9	11
    0	1	2	3	4	5	6	7	8	9	12
    0	1	2	3	4	5	6	7	8	10	11
    0	1	2	3	4	5	6	7	8	10	12
    0	1	2	3	4	5	6	7	8	11	12
    0	1	2	3	4	5	6	7	9	10	11
    0	1	2	3	4	5	6	7	9	10	12
    0	1	2	3	4	5	6	7	9	11	12
    0	1	2	3	4	5	6	7	10	11	12
    0	1	2	3	4	5	6	8	9	10	11
    0	1	2	3	4	5	6	8	9	10	12
    0	1	2	3	4	5	6	8	9	11	12
    0	1	2	3	4	5	6	8	10	11	12
    0	1	2	3	4	5	6	9	10	11	12
    0	1	2	3	4	5	7	8	9	10	11
    0	1	2	3	4	5	7	8	9	10	12
    0	1	2	3	4	5	7	8	9	11	12
    0	1	2	3	4	5	7	8	10	11	12
    0	1	2	3	4	5	7	9	10	11	12
    0	1	2	3	4	5	8	9	10	11	12
    0	1	2	3	4	6	7	8	9	10	11
    0	1	2	3	4	6	7	8	9	10	12
    0	1	2	3	4	6	7	8	9	11	12
    0	1	2	3	4	6	7	8	10	11	12
    0	1	2	3	4	6	7	9	10	11	12
    0	1	2	3	4	6	8	9	10	11	12
    0	1	2	3	4	7	8	9	10	11	12
    0	1	2	3	5	6	7	8	9	10	11
    0	1	2	3	5	6	7	8	9	10	12
    0	1	2	3	5	6	7	8	9	11	12
    0	1	2	3	5	6	7	8	10	11	12
    0	1	2	3	5	6	7	9	10	11	12
    0	1	2	3	5	6	8	9	10	11	12
    0	1	2	3	5	7	8	9	10	11	12
    0	1	2	3	6	7	8	9	10	11	12
    0	1	2	4	5	6	7	8	9	10	11
    0	1	2	4	5	6	7	8	9	10	12
    0	1	2	4	5	6	7	8	9	11	12
    0	1	2	4	5	6	7	8	10	11	12
    0	1	2	4	5	6	7	9	10	11	12
    0	1	2	4	5	6	8	9	10	11	12
    0	1	2	4	5	7	8	9	10	11	12
    0	1	2	4	6	7	8	9	10	11	12
    0	1	2	5	6	7	8	9	10	11	12
    0	1	3	4	5	6	7	8	9	10	11
    0	1	3	4	5	6	7	8	9	10	12
    0	1	3	4	5	6	7	8	9	11	12
    0	1	3	4	5	6	7	8	10	11	12
    0	1	3	4	5	6	7	9	10	11	12
    0	1	3	4	5	6	8	9	10	11	12
    0	1	3	4	5	7	8	9	10	11	12
    0	1	3	4	6	7	8	9	10	11	12
    0	1	3	5	6	7	8	9	10	11	12
    0	1	4	5	6	7	8	9	10	11	12
    0	2	3	4	5	6	7	8	9	10	11
    0	2	3	4	5	6	7	8	9	10	12
    0	2	3	4	5	6	7	8	9	11	12
    0	2	3	4	5	6	7	8	10	11	12
    0	2	3	4	5	6	7	9	10	11	12
    0	2	3	4	5	6	8	9	10	11	12
    0	2	3	4	5	7	8	9	10	11	12
    0	2	3	4	6	7	8	9
    The program will stop at that 9. The rest of that, and no other premutations, will be printed. Thats the problem. Its got nothing to do with time, it just stops there. It always does.
    Last edited by koxson; 11-03-2008 at 08:04 PM.

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    22
    P.S The last thing I should see is

    2 3 4 5 6 7 8 9 10 11 12

    I'm sure you've gathered I stole the word premutations from you guys. Sorry for confusing you.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's just where your file buffer ends; either the program crashes or spins to a halt before it fills up another buffer. (If you printed to the screen, (1) things would go by pretty quickly and (2) you'd have a better idea of where things actually stop.)

    Anyway, I'm not going to try to pick apart that algorithm, but I will congratulate you on getting what appears to be a working implementation (for the most part). Perhaps between what I posted and the output you can see how to do it without 56 auxiliary variables.

  5. #20
    Registered User
    Join Date
    May 2008
    Posts
    22
    How do I deal with the buffer problem as I need to print a list to txt and not to the screen?

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You want a buffer when you write to a file. It just screws with your debugging. So print to the screen until the debugging is done, then go back to the text file.

    (Or alternatively, fflush(myfile) after every line you write. Don't blame me when it takes three times as long for your program to run.)

  7. #22
    Registered User
    Join Date
    May 2008
    Posts
    22
    Thanks, I'm fine with it taking 3 or even 10 times as long to run as long as it does run and print all the lines. However, I don't know how to use fflush. I took a long shot and tried.
    Code:
    if (H == 0) {myfile << P[0] << "\t" << P[1] << "\t" << P[2] << "\t" << P[3] << "\t" << P[4] << "\t" << P[5]
    << "\t" << P[6] << "\t" << P[7] << "\t" << P[8] << "\t" << P[9] << "\t" << P[10] << "\n"; fflush (myile);}
    However, Dev c++ told me that line wouldn't get me anywhere. *Sigh* sometimes I think source codes are secretly women that get off on tellimg me no.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you try myfile instead of myile? Wait -- we're in C++. If this is an fstream, then you would call myfile.flush().

  9. #24
    Registered User
    Join Date
    May 2008
    Posts
    22
    haha, you were right I can't believe I typed myile. myfile didn't work, however, I think myfile.flush() is. You have my thanks. I'm trying it out.

  10. #25
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    Quote Originally Posted by master5001 View Post
    I didn't have that hard of a time following the question.
    You're my hero.

  11. #26
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why thank you. I am sorry for not reposting kosson's code reformatted for the rest of the world's viewing pleasure. Alas I had to scamper off to my Spanish class moments after he posted.

  12. #27
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by master5001 View Post
    You wouldn't Pee me off.. I would simply recorrect it again and know that your knowledge of my thoughts is duly noted. I do not take much to heart, bud.
    Ah then perhaps you're an ISTJ personality type as well ?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hooking Crash?
    By Elysia in forum Windows Programming
    Replies: 9
    Last Post: 03-15-2008, 01:13 PM
  2. Can not debug a crash
    By hannibar in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2007, 10:02 AM
  3. Dynamic array sizing causes crash
    By Mithoric in forum C++ Programming
    Replies: 3
    Last Post: 12-30-2003, 07:46 AM
  4. FYI: asctime(gmtime(&mytime)) = crash!
    By anonytmouse in forum C Programming
    Replies: 2
    Last Post: 09-29-2003, 02:24 AM
  5. Crash after crash!
    By Dual-Catfish in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2002, 09:32 AM