mrnutty 761 Senior Poster

Ok, why do you need array when you have vectors? Anyways, you can solve that problem with dynamic memory. Here is an example :

std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);

int vecSize = vec.size();

int *Array = new int[vecSize]; //create dynamic array.

for(int i = 0; i < vecSize; ++i)
  cout << Array[i] << endl;


delete Array; //free the memory
mrnutty 761 Senior Poster

Just for giggle, here is my solution :

bool hasWinningMove(const int stonesLeft,int& nextMove){
 const float tolerance = 0.00001f;
 //magic formula muhahahhaaa
 float intPart = 0;
 float frac = modf( (stonesLeft+3.0f)/4.0f , &intPart);

 if(frac <= tolerance|| intPart == 0){ 
     nextMove = 1; return false; 
 }
 else{
    int magicHole = int(ceil(4*intPart - 3));
    nextMove = stonesLeft - magicHole;    
	return true;
 }
}

Runs in constant time. Although no guarantees!!!

mrnutty 761 Senior Poster

Oh in that case no. May I ask you what are you trying to achieve?

mrnutty 761 Senior Poster

Umm not sure what your saying, some guesses :

const int N = 100;
const float pi = 3.14f;
const string = "what?"
enum{ CONSTANT_1, CONSTANT_2}

struct Foo{
 mutable semiConstant;
}
namespace Math{
 const float DEGTORAD = 0.0174532925f;
}
mrnutty 761 Senior Poster

Also, just for giggle, I calculated that this function : 1/2n^2-1/2n+1 gives the number of sequence that sums to a number n. Try it out and see if its correct. I give no guarantees.

mrnutty 761 Senior Poster

No the number 5 has 11 series that adds up to 5, right?

5 = 1 + 1 + 1 + 1 + 1
5 = 1 + 1 + 1 + 2
5 = 1 + 1 + 3
5 = 1 + 4
5 = 5
5 = 4 + 1
5 = 3 + 2
5 = 3 + 1 + 1
5 = 2 + 3
5 = 2 + 2 + 1
5 = 2 + 1 + 1 + 1

looking above you can see that there is some sort of recursive relation. You just need to think about it a little more.

mrnutty 761 Senior Poster

I think he means for example :

5 = 1 + 1 + 1 + 1 + 1
5 = 1 + 1 + 1 + 2
5 = 1 + 1 + 3
5 = 1 + 4
5 = 5
5 = 4 + 1
5 = 3 + 1 + 1
....

EDIT: too late.

mrnutty 761 Senior Poster

Yes, your not quite done yet. Your line 4 is not quite yet correct. Here is what you have to do:

check if the last digit is a 7, if so then return 1 + count(n/10) if not then return 0 + count(n/10)

mrnutty 761 Senior Poster

Not quite, you need to check if n < 7, if so then stop and return 0 else you should check each digit for a seven.

mrnutty 761 Senior Poster

Your attempt is close. You need to change line 3. Also line 4 can be reduced to simply if(n % 10 == 7) return 1 + count7(n/10) Line 3 is going to be your base case. When should you stop in ALL cases? For example in you base case if n = 71, then (71 % 10 != 7) == true, hence your function stops and returns 0.

Base cases is a case where the input n is the minimal number possible for the recursion to work. So what range of value passed in should the function automatically just stop?

mrnutty 761 Senior Poster

I did this for my university like 3 years ago. The exact same problem. I still have it, for a price.

jon.kiparsky commented: Funny. +0
mrnutty 761 Senior Poster

Yes I can. But instead of giving you the answer. I am going to help and guide you through this long and tough journey to find the number of magical sevens in a given integer.

So to start, do you have any ideas? Does not have to be in code, just basic ideas?

mrnutty 761 Senior Poster

Take out the template<class T> in line 8.

mrnutty 761 Senior Poster

Again more corrections :

