Need help from you programmers!

A General Discussion forum for cars and other topics, and a great place to introduce yourself if you are new to NICO!
User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

yeah, after 8 hours i killed the run. the program was only about 1/3 of the way done. on the machine i was running it on it would take about 24 hours to complete, a 3Ghz P4 may be able to do it quicker.

keep in mind, i used a pretty simple and inefficient algorithm, surely there are much quicker ones.

this was my program (a slightly modified version of the one from C/C++ Users Journal):

#include <iostream>#include <string>#include <fstream>using namespace std;

void permute( string prefix, string s, ofstream& file );

main(){ ofstream myFile; myFile.open("results.txt"); permute( "", "PERPENDICULAR", myFile ); return 0;} void permute( string prefix, string s, ofstream& f ){ if ( s.size() <= 1 ) f << prefix << s << endl; else for ( char *p = s.begin(); p < s.end(); p++ ) { char c = *p; s.erase( p ); permute( prefix + c, s, f ); s.insert( p, c ); }}


Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

*bows down*Thanks, some buddies and I are going to see if we can improve upon your code.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

OK this gets it to the screen. I have no idea about getting it to a file. I've never seen the copy thing. (only had 2 semesters of C++)

#include <iostream>#include <vector>#include <string>#include <algorithm>#include <functional>#include <fstream>

using namespace std;

void main(){ ofstream myFile; myFile.open("results.txt");

const int VECTOR_SIZE = 13 ; char stringA[13];

// Define a template class vector of strings typedef vector<string, allocator<string> > StrVector ;

//Define an iterator for template class vector of strings typedef StrVector::iterator StrVectorIt ;

//Define an ostream iterator for strings typedef ostream_iterator<string,char,char_traits<char> > StrOstreamIt;

StrVector Pattern(VECTOR_SIZE) ;

StrVectorIt start, end, it ;

StrOstreamIt outIt(cout, " ") ;

start = Pattern.begin() ; // location of first // element of Pattern

end = Pattern.end() ; // one past the location last // element of Pattern

//Initialize vector Pattern Pattern[0] = "P" ; Pattern[1] = "E" ; Pattern[2] = "R" ; Pattern[3] = "P" ; Pattern[4] = "E" ; Pattern[5] = "N" ; Pattern[6] = "D" ; Pattern[7] = "I" ; Pattern[8] = "C" ; Pattern[9] = "U" ; Pattern[10] = "L" ; Pattern[11] = "A" ; Pattern[12] = "R" ;

while ( next_permutation(start, end) ) { copy(start, end, outIt); cout << endl; }}

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

It runs pretty fast too... If you know how to make the file work let me know...

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

Nevermind here it is

#include <iostream>#include <vector>#include <string>#include <algorithm>#include <functional>#include <fstream>#include <iomanip.h>using namespace std;

void main(){ ofstream myFile; myFile.open("results.txt", ios::out);

const int VECTOR_SIZE = 13 ; char stringA[13];

// Define a template class vector of strings typedef vector<string, allocator<string> > StrVector ;

//Define an iterator for template class vector of strings typedef StrVector::iterator StrVectorIt ;

//Define an ostream iterator for strings typedef ostream_iterator<string,char,char_traits<char> > StrOstreamIt;

StrVector Pattern(VECTOR_SIZE) ;

StrVectorIt start, end, it ;

StrOstreamIt outIt(myFile, " ") ;

start = Pattern.begin() ; // location of first // element of Pattern

end = Pattern.end() ; // one past the location last // element of Pattern

//Initialize vector Pattern Pattern[0] = "P" ; Pattern[1] = "E" ; Pattern[2] = "R" ; Pattern[3] = "P" ; Pattern[4] = "E" ; Pattern[5] = "N" ; Pattern[6] = "D" ; Pattern[7] = "I" ; Pattern[8] = "C" ; Pattern[9] = "U" ; Pattern[10] = "L" ; Pattern[11] = "A" ; Pattern[12] = "R" ;

while ( next_permutation(start, end) ) { copy(start, end, outIt); myFile << endl; }}

Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

