473,804 Members | 3,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to improve this code

Any suggestions welcome. Sorry if google groups screws up the
formatting.
TIA,
MWB

#include <iostream>
#include <list>

using namespace std;

void
print_perm(cons t list<int> &p)
{
list<int>::cons t_iterator t;
for(t=p.begin() ; t != p.end() ; t++)
cout<< *t << ' ';
cout<<endl;
}

void
get_perm(int i,int n,list<int> &p)
{
list<int>::iter ator t=p.begin();
if(i>n) return;
for(int k=0;k<i;k++)
{
p.insert(t++,i) ;
if (i==n) print_perm(p);
else get_perm(i+1,n, p);
p.remove(i);
}
}
void
gen_perm(int n)
{
list<int> p;
get_perm(1,n,p) ;
}

int
main(int argc,char *argv[])
{

int n=5;
gen_perm(n);
return 0;
}

--
Ram lala hum ayenge, mandir wahin banayenge

Feb 4 '06 #1
6 1197
ma************* *******@yahoo.c om wrote:
Any suggestions welcome. Sorry if google groups screws up the
formatting.
Formatting is fine. The code doesn't work. You need to fix
it first. Then you can think of improving it...
TIA,
MWB

#include <iostream>
#include <list>

using namespace std;

void
print_perm(cons t list<int> &p)
{
list<int>::cons t_iterator t;
for(t=p.begin() ; t != p.end() ; t++)
cout<< *t << ' ';
cout<<endl;
}

void
get_perm(int i,int n,list<int> &p)
{
list<int>::iter ator t=p.begin();
if(i>n) return;
for(int k=0;k<i;k++)
{
p.insert(t++,i) ;
If the list 'p' is empty (and that's the case when this function
is called for the first time), 't' evaluates to 'p.end()'. That
iterator is _not_ incrementable.
if (i==n) print_perm(p);
else get_perm(i+1,n, p);
p.remove(i);
}
}
void
gen_perm(int n)
{
list<int> p;
get_perm(1,n,p) ;
}

int
main(int argc,char *argv[])
You don't use those arguments, why declare them?
{

int n=5;
gen_perm(n);
return 0;
}

V
--
Please remove capital As from my address when replying by mail
Feb 4 '06 #2
ma************* *******@yahoo.c om wrote:
Any suggestions welcome. [snip]

[code generating all permutations of (1,2,3,4,5) snipped]

You may want to have a look at the algorithms

std::next_permu tation

and

std::prev_permu tation
Best

Kai-Uwe Bux

Feb 4 '06 #3

Victor Bazarov wrote:
ma************* *******@yahoo.c om wrote:
Any suggestions welcome. Sorry if google groups screws up the
formatting.


Formatting is fine. The code doesn't work. You need to fix
it first. Then you can think of improving it...


Why didn't you actually bother to run the code? You clearly didn't
understand how the code works. I don't mind being criticized, but
please use your head and if that is too difficult for you compile and
run the code before making an idiot of yourself in a public forum.

It works fine for me
Using cygwin

$ g++ -ansi -pedantic -Wall perm.cpp -o perm
$ ./perm.exe > out.txt
$ cat out.txt
5 4 3 2 1
4 5 3 2 1
4 3 5 2 1
4 3 2 5 1
4 3 2 1 5
5 3 4 2 1
3 5 4 2 1
3 4 5 2 1
3 4 2 5 1
3 4 2 1 5
5 3 2 4 1
3 5 2 4 1
3 2 5 4 1
3 2 4 5 1
3 2 4 1 5
5 3 2 1 4
3 5 2 1 4
3 2 5 1 4
3 2 1 5 4
3 2 1 4 5
5 4 2 3 1
4 5 2 3 1
4 2 5 3 1
4 2 3 5 1
4 2 3 1 5
5 2 4 3 1
2 5 4 3 1
2 4 5 3 1
2 4 3 5 1
2 4 3 1 5
5 2 3 4 1
2 5 3 4 1
2 3 5 4 1
2 3 4 5 1
2 3 4 1 5
5 2 3 1 4
2 5 3 1 4
2 3 5 1 4
2 3 1 5 4
2 3 1 4 5
5 4 2 1 3
4 5 2 1 3
4 2 5 1 3
4 2 1 5 3
4 2 1 3 5
5 2 4 1 3
2 5 4 1 3
2 4 5 1 3
2 4 1 5 3
2 4 1 3 5
5 2 1 4 3
2 5 1 4 3
2 1 5 4 3
2 1 4 5 3
2 1 4 3 5
5 2 1 3 4
2 5 1 3 4
2 1 5 3 4
2 1 3 5 4
2 1 3 4 5
5 4 3 1 2
4 5 3 1 2
4 3 5 1 2
4 3 1 5 2
4 3 1 2 5
5 3 4 1 2
3 5 4 1 2
3 4 5 1 2
3 4 1 5 2
3 4 1 2 5
5 3 1 4 2
3 5 1 4 2
3 1 5 4 2
3 1 4 5 2
3 1 4 2 5
5 3 1 2 4
3 5 1 2 4
3 1 5 2 4
3 1 2 5 4
3 1 2 4 5
5 4 1 3 2
4 5 1 3 2
4 1 5 3 2
4 1 3 5 2
4 1 3 2 5
5 1 4 3 2
1 5 4 3 2
1 4 5 3 2
1 4 3 5 2
1 4 3 2 5
5 1 3 4 2
1 5 3 4 2
1 3 5 4 2
1 3 4 5 2
1 3 4 2 5
5 1 3 2 4
1 5 3 2 4
1 3 5 2 4
1 3 2 5 4
1 3 2 4 5
5 4 1 2 3
4 5 1 2 3
4 1 5 2 3
4 1 2 5 3
4 1 2 3 5
5 1 4 2 3
1 5 4 2 3
1 4 5 2 3
1 4 2 5 3
1 4 2 3 5
5 1 2 4 3
1 5 2 4 3
1 2 5 4 3
1 2 4 5 3
1 2 4 3 5
5 1 2 3 4
1 5 2 3 4
1 2 5 3 4
1 2 3 5 4
1 2 3 4 5