while (read>>Name)
    {
        l.push_back(Name);
    {
//...

  while(temp !=l.end())
    {
       cout<<*temp<< " ";
    {

For loops you use the curly braces not the parenthesis.

mrnutty 761 Senior Poster

Yes, first you need to use C++ fstream, instead of C functions, and incorporate getline() command like so :

ifstream fileInput("input.txt");
if(!fileInput){ cerr << "File not found\n"; }
string line;
while(getline(fileInput,line)){
 cout << line << endl; //prints line by line
}
mrnutty 761 Senior Poster

Correction:

while(iter !=aList.end())
    {
        if( *max<*iter)
            max=iter;
        iter++;
        //return max;
    }
 return max;
mrnutty 761 Senior Poster

Easy fix, is the have a default do-nothing displayEntity for ark::Entity. Alternative is to rethink your design. It looks like your design went bad somewhere, if your left to do the above hack.

mrnutty 761 Senior Poster
Fraction Fraction :: operator* (const Fraction& fr2)
{
     int numen = (numerator * fr2.numerator);     
     int denom =  (denominator * fr2.denominator);
     return Fraction (numen, denom);  // Trouble here
}
mrnutty 761 Senior Poster

I will delegate the explanation to this site,

mrnutty 761 Senior Poster

There is no need to redefine true/false.

mrnutty 761 Senior Poster

So do you know how to read in from a file? google getline()

mrnutty 761 Senior Poster

Do you know what this data means ?

0 1 1 0 1
1 0 1 1 1
1 1 0 0 0
0 1 0 0 1
1 1 0 1 0

Read it like this way :

1 2 3 4 5
  ----------
1 |0 1 1 0 1
2 |1 0 1 1 1
3 |1 1 0 0 0
4 |0 1 0 0 1
5 |1 1 0 1 0
  ----------

So for example, Node1 is connected to Node2, Node3, and Node5.

So that should give you an idea.

mrnutty 761 Senior Poster

What exactly is the problem?

mrnutty 761 Senior Poster

Discard the char* and use std::string

mrnutty 761 Senior Poster

You cannot as of right now, seperate the template definition from its implementation. They both need to go in the same file.

mrnutty 761 Senior Poster

Call it like so : display( (num1+num2+num3)/3.0f) );

mrnutty 761 Senior Poster

average of 3 numbers = (number1 + number2 + number3)/3.0f

mrnutty 761 Senior Poster

One way is to do the following:

void display(float average){
 cout.precision(2);
 cout << average << endl;
}

Or you can truncate the number to 2 decimal place like so :

void display(float average){
 cout << truncate(average,2) << endl;
}
//use integer division
float truncate(float num,int decimalPlaces){
 int factor = std::pow(10,decimalPlaces); 
 int tmp = num * factor;
 return tmp/float(factor);
}
mrnutty 761 Senior Poster

how would you write a for loop that calculates the total investment over 40 years if a one pays $500 each month but every 2 years the payment increases 10%?

I wouldn't. Instead, I would post in a forum by disguising the question by asking the members in the forum, how would they write this. Actually, come to think of it, your statement above would suffice as a disguised question.

mrnutty 761 Senior Poster

You cannot do that in C++.

mrnutty 761 Senior Poster

Also consider using string/list<int> instead of ints[]. That way its easier to change the whole array if you need to.

mrnutty 761 Senior Poster

According to this documentation:

http://www.cplusplus.com/reference/algorithm/search/

The search() function is overloaded to provide 2 variations of the function to the user, depending on program needs; the first version takes 4 args, the second takes 5.

Thanks for the correction.

mrnutty 761 Senior Poster

the search function takes in 4 arguments not five.

What you want is something like this:

//returns true if source contains target with case ignored
bool no_case_compare(const string& source, const string& target){
 string srcCpy(source.size(),0);
 string targetCpy(target.size(),0);
 std::transform(source.begin(),source.end(),srcCpy.begin(),tolower);
 std::transform(target.begin(),target.end(),targetCpy.begin(),tolower);
 return srcCpy.find(targetCpy) != string::npos;
}
mrnutty 761 Senior Poster

For you exception handling, it would make sense if you propagate the exception to the client instead of throwing it yourself and catching it. If thats the case then you need not to throw any exception, you can simply use a boolean statement. And to further extend your exception handling, create your own exception class call it NegativeDimensionException which extends from the java standard exception. Then you can throw a more meaningful exception which lets the user know what to specifically to expect. Its just an idea.

mrnutty 761 Senior Poster

Well the point is to learn how to code function and not learn how to use the java library. Although later on you will almost always. Here is some psuedo code that might help:

Function max(a,b,c)
  max(a,b)->x
  return max(c,x)
End Function

Function min(a,b,c)
  min(a,b)->x
  return min(c,x)
End Function

Function Ceil(x)
  return integerPartOf(x+1)
End Function

Function Trunicate(x)
 return integerPartOf(x)
End Function

I'll let you take it from there.

mrnutty 761 Senior Poster

I'm not sure what you mean, post example of what you have and possibly what you are trying to achieve.

mrnutty 761 Senior Poster

Why are you using anonymous functions? Its very unclear. Also your start action listener is wrong I think. It goes into an infinite loop.

mrnutty 761 Senior Poster

Can you post with code tags. Thanks.

mrnutty 761 Senior Poster

Oh didn't see the finite part. Yes your right.

mrnutty 761 Senior Poster

So the easiest thing for you todo is make this into classes. I'm guess you don't know about classes that much. So the next best thing you can do is make this into functions.

Here is a brief skeleton.

//#include everything
int main(){
 const int MAX_COLORS_TO_GUESS = 4;
 string guessColors[MAX_COLORS_TO_GUESS] = { "yellow","green"...and so on };
 std::vector<string> correctColors(guessColors,guessColors + MAX_COLORS_TO_GUESS);
 std::random_shuffle(correctColors.begin(),correctColors.end());
 int correctColorsGuessed = 0;
 int correctPositionOfCorrectColorGuessed = 0;
 int numberOfTries = 0;
 while(correctColorsGuessed != MAX_COLORS_TO_GUESS && correctPositionOfCorrectColorGuessed != MAX_COLORS_TO_GUESS ){
   std::vector<string> userGuessedColors = getUserGuessedColors();
   correctColorsGuessed = getCorrectColorsGuessed(correctColors,userGuessedColors);
   correctPositionOfCorrectColorGuessed = getNumberOfCorrectPosition(correctColors,userGuessedColors);
 ++numberOfTries;
 }
 printInformation(numberOfTries,..and more stuff if you need it);
}

This seems like the mastermind game? Now all you have to do is implement the needed
functions. Make sure they are simple. If they get complicated, then break the function into sub-functions.

mrnutty 761 Senior Poster

What information you got :

a) 3 husband and 3 wives.
b) A Boat to transport a couple/wife/2 wives

Objective: Transport 3 husband and the wives by the rules governed by (b).


So With that think and list all possible class names you need?
Do you really a Person class?
If so, should you have a Husband compose the Person class?
If so, does the husband need to know about his wife? Can you do it without?
Do you need a GameTransportation class that transports appropriately?

What else do you need? Take time to think and write down everything. After all this will be your job after your done with this "pre-game".

mrnutty 761 Senior Poster

You don't call Accumulate anywhere. Call it inside your constructor.

mrnutty 761 Senior Poster

Good Job.

mrnutty 761 Senior Poster

The only way that loop terminates is if result = true and current.getNext() = null.

What you want is the binary AND logical operation not the binary OR logical operation.
Also there is no need for line 10. result will stay false unless it is true, and if it is true then the target is found and you can break.

The result in the while loop is really not needed, you can simply use a break statement like so :

public boolean contains(Celebrities target){
 LinearNode<T> start = contents;
 boolean isFound = false;
 while(start.getNext() != null){
   if(_equal(target,start)){ 
       isFound = true; break;
    }
  start.moveForward();
 }
 return isFound;
 }
}
mrnutty 761 Senior Poster

As of right now, C++ does not allow you to separate template header and its definition.
You need to put the template definition in the same file as the header.

mrnutty 761 Senior Poster

Do you think a Finite Automaton can represent a PDA? If it can the you would have proved that PDA = FA

mrnutty 761 Senior Poster

You guys see it as bad coding, while I see it as an art of stupid.

mrnutty 761 Senior Poster

My guess is that you need to override indexOf(Object obj); Post your code including the imports.

mrnutty 761 Senior Poster

If you do not go towards the better solution of using the above links, then what you want is to use an array. Work by digit by digit.

mrnutty 761 Senior Poster

I always find myself using the template pattern. The decorator pattern is pretty elegant.

Just some thought, visitor pattern is rarely used, at least for me. And usually, unless its the rare case, using Singleton Pattern is not the answer. Factory patterns are ok. They're nothing unusual. But for the most part, I just code, and find myself using patterns without thinking about them. As for my number one idiom, I don't have one. I haven't got enough experience to claim a favorite.