Rob... I owe you. If ever I'm dirty filthy rich, you can have any car you want... ever.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

I ran it overnight. Don't know how long it takes(i was asleep :D) but it finished in less than 8 hours. The output is 6.25 GB. The only problem is that My computer crashes when i try to zip it. I'm working on that problem now.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

the windows XP "Zipper" is the one that crashes. It also Corrupted the file! DAMN! I got WinZip and i'll generate the text file again tonight. See if WinZip will do a better job.

Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

If you can't zip it, send me the executable and I'll just give it to my teacher. He can deal with it then.

User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

Rob, good job. Found a small curiosity, I ran your program with a smaller array [P, E, R] and it produced this result:

P R E R E P R P E

Two permutations (E R P and E P R) were missing. Can you verify this.

Interestingly using [A, B, C] gave all permutations:

A C B B A C B C A C A B C B A

p.s.

#include <iostream>#include <functional>#include <iomanip.h>

you don't need to include these

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

wierd. I wonder if it works for the big one.

And yah originally i had those things included becasue i was writing the answere to the console.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

crap. The input has to be ordered for it to work...

E R P P E R P R E R E P R P E

I may or may not have missed some last night. I'll let you know in tonights run if it gets bigger.

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

#include <iostream>#include <vector>#include <string>#include <algorithm>#include <functional>#include <fstream>

using namespace std;

void main(){

cout << "Please Don't Stop me!"; ofstream myFile; myFile.open("results.txt");

const int VECTOR_SIZE = 13 ;// char stringA[13];

// Define a template class vector of strings typedef vector<string, allocator<string> > StrVector ;

//Define an iterator for template class vector of strings typedef StrVector::iterator StrVectorIt ;

//Define an ostream iterator for strings typedef ostream_iterator<string,char,char_traits<char> > StrOstreamIt;

StrVector Pattern(VECTOR_SIZE) ;

StrVectorIt start, end, it ;

StrOstreamIt outIt(myFile, " ") ;

start = Pattern.begin() ; // location of first // element of Pattern

end = Pattern.end() ; // one past the location last // element of Pattern

//Initialize vector Pattern Pattern[0] = "A" ; Pattern[1] = "C" ; Pattern[2] = "D" ; Pattern[3] = "E" ; Pattern[4] = "E" ; Pattern[5] = "I" ; Pattern[6] = "L" ; Pattern[7] = "N" ; Pattern[8] = "P" ; Pattern[9] = "P" ; Pattern[10] = "R" ; Pattern[11] = "R" ; Pattern[12] = "U" ;

while ( next_permutation(start, end) ) { copy(start, end, outIt); myFile << endl; }}

That should do it. My version of Microsoft C++ won't let me build executables i don't think. Let me try it and get back to you.

User avatar
nomuken
Posts: 436
Joined: Sat May 03, 2003 5:12 pm

Post

Quote »That should do it. My version of Microsoft C++ won't let me build executables i don't think. Let me try it and get back to you.[/quote]

i'll try it as well.

don't wanna come across as too critical, just trying to help debug :pface another thing i noticed is that StrVectorIt it is declared but never used.

how come you can't build executables?

User avatar
RobDET
Posts: 1553
Joined: Wed May 07, 2003 5:51 pm
Car: Cars

Post

I ripped a lot of that stuff from another program. What you are looking at is 2 programms i spliced togather. I don't usually write original stuff unless it's for class. After all there is so mutch data on the internet.

It turns out i can make executables. I didn't know if my free version of MS C++ would let me.

Sircnay
Posts: 1384
Joined: Mon Jun 23, 2003 11:13 am
Car: EVERYTHING

Post

Yes..... do my bidding *clasps hands together* MWAHAHAHAHAHAHAHA!


Return to “General Chat”