Feb 4 '06 #4

ma************* *******@yahoo.c om wrote:
Victor Bazarov wrote:
ma************* *******@yahoo.c om wrote:
Any suggestions welcome. Sorry if google groups screws up the
formatting.
Formatting is fine. The code doesn't work. You need to fix
it first. Then you can think of improving it...


Why didn't you actually bother to run the code? You clearly didn't
understand how the code works. I don't mind being criticized, but
please use your head and if that is too difficult for you compile and
run the code before making an idiot of yourself in a public forum.


Whether he compiled the code or not, Victor is correct. The code in
question is

list<int>::iter ator t=p.begin();
if(i>n) return;
for(int k=0;k<i;k++)
{
p.insert(t++,i) ;

The first time this is executed, the list p is empty and so the
iterator t is initialised to p.end() (which is the same thing as
p.begin() for an empty container). Incrementing p.end(), as you do on
the last line I have quoted, is undefined behaviour. Anything can
happen, including exactly what you expect. I compiled and ran your code
and the undefined behaviour manifested itself in a different way. The
program crashed on that line.
It works fine for me


Purely because you were unlucky.

Gavin Deane

Feb 4 '06 #5
ma************* *******@yahoo.c om wrote:
Victor Bazarov wrote:
ma************* *******@yahoo.c om wrote:
Any suggestions welcome. Sorry if google groups screws up the
formatting.
Formatting is fine. The code doesn't work. You need to fix
it first. Then you can think of improving it...


Why didn't you actually bother to run the code?


I did. It didn't work.
You clearly didn't
understand how the code works.
It does NOT.
I don't mind being criticized, but
Oh, *really*?! All indications to the contrary.
please use your head
You mean, like you use yours?
and if that is too difficult for you compile and
run the code before making an idiot of yourself in a public forum.
ROFLMAO
It works fine for me
[..]


<wiping tears of my eyes> I am sorry for you then. Come back when
you learn some manners.

V
Feb 4 '06 #6
Agreed - the empty list causes a 'nonincrementab le iterator' debug
assertion when first compiled and attempted to run. Easy enough fix for
the code - not so sure about fixing the person's manners tho. Clearly
have never heard (or understood) the expression about gift horses &
mouths....

Feb 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3142
by: pembed2003 | last post by:
Hi all, I asked this question in the C group but no one seems to be interested in answering it. :-( Basically, I wrote a search and replace function so I can do: char source = "abcd?1234?x"; char search = '?'; char* replace = "***"; char* result = search_and_replace(source,search,replace);
3
3031
by: Anand | last post by:
Hi Iam keen interested to improve my skills in c programming. Now Iam familiar with all the syntax and concepts. My main idea is to know how entire c compiler behaves in all circumstances(i.e for all kind of programs). Also I want the "Tips and Tracks" of C. With this current knowledge how can I improve my skills. Is there any speical book to develop these attitudes? Can anyone help me ! Regards, Anand.
9
4620
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
23
1841
by: philipl | last post by:
hi, I have some code here which basically look for within a string, the occurance of any 3 consectative characters which are the same. so AAA bbb etc would be reported by this function. I later added some code which is needed to detect the scenario where the same char appears 4 times for example AAAA, in this case want to skip the remaining 3 chars and check the next character in the string. This has led me to increase my for loop...
6
1540
by: Jéjé | last post by:
Hi, hoew can I improve the compilation process of a sharepoint website? my server is: 2 * P3 Xeom 1ghz 4go ram 2 * 36gb (mirror for OS and website) 2 * 36 Raid 0 (stripping; for temp files and ASP.net compiled files) 3.*36 raid 5 (for databases)
7
1591
by: dphizler | last post by:
This is the basic concept of my code: #include <iostream.h> #include <math.h> #include <stdlib.h> int main() { double Gtotal, L, size, distance, theRandom; double Gi; int xi, xf, yi, yf, zi, zf;
16
3079
by: weidongtom | last post by:
Hi, I have just finished reading some tutorials on C, I am wondering how I could improve my skill. Is there any advice? Is reading others' codes the best way? If so, what type of codes are suitable for novice? The ones in fsf freed software directory? I have been reading quite a few books on the programming language C, but when I tried to start a project of my own, I find myself to be incompetent. What should I do? Thanks in advance.
11
3390
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem to work out how to start on a solution. I have of course used a varienty of componets, mostly radio buttons with "button" appearence.
2
2778
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known 1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver 2) In that programme I m using prepared statement instead of statement 3) I am...
5
1554
by: Gilles Ganault | last post by:
Hello I'm no PHP expert, and I'm reading "Building scalable web sites". In the tips section, the author mentions using templates to speed things up. I was wondering how the template engines manage PHP pages that contain calls to MySQL: In our application, the data returned is different for most users, so the resulting page has different contents. So why do templates (and opcode cache) improve performance? Thank you.
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7616
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.