lotrsimp12345 37 Posting Pro in Training

I'm not sure what you mean by 4) The boolean should match the guess peg/inner loop.

Here's what I did....

void Mastermind::getblackpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		for(int i=0; i<4; i++)
		{
			if(population_[k][i]==answer_[i])
			{
				totalnumberofblackpegs_[k]++;
			}
		}
	}
}

void Mastermind::gettotalnumberofpegs()
{
	bool** guesscounted=(bool**)(malloc(sizeof(bool*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(guesscounted+i)=(bool*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			guesscounted[i][j]=false;
		}
	}
	//through population
	for(int k=0; k<populationsize_; k++)
	{
		//outer loop go through answer peg
		for(int i=0; i<4; i++)
		{
			//through guess pegs
			for(int j=0; j<4; j++)
			{
				if((population_[k][j]==answer_[i])&&!guesscounted[k][j])
				{
					totalnumberofpegs_[k]++;
					guesscounted[i][j]=true;
				}
			}
		}
	}
}

void Mastermind::getwhitepegs()
{
	for(int i=0; i<populationsize_; i++)
	{
		totalnumberofwhitepegs_[i]=totalnumberofpegs_[i]-totalnumberofblackpegs_[i];
	}
}
lotrsimp12345 37 Posting Pro in Training

I have underscores since I can't have a function name be the same as a datatype name in my .h file. Thanks I will try that.

lotrsimp12345 37 Posting Pro in Training

So I think i figure it out... But when my population is too big the answers are screwed up. :(.

Here's my code:
Main

#include <iostream>
#include <ctime>
#include "Mastermind.h"

using namespace std;

int main()
{
	int* answer=new int[4];
	int populationsize=0;
	srand((unsigned)time(NULL));
	cout<<"please select 4 numbers out of the following options";
	cout<<"\nred:0\n"
		  "black:1\n"
		  "yellow:2\n"
		  "white:3\n"
		  "blue:4\n"
		  "green:5\n";
	for(int i=0; i<4; i++)
	{
		cin>>answer[i];
	}
	Mastermind a;
	a.setanswer(answer);
	cout<<"please enter a population size";
	cin>>populationsize;
	a.setpopulationsize(populationsize);
	a.initializepopulation();
	a.processGuess();
	a.print();
	return 0;
}

Mastermind.h

#include <iostream>

class Mastermind
{
public:
	Mastermind();

	void initializepopulation();
	void setpopulationsize(int populationsize);
	void setanswer(int* answer);
	void setgenerationnumber(int generationnumber);
	int randomizenumber();

	void processGuess();
	int getblackpegs(int* guess);
	int getwhitepegs(int* guess);

	/*void calculatenumberofblackpegs();
	void calculatenumberofwhitepegs();
	void totalnumberofpegs();*/

	void mutate();
	void crossover();

	void calculatepopulationfitness();

	int evaluatepopulation();

	void print();
	
private:
	int** population_;
	int populationsize_;
	int* answer_;
	int generationnumber_;
	int* totalnumberofblackpegs_;
	int* totalnumberofwhitepegs_;
	int* totalnumberofpegs_;
	//store answer
	//int** temp_;
	//int** indtemp_;
	int* fitness_;
	int sum(int index);
	int pickrandomposition();
};

Mastermind.cpp

#include <cstdlib>
#include "Mastermind.h"
#include <iostream>

Mastermind::Mastermind()
{
	populationsize_=0;
	answer_=new int[4];
	generationnumber_=0;
	totalnumberofblackpegs_=new int[populationsize_];
	totalnumberofwhitepegs_=new int[populationsize_];
	totalnumberofpegs_=new int[populationsize_];
	fitness_=new int[populationsize_];
}

void Mastermind::initializepopulation()
{
	population_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(population_+i)=(int*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		//std::cout<<i<<std::endl;
		for(int j=0; j<4; j++)
		{
			population_[i][j]=randomizenumber();
			//std::cout<<population_[i][j];
		}
		//std::cout<<"\n";
	}
}

void Mastermind::setpopulationsize(int populationsize)
{
	populationsize_=populationsize;
}

void Mastermind::setanswer(int* answer)
{
	answer_=answer;
}

void Mastermind::setgenerationnumber(int generationnumber)
{
	generationnumber_=generationnumber;
}

int Mastermind::randomizenumber()
{
	int d=rand()% 6;
	return d;
}

void Mastermind::processGuess()
{
	for(int k=0; k<populationsize_; k++)
	{
		totalnumberofblackpegs_[k]=0;
		totalnumberofwhitepegs_[k]=0;
		totalnumberofpegs_[k]=0;
		int blackpegs=getblackpegs(population_[k]);
		int …
lotrsimp12345 37 Posting Pro in Training

so i think to fix this i need an array which stores number of each element? Based on above??????

AHHHHHHHHHHHHH

lotrsimp12345 37 Posting Pro in Training

Here's the function:
still some errors with it :(. Please help.

void Mastermind::calculatenumberofblackpegs()
{
	temp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(temp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	indtemp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(indtemp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		totalnumberofblackpegs_[i]=0;
		totalnumberofwhitepegs_[i]=0;
		totalnumberofpegs_[i]=0;
		for(int j=0; j<4; j++)
		{
			//set indtemp and temp to 0
			indtemp_[i][j]=0;
			temp_[i][j]=0;
			if(answer_[j]==population_[i][j])
			{
				//get rid of positions which match by putting 6 in those positions
				totalnumberofblackpegs_[i]++;
				temp_[i][j]=6;
				indtemp_[i][j]=6;
			}
			else
			{
				temp_[i][j]=answer_[j];
				indtemp_[i][j]=population_[i][j];
			}
		}
		std::cout<<totalnumberofblackpegs_[i]<<std::endl;
	}
	/*std::cout<<"temp+indtemp\n";
	for(int j=0; j<populationsize_; j++)
	{
		for(int k=0; k<4; k++)
		{
			std::cout<<temp_[j][k];
		}
		std::cout<<std::endl;
		for(int k=0; k<4; k++)
		{
			std::cout<<indtemp_[j][k];
		}
		std::cout<<std::endl;
	}*/
}

void Mastermind::calculatenumberofwhitepegs()
{
	
	for(int pop=0; pop<populationsize_; pop++)
	{
		//index temp which is answer array replaced by 6 where positions match
		int i=0;
		//index indtemp which is each individual in population where 
		//each match is replaced by 6
		int j=0;
		//temp isn't at end
		do
		{
			//position in temp already accounted for
			if(temp_[pop][i]==6)
			{
				i++;
			}
			else
			{
				//indtemp isn't at end
				do
				{
					//values match
					if(indtemp_[pop][j]==temp_[pop][i])
					{
						if(indtemp_[pop][j]!=6&&temp_[pop][i]!=6)
						{
							totalnumberofwhitepegs_[pop]++;
							//set count to 6 so that it skips 
							//those since it accounted for those
							indtemp_[pop][j]=6;
							temp_[pop][i]=6;
						}
						//move both indexes forward
						j++;
						i++;
					}
					else
					{
						j++;
					}
				}while(j<4);
				i++;
				j=0;
			}
		}while(i<4);
		std::cout<<totalnumberofwhitepegs_[pop]<<std::endl;
	}
}
lotrsimp12345 37 Posting Pro in Training

So I think I MADE SOME PROGRESS:
THIS IS MOST RECENT ONE THERE ARE STILL SOME GLICHES... Disregard earlier posts
PLEASE HELP.
Take a look at function calculatenumberofwhitepegs() and calculatenumberofblackpegs(). That's the part where I am stuck. :( I have spent 40 hours on this.... think its about to call it quits..

main

#include <iostream>
#include <ctime>
#include "Mastermind.h"

using namespace std;

int main()
{
	int* answer=new int[4];
	int populationsize=0;
	srand((unsigned)time(NULL));
	cout<<"please select 4 numbers out of the following options";
	cout<<"\nred:0\n"
		  "black:1\n"
		  "yellow:2\n"
		  "white:3\n"
		  "blue:4\n"
		  "green:5\n";
	for(int i=0; i<4; i++)
	{
		cin>>answer[i];
	}
	Mastermind a;
	a.setanswer(answer);
	cout<<"please enter a population size";
	cin>>populationsize;
	a.setpopulationsize(populationsize);
	a.initializepopulation();
	a.calculatenumberofblackpegs();
	cout<<"white pegs\n";
	a.calculatenumberofwhitepegs();
	return 0;
}

Mastermind.h

Mastermind.cpp

#include <cstdlib>
#include "Mastermind.h"
#include <iostream>

Mastermind::Mastermind()
{
	populationsize_=0;
	answer_=new int[4];
	generationnumber_=0;
	totalnumberofblackpegs_=new int[populationsize_];
	totalnumberofwhitepegs_=new int[populationsize_];
	totalnumberofpegs_=new int[populationsize_];
	fitness_=new int[populationsize_];
}

void Mastermind::initializepopulation()
{
	population_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(population_+i)=(int*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		std::cout<<i<<std::endl;
		for(int j=0; j<4; j++)
		{
			population_[i][j]=randomizenumber();
			std::cout<<population_[i][j];
		}
		std::cout<<"\n";
	}
}

void Mastermind::setpopulationsize(int populationsize)
{
	populationsize_=populationsize;
}

void Mastermind::setanswer(int* answer)
{
	answer_=answer;
}

void Mastermind::setgenerationnumber(int generationnumber)
{
	generationnumber_=generationnumber;
}

int Mastermind::randomizenumber()
{
	int d=rand()% 6;
	return d;
}

void Mastermind::calculatenumberofblackpegs()
{
	temp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(temp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	indtemp_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(indtemp_+i)=(int*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		totalnumberofblackpegs_[i]=0;
		totalnumberofwhitepegs_[i]=0;
		totalnumberofpegs_[i]=0;
		for(int j=0; j<4; j++)
		{
			//set indtemp and temp to 0
			indtemp_[i][j]=0;
			temp_[i][j]=0;
			if(answer_[j]==population_[i][j])
			{
				//get rid of positions which match by putting 6 in those positions
				totalnumberofblackpegs_[i]++; …
lotrsimp12345 37 Posting Pro in Training

i am really confused now.... so my answer would store 4 numbers and then input would store 4 numbers. But why am I comparing answer at index to user where user is 0 or 1. Should the condition be

if(answer[i]==input[j])?

Thanks.

What exactly is this calculating?? I am really confused now.

I don't think this is right.

for(int m=0; m<populationsize_; m++)
	{
		totalnumberofpegs_[m]=0;
	}
	int flag[4];
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			flag[j]=0;
			for(int k=0; k<4; k++)
			{
				if(answer_[j]==population_[i][k])
				{
					flag[j]=1;
					totalnumberofpegs_[i]++;
				}
			}
		}
			std::cout<<totalnumberofpegs_[i]<<std::endl;
	}
lotrsimp12345 37 Posting Pro in Training

nope that doesn't work. For example the case 1221. gets you 2 and 2 when it should be 2 and 0.

lotrsimp12345 37 Posting Pro in Training

I think i finally got it!!
I put both of them in one function.
Anyone check whether this is correct.
Thanks

void Mastermind::calculatenumberofwhiteandblackpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		totalnumberofblackpegs_[k]=0;
		totalnumberofwhitepegs_[k]=0;
		totalnumberofpegs_[k]=0;
	}
	int flag[4];
	//correct colored peg placed in wrong position
	//start with first item in array and check against the temp array
	for(int i=0; i<populationsize_; i++)
	{
		for(int j=0; j<4; j++)
		{
			flag[j]=0;
			if(answer_[j]==population_[i][j])
			{
				totalnumberofblackpegs_[i]++;
			}
			else
			{
				for(int s=0; s<4; s++)
				{
					if(s!=j && population_[i][j]==answer_[s]&&!flag[s])
					{
						totalnumberofwhitepegs_[i]++;
						flag[s]=1;
						break;
					}
				}
			}
		}
		std::cout<<"total number of black:"<<totalnumberofblackpegs_[i]
		<<"total number of white:"<<totalnumberofwhitepegs_[i]<<std::endl;
	}
}
lotrsimp12345 37 Posting Pro in Training

i am baiscally trying to find the intersection of two arrays but keeping duplicates? does that make sense?

lotrsimp12345 37 Posting Pro in Training

So i found one of my earlier codes which takes two files and find the elements in common and not in common so i was going to do the same thing but with arrays. It gives me some run time error and I am unsure why. Thanks for the help. The debug error is in the second getnextitem().

void Mastermind::totalnumberofpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		int i=0;
		int j=0;
		int moreitems;
		//answer array
		int list1=getnextitem(i);
		//population array
		int list2=getnextitem(k,j);
		moreitems=list1&&list2;
		while(moreitems)
		{
			if(list1<list2)
			{
				list1=getnextitem(i++);
				totalnumberofpegs_[k]=totalnumberofpegs_[k];
			}
			else if(list1==list2)
			{
				list1=getnextitem(i++);
				list2=getnextitem(k,j++);
				totalnumberofpegs_[k]++;
			}
			else
			{
				list2=getnextitem(k,j++);
				totalnumberofpegs_[k]=totalnumberofpegs_[k];
			}
		}
		std::cout<<k<<"::"<<totalnumberofpegs_[k]<<"\n";
	}
}

int Mastermind::getnextitem(int index)
{
	return answer_[index];
}

int Mastermind::getnextitem(int pop,int index)
{
	return population_[pop][index];
}
lotrsimp12345 37 Posting Pro in Training

i am unsure of how to do the first part.

Here's what I have.
Not sure why it doesn't work. Any help appreciated.

void Mastermind::totalnumberofpegs()
{
	for(int k=0; k<populationsize_; k++)
	{
		for(int i=0; i<4; i++)
		{
			for(int j=0; j<4; j++)
			{
				if(answer_[i]==population_[k][j])
				{
					totalnumberofpegs_[i]++;
				}
				else
				{
					totalnumberofpegs_[i]=totalnumberofwhitepegs_[i];
				}
			}
		}
		std::cout<<k<<"::"<<totalnumberofpegs_[k]<<"\n";
	}
}
lotrsimp12345 37 Posting Pro in Training

For example:
if i enter 1232
and population size is 5. randomly generate following population. the temp changes number to 6 which match in position and value. Please help.

population Black Temp White ExpectedWhite
5024| 0| 5024| 0| 1
5210| 1| 5610| 1| 1
5551| 0| 5551| 0| 1
5143| 0| 5143| 1| 2
2455| 0| 2455| 0| 1

lotrsimp12345 37 Posting Pro in Training

Need to calculate number of white pegs so I can write fitness function. It works sometimes and it doesn't work other times.

I got the calculate number of black pegs to work.
A black peg is correct position and color.
A white peg is correct color wrong position.
Colors are numbers in my program.
Here is all my code so far.
Any help appreciated.

main.cpp

#include <iostream>
#include <ctime>
#include "Mastermind.h"

using namespace std;

int main()
{
	int* answer=new int[4];
	int populationsize=0;
	srand((unsigned)time(NULL));
	cout<<"please select 4 numbers out of the following options";
	cout<<"\nred:0\n"
		  "black:1\n"
		  "yellow:2\n"
		  "white:3\n"
		  "blue:4\n"
		  "green:5\n";
	for(int i=0; i<4; i++)
	{
		cin>>answer[i];
	}
	Mastermind a;
	a.setanswer(answer);
	cout<<"please enter a population size";
	cin>>populationsize;
	a.setpopulationsize(populationsize);
	a.initializepopulation();
	cout<<std::endl;
	a.calculatenumberofblackpegs();
	a.calculatenumberofwhitepegs();
	//a.calculatenumberofwhitepegs();
	//a.evaluatepopulation();
	//while(populationfitness
	return 0;
}

Mastermind.h

#include <iostream>

class Mastermind
{
public:
	Mastermind();

	void initializepopulation();
	void evaluatepopulation();
	void setpopulationsize(int populationsize);
	void setanswer(int* answer);
	void setgenerationnumber(int generationnumber);
	int randomizenumber();

	void calculatenumberofblackpegs();
	void calculatenumberofwhitepegs();
	void totalnumberofpegs();

	void mutate();
	void crossover();

	int calculatepopulationfitness();

private:
	int** population_;
	int populationsize_;
	int* answer_;
	int generationnumber_;
	int* totalnumberofblackpegs_;
	int* totalnumberofwhitepegs_;
	int* totalnumberofpegs_;
	int** temp;
	int* fitness_;
};

Mastermind.cpp

#include <cstdlib>
#include "Mastermind.h"

Mastermind::Mastermind()
{
	populationsize_=0;
	answer_=new int[4];
	generationnumber_=0;
	totalnumberofblackpegs_=new int[populationsize_];
	totalnumberofwhitepegs_=new int[populationsize_];
	totalnumberofpegs_=new int[populationsize_];
	fitness_=new int[populationsize_];
}

void Mastermind::initializepopulation()
{
	population_=(int**)(malloc(sizeof(int*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(population_+i)=(int*)(malloc(sizeof(int)*4));
	}
	for(int i=0; i<populationsize_; i++)
	{
		std::cout<<i<<std::endl;
		for(int j=0; j<4; j++)
		{
			population_[i][j]=randomizenumber();
			std::cout<<population_[i][j];
		}
		std::cout<<"\n";
	}
}

void Mastermind::setpopulationsize(int populationsize)
{
	populationsize_=populationsize;
}

void Mastermind::setanswer(int* answer)
{
	answer_=answer;
} …
lotrsimp12345 37 Posting Pro in Training

Here's my code.

(define L ())
(define (union L1 L2)
  ;continue until one list is empty
  (do()
    ((or (null? L1) (null? L2)))
    (cond
      ((<(car L1)(car L2)) (attach(car L1)(cdr L1)L2))
      ((eq?(car L1)(car L2)) (attach(car L1)(cdr L1)(cdr L2)))
      (else(>(car L1)(car L2)) (attach(car L2)L1(cdr L2)))
    )
  )
;after which you continue to add add while one list isn't empty and ;the second one isn't empty. I know I need a new attach function. 
)

(define (attach item List1 List2)
  (cons item L)
  (union List1 List2)
)
lotrsimp12345 37 Posting Pro in Training

thanks i figured it out.

lotrsimp12345 37 Posting Pro in Training

so far I can't figure out a way to write powerset in SML :(.
I see a pattern for example

powerset([1,2,3]) is

[] [1]
[2] [1,3]
[3] [1,2]
[2,3] [1,2,3]

Here's what I have come up with:

fun add(a,L)= [a]@L

fun ps(L)=
ps(tl L)@add(hd L, ps(tl L));

Any help appreciated.

lotrsimp12345 37 Posting Pro in Training

the error message i get is:
[use failed:IO openIn failed on "cyclen.sml", Win32TextPrimIO:openRd: failed]

uncaught exception error
raised at: ../compiler/TopLevel/interact/interact.sml:24-14-24.28

lotrsimp12345 37 Posting Pro in Training

I recently updated to windows 7 and since then I have been having this problem. Any help appreciated.

lotrsimp12345 37 Posting Pro in Training

I plan on using the Bayesian method to try and solve the mastermind problem. The user will enter 4 numbers which represent the colors on the board from 6 different colors. After which the computer will randomly estimate the posterior probabilities for all events. ?Then continue with this scheme until it has gone through 12 rounds or guesses the score?. An event is defined as a row that the codebreaker uses.

lotrsimp12345 37 Posting Pro in Training

got it to work but not the way i wanted. oh well.

lotrsimp12345 37 Posting Pro in Training

When i run main it prints out gibberish not sure why.

main.cpp

#include <iostream>
#include <string>
#include "phrase.h"
#include "EA.h"
#include <cstdlib>
#include <time.h>

int main()
{
	phrase a;
	EA b;

	std::string phrase;
	int populationsize;

	std::cout<<"enter a population size\n";
	std::cin>>populationsize;
	std::cin.ignore();
	b.setpopulationsize(populationsize);

	std::cout<<"enter a string\n";
	getline(std::cin,phrase);
	a.setphraselength(phrase);
	b.setlength(phrase);
	std::cout<<"the phrase length is"<<a.getphraselength()<<std::endl;

	srand ((unsigned)time(0));
	
	
	b.initalizepopulation();
	/*while(phrase!=bestofpopulation())
	{
		//mutate and crossover to create new population
	}*/
	
	return 0;
}

EA.h

#ifndef EA_H_INCLUDED
#define EA_H_INCLUDED
#include <string>
#include "phrase.h"
#include <iostream>

class EA:public phrase
{
public:
	EA();

	~EA();

	void setgenerationnumber(int generationnumber);
	int getgenerationnumber();

	void setpopulationsize(int populationsize);

	void setlength(std::string length);

	void initalizepopulation();

	//void mutate();

	//void crossover();

	std::string bestofpopulation();

private:
	int populationsize_;
	int generationnumber_;
	int length_;
	char** population_;
	
};
#endif // INTERFACE_H_INCLUDED

EA.cpp

#include "EA.h"

EA::EA()
{
	populationsize_=0;
	generationnumber_=0;
	length_=0;
	population_=NULL;
}

EA::~EA()
{
	delete population_;
}

void EA::setgenerationnumber(int generationnumber)
{
	generationnumber_=generationnumber;
}

int EA::getgenerationnumber()
{
	return generationnumber_;
}

void EA::setpopulationsize(int populationsize)
{
	populationsize_=populationsize;
}

void EA::setlength(std::string length)
{
	length_=length.length();
}

void EA::initalizepopulation()
{
	population_=(char**)(malloc(sizeof(char*)*populationsize_));
	for(int i=0; i<populationsize_; ++i)
	{
		*(population_+i)=(char*)(malloc(sizeof(char)*length_));
	}

	for(int j=0; j<populationsize_; ++j)
	{
		std::cout<<j<<std::endl;
		for(int k=0; k<length_; ++k)
		{
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<*((*population_+k)+j);
		}
		std::cout<<"\n";
	}
}
	/*void mutate()

	void crossover()

	std::string bestofpopulation()
*/

phrase.h

#ifndef PHRASE_H_INCLUDED
#define PHRASE_H_INCLUDED

#include <string>

class phrase
{
public:	
	phrase();

	void setphraselength(std::string phraselength);
	int getphraselength();

	//randomly intialize a string
	char* generatenewIndividual();
private:
	char randomizecharacter();
	
private:
	int phraselength_;	
};
#endif // INTERFACE_H_INCLUDED

phrase.cpp

#include "phrase.h"
#include <string>

phrase::phrase()
{
	phraselength_=0;
}

void phrase::setphraselength(std::string phraselength)
{
	phraselength_=phraselength.length();
}

int phrase::getphraselength()
{
	return …
lotrsimp12345 37 Posting Pro in Training

most updated version:

for(int j=0; j<populationsize_; j++)
	{
		std::cout<<j<<std::endl;
                std::cout<<"enter first loop\n";
		population_=new char*[populationsize_];
		std::cout<<"length is"<<phraselength;
		for(int k=0; k<phraselength; k++)
		{
                        std::cout<<"enter second loop\n";
			(*(population_))=new char[phraselength];
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<(*population_);
		}
		std::cout<<"\n";
	}
lotrsimp12345 37 Posting Pro in Training

somehow in the main i get the correct length to appear but in doesn't when i call in in intializepopulation. Perhaps i should have EA constructor to which i call phrase instead of using inheritance.

lotrsimp12345 37 Posting Pro in Training

why is saying length is 0 when it isn't in my main???

void EA::initalizepopulation()
{
	int len;
	for(int j=0; j<populationsize_; j++)
	{
                std::cout<<"enter first loop\n";
		population_=new char*[j];
		len = getphraselength();
		for(int k=0; k<len; k++)
		{
            std::cout<<"enter second loop\n";
			(*((population_)+k))=new char[k];
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<(*population_);
		}
		std::cout<<"\n";
	}
 
}
lotrsimp12345 37 Posting Pro in Training

IT doesn't enter second loop don't know why. Any suggestion?

void EA::initalizepopulation()
{
	
	
	for(int j=0; j<populationsize_; j++)
	{
                std::cout<<"enter first loop\n";
		population_=new char*[j];
		for(int k=0; k<getphraselength(); k++)
		{
                        std::cout<<"enter second loop\n";
			(*((population_)+j))=new char[k];
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<(*population_);
		}
		std::cout<<"\n";
	}
	
}
lotrsimp12345 37 Posting Pro in Training

I am trying to create a double pointer. never done it before. Problem is currently in intializepopulation();
Error C2106 left of operand must be l-value.

EA.h

#ifndef EA_H_INCLUDED
#define EA_H_INCLUDED
#include <string>
#include "phrase.h"
#include <iostream>

class EA:public phrase
{
public:
	EA();
	~EA();

	void setgenerationnumber(int generationnumber);
	int getgenerationnumber();

	void setpopulationsize(int populationsize);

	void initalizepopulation();

	void mutate();

	//void crossover();

	std::string bestofpopulation();

private:
	int populationsize_;
	int generationnumber_;
	char** population_;
};
#endif // INTERFACE_H_INCLUDED

EA.cpp

#include "EA.h"

EA::EA()
{
	populationsize_=NULL;
	generationnumber_=0;
	population_=NULL;
}

EA::~EA()
{
	delete population_;
}

void EA::setgenerationnumber(int generationnumber)
{
	generationnumber_=generationnumber;
}

int EA::getgenerationnumber()
{
	return generationnumber_;
}

void EA::setpopulationsize(int populationsize)
{
	populationsize_=populationsize;
}

void EA::initalizepopulation()
{
	
	
	for(int j=0; j<populationsize_; j++)
	{
		population_=new char*[j];
		for(int k=0; k<getphraselength(); k++)
		{
			(*(population_)+j)=new char[k];
			population_[j][k]=(char)generatenewIndividual();
			std::cout<<(*population_);
		}
		std::cout<<"\n";
	}
	
}
	/*void mutate()

	void crossover()

	std::string bestofpopulation()
*/

phrase.h

#ifndef PHRASE_H_INCLUDED
#define PHRASE_H_INCLUDED

#include <string>

class phrase
{
public:	
	phrase();

	void setphraselength(std::string phraselength);
	int getphraselength();

	//randomly intialize a string
	char* generatenewIndividual();
	char randomizecharacter();
private:
	int phraselength_;	
};
#endif // INTERFACE_H_INCLUDED

phrase.cpp

#include "phrase.h"
#include <string>

phrase::phrase()
{
	phraselength_=0;
}

void phrase::setphraselength(std::string phraselength)
{
	phraselength_=phraselength.length();
}

int phrase::getphraselength()
{
	return phraselength_;
}

char phrase::randomizecharacter()
{
	int n = rand() % 26;
    char c = (char)(n+'a');
	return c;
}

char* phrase::generatenewIndividual()
{
	char* random = new char[phraselength_];
	for(int i=0; i<=phraselength_; ++i)
	{
		random[i]=randomizecharacter();
	}
	random[phraselength_]=NULL;
	return random;
}

main

#include <iostream>
#include <string>
#include "phrase.h"
#include "EA.h"
#include <cstdlib>
#include <time.h>

int main()
{
	phrase a;
	EA b;
	std::string phrase;
	int …
lotrsimp12345 37 Posting Pro in Training

its it skipping. This is the part of the code. :(

cout<<"enter a string\n";
getline(std::cin,phrase);

#include <iostream>
#include <string>
#include "phrase.h"
#include "EA.h"
#include <cstdlib>
#include <time.h>

int main()
{
	phrase a;
	EA b;
	std::string phrase;
	int populationsize;
	//int percentmutation;
	std::cout<<"enter a population size\n";
	std::cin>>populationsize;
	b.setpopulationsize(populationsize);
	//std::cout<<"enter the percentage you would like to mutate\n";
	//std::cin>>percentmutation;
	std::cout<<"enter a string\n";
	getline(std::cin,phrase);
	
	//std::cout<<a.getphraselength();

	srand ((unsigned)time(0));
	
	
	b.initalizepopulation();
	/*while(phrase!=bestofpopulation())
	{
		//mutate and crossover to create new population
	}*/
	
	return 0;
}
lotrsimp12345 37 Posting Pro in Training

don't forget about the #ifndef

lotrsimp12345 37 Posting Pro in Training

don't forget the ifndef thing on top.

lotrsimp12345 37 Posting Pro in Training

don't forget the ifndef thing on top.

lotrsimp12345 37 Posting Pro in Training

like that. don't forget to delete after your done using as mentioned above.

int n = 2;   
int* list = new int[n];
lotrsimp12345 37 Posting Pro in Training

basically imagine a box and point your finger to it. That's basically a pointer. That's what you are doing on the first line.

don't need code shown on bottom. other than that it looks good.

(nothrow)
lotrsimp12345 37 Posting Pro in Training

pass pointers to functions instead of arrays. In the main dynamically allocate memory using new keyword on the first line. Believe that should work.

lotrsimp12345 37 Posting Pro in Training

Still having trouble install opencv.

I used cmake doing what was told and it created files in a folder then i ran the install VC++ project. After which I went back to cmake and generated the examples. ran those.

My path environment variable is C:\OpenCV2.0\bin, I was wondering whether it should be where the generated files by cmake are?

Thanks.

lotrsimp12345 37 Posting Pro in Training

one piece of advice switch statements will only accept built in types of C++. gl.

lotrsimp12345 37 Posting Pro in Training

i would consider using opencv. I still have to figure out how exactly you install it but I would recommend you use it.

lotrsimp12345 37 Posting Pro in Training

darn I can't seem to figure out how to do reverse append.

lotrsimp12345 37 Posting Pro in Training

figured it out. Stupid syntax :(.

Here's the answer

rappend(L1,L2)
    if null L2 then L1
    else rappend(tl(L2)@L1,[hd(L2)])
lotrsimp12345 37 Posting Pro in Training

Please help me out with syntax mistakes

fun rappend(L1,L2)
    if null L2 then L1
    else rappend(tl(L2) @ L1, hd(L2))
lotrsimp12345 37 Posting Pro in Training

why are you using a character array? could easily do it with string and use concatenation.

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

int main()
{
	string input;
	cin>>input;
	string a="abcdefghijklmnopqrstuvwxyz";
	string output=input+a;
	cout<<output;
	return 0;
}
lotrsimp12345 37 Posting Pro in Training

don't do it with a stack you are typing too much...
use recursion very powerful and easy to understand.

WaltP commented: Recursion is RARELY a good option. -2
lotrsimp12345 37 Posting Pro in Training

please help really stuck :(.

lotrsimp12345 37 Posting Pro in Training

some one please help I am really confused!! :(

lotrsimp12345 37 Posting Pro in Training

there is a math library.
Here's a link: http://www.cppreference.com/wiki/c/math/start

lotrsimp12345 37 Posting Pro in Training

if you want to put each line separately use endl like follows:

while(!myfile1.eof())
{
myfile1.getline(buffer,'\t');
myfile<<buffer<<endl;
}
lotrsimp12345 37 Posting Pro in Training

don't erase create substring just incase you still need the string later on.

lotrsimp12345 37 Posting Pro in Training

use event handlers, probably be easier in java since it comes with a lot of predefined shapes and stuff.

lotrsimp12345 37 Posting Pro in Training

the actual code would be helpful not just the errors.

lotrsimp12345 37 Posting Pro in Training

delete one of the threads since i posted it here. doesn't matter which one.