×

# To find the next permutation

 0 Input format ; enter N(<1000) enter K(<=10) the next k lines each contain a permutaion of 1 to n ; Output format; K lines each containg the lexicographically next permutation (entered in input) There seems to be some small error in my code (using Dev C++ ) Please help me to find it out.  #include #include using namespace std; int main() { int ar[1000][10],N,K,x,i,j,k,max,trig,replace=1001; cin >> N; cin >> K; for(i=0;i> ar[i][j]; } }//end of outer most for for(i=0;i=0;j--) { if(ar[i][j]>max) { max=ar[i][j];//finding a max to whom a minimum can be found } else { trig=ar[i][j]; for(k=j+1;ktrig && ar[i][k]

 4 I haven't really studied the program for logic errors. But here are my two cents of advice. You have stated N<1000 and k<=10 but you seem to have coded for the reverse. You are setting max=ar[i][N]; but there is no value in ar[i][N] really so it picks up a stray value or results in a segmentation fault. Replace this with max = -1. Also consider the change suggested by mkagenius. The second condition in ar[i][k]>trig && ar[i][j] < replace as it is now in your code is always true but I doubt if that is the real intended behaviour. If after considering these changes still if your code is not behaving as expected then let us know but this time it would be better if you can also provide the input for which it is failing. EDIT: Hope you were writing this code just for learning purpose. In reality you would never need to write a code for lexicographically next permutation. You can simply use next_permutation available in STL. answered 12 Mar '12, 02:50 4★gultus ♦ 1.5k●1●13●25 accept rate: 51% Updated a test case in the question and yes i am writing this code for learning purpose. There still seems to be something wrong with the code :( (12 Mar '12, 12:42) @anmolsood It would obviously not work as expected for the input you have provided since you coded for N<=10 and K<=1000 while you are now inputting N=50. Make the change int ar[10][1000] instead of int ar[1000][10] this will resolve the issue. (12 Mar '12, 20:22) gultus ♦4★ @guitus Thanks . That resolves the issue . I am such a noob. (12 Mar '12, 22:27) @anmolsood , he is gultus not guitus, see u don't pay attention to small things. (just kidding as you are in school. All the best for your future.) (12 Mar '12, 22:33)
 1 First of all FOR GOD sake indent your code properly. may be this is the error- if(ar[i][k] > trig && ar[i][k] < replace) (and yes, also put spaces where needed, like before and after "=" sign. etc.) answered 11 Mar '12, 20:07 303●4●9 accept rate: 25% 1 Sorry about indentation , I am new to programming. i can't find error in the line you mentioned. My purpose for that if condition is to find the smallest value which is greater than trig so that it can swapped with trig. (11 Mar '12, 20:59) Hey dude, where is your concentration? Notice :my line---> if(ar[i][k] > trig && ar[i][*k*] < replace) , and your line----> if(ar[i][k] > trig && ar[i][j] < replace) , can you find the difference ? (12 Mar '12, 01:16) 2 @mkagenius be little soft on noob mistakes, no point using such harsh words. If people do repeat the mistakes then I can understand the usage of harsh words. (12 Mar '12, 02:52) gultus ♦4★ @mkagenius && @gultus :- Thanks a lot and Sorry. I made the changes and yet the code is not working perfectly for large values of n(let's say 50). The code seems to work perfectly fine for small values. (12 Mar '12, 12:37) @gultus , u rock! m/ (12 Mar '12, 22:27)
 0 answered 06 Feb '17, 12:28 -83 accept rate: 0%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,901
×591

question asked: 10 Mar '12, 23:54

question was seen: 11,218 times

last updated: 06 Feb '17, 14:06