mrnutty 761 Senior Poster

They all suck and have their weakness. So there isn't one.

mrnutty 761 Senior Poster

You want something like this :

int getIntGrade(char letter){
 if(letter == 'A' || letter == 'a') return 95;
 if(letter == 'B' || letter == 'b') return 85;
 //and so on
}

And if you want to go the other way it would look something like this.

char getLetterGrade(int grade){
 if(grade > 90) return 'A';
 else if(grade > 80) return 'B';
 //and so on
}
mrnutty 761 Senior Poster

>> I want to get some feedback on my code to facilitate improvement.

Drop the array_begin and array_end pointer stuff. Use the [] operator instead on the passed in array. As pointed out, forget about register keyword. BubbleSort is slow already so drop the early exit boolean stuff and just simplify it all. That way you have at least something thats readable and just as slow-ish.

mrnutty 761 Senior Poster

In C++ you should avoid pointers. In this example, you can just use a static object like so :

Shape s = Shape(10,10);

no need for the new operator or pointers.

mrnutty 761 Senior Poster

You should make the whole iterator class templatized.

mrnutty 761 Senior Poster

another thing, is that you might want to create a conversion operator.

mrnutty 761 Senior Poster

@OP: Your looking at it the wrong way, instead of letting the client check if a circle is intersects a rectangle, have the rectangle have a function called contains(Circle c) and call it like so, rectangleObject.contains(circleObject);

So that way there is more encapsulation, and a better design. Of course you would probably have to have getX() and getY() eventually, so might as well have a getX() and getY().

mrnutty 761 Senior Poster

This is what you want :

string myName = "josh";
string myBrotherName = "tommy";
string input;

cout << "Enter a name : ";
cin >> input;

if(input == myName){
 cout << "Welcome admin\n";
}
else if(input == myBrotherName){
 cout  << "Welcome guest\n";
}
else cout << "Invalid user\n";
mrnutty 761 Senior Poster

@OP: maybe you should use a 1D array to implement a 2D array, using the index conversion, ROW_SIZE*row_index + col_index

mrnutty 761 Senior Poster

Just use the C++ random_shuffle, its been tested for its distribution so its most likely good enough.

mrnutty 761 Senior Poster

This is not quite right: You need to restrict the swapping so that the destination position is always >= the source position.

int rowToSwap = a random valid row that is >= row
int colToSwap = rowToSwap == row?
    a random valid row that is >= col :
    a random valid row

To see why, consider what happens if there are only two elements in the array using the original strategy. We look at the first element and swap it with the second with probability 0.5. Then we look at the second and swap it with the first with probability 0.5. The net effect is to keep the original ordering 3/4 of the time. Not a random distribution.

Oh I see. But if the probability of swapping is 0.5 per say, then the probability of swapping 2 times in a row is 0.5*0.5 = 0.25? So isn't the probability of keeping the original order 1/4? But either way its not equal distribution. So I see your point. Is there a induction proof on the equal random distribution on this method? Just curious.

mrnutty 761 Senior Poster

Is there anyway you can change the design of the program so it doesn't rely on compile time ifs? Seems like you want to implement state machine.

mrnutty 761 Senior Poster

Just some skeleton to help you visualize whats been said above :

for row = 0 to ROW_SIZE
    for col = 0 to COL_SIZE
         int rowToSwap  = a random valid row
         int colToSwap    = a random valid col
         swap array[row][col] with array[rowToSwap][colToSwap]
mrnutty 761 Senior Poster

First you need to be able to represent data in arbitrary length. One way to do that is use a std::vector<int> numbers or even std::string. Personally, I would choose the std::strings, because its easier to work with.

So lets define such a type :

class Number{
 private:
 std::string _numbers;
 public:
 Number(){}
 Number(const std::string& num) : _numbers(num){}
};

Now you need to define the operator interface, you can either use overloaded operators or member functions. I would suggest you to have private member function and implement the overloaded operators like so :

class Number{
private:
 std::string _numbers;
public:
 Number(){}
 Number(const std::string& num) : _numbers(num){}
 //add operator
 std::string operator+(const std::string& rhsNumber)const{ return _add(rhsNumber);}
 //subtract operator
 std::string operator-(const std::string& rhsNumber)const{return _add(-rhsNumber);}
 //divide operator
 std::string operator/(const std::string& rhsNumber)const{return _divide(rhsNumber);}
 //multiply operator
 std::string operator*(const std::string& rhsNumber)const{return _multiply(rhsNumber);}
 //negation operator
 std::string operator-()const;
private:
 //helper function
 string _add(std::string rhsNumber)const{/*add logic here */}
 string _multiply(std::string rhsNumber)const{/*add logic here */}
 string _divide(std::string rhsNumber)const{/*add logic here */}
};
mrnutty 761 Senior Poster

Good points. Either way, the program is trying to distinguish a certain behavior from the kids, and trying to show that the range of kids with the certain behavior seems more likely to succeed. But if this distinguishing behavior is something that obviously would help formulate the child into a better person in the future, then this study isn't surprising. Thats all I'm saying.

mrnutty 761 Senior Poster

This is interesting. But the correlation isn't that surprising since it test the ability to resist. And the ability to resist if a form of self control. And self control helps people make better decision and so on...

mrnutty 761 Senior Poster

Try not to use the raw arrays if you can. But what you want it template deduction.

#include <iostream>
using namespace std;

template<typename T, size_t SIZE>
int arraySize(const T (&array)[SIZE] ){
 return sizeof(array)/sizeof(array[0]);
}

int main(){
 int a[10] = {0};
 cout << arraySize(a) << endl;
}
mrnutty 761 Senior Poster

This code test t2 = 50; is the same as test t2 = test(50); and this code test t3; is the same as test t3 = test();

mrnutty 761 Senior Poster

Create another JPanel, and add the toolbar to that JPanel. And think of that new panel as a toolbar.

mrnutty 761 Senior Poster

This is what you are after possibly.

mrnutty 761 Senior Poster

Seperate the body and the code. Define the skeleton first and then the body in seperate files. or the same.

mrnutty 761 Senior Poster

Yes as you figured out you need a forward deceleration :

class Student; //tell compiler that you are going to create a Student class later

class Subject{
    vector<Student> list_of_student;
};

class Student{
    vector<Subject> list_of_subject;
};

Your first attempt doesn't work because the Student class doesn't exist when you declare std::vector<Student>.

mrnutty 761 Senior Poster

>>all variations of 1-10

There are 10! different variations go through them like so :

1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
1 1 1 5

and so on

mrnutty 761 Senior Poster

Do you specify your own compare function or use the default?

mrnutty 761 Senior Poster

Do you check if p.first points to the correct element?

mrnutty 761 Senior Poster

Remember a set can only have distinct elements, that might be the reason why it fails sometimes.

mrnutty 761 Senior Poster

Change to this input(carSpeed); Also use code tags.

mrnutty 761 Senior Poster

should sortPrint first sort the vector and then print it out?

Do something like this :

void sortPrint(std::vector<int> v){
 //sort v using your sorting algorithm?
 //print out each element in v;
}
mrnutty 761 Senior Poster

sort the vector inside the sortPrint function and then print it?

mrnutty 761 Senior Poster

This thread was a waste of typing.

mrnutty 761 Senior Poster

No you need to check if the current index location has the same value as the previous location and repeat until the end of string

mrnutty 761 Senior Poster

How about a skeleton :

string uniqify(const std::string& source,int currentLocation){
 if(source.length() <= 1) return source;
 else{/*code here */}
}
string string_clean(const std::string& str){
 return uniqify(source,0);
}
mrnutty 761 Senior Poster

Ok show us how/where you call the allocation for texture. Presumably, you only need to allocate once for the texture.

mrnutty 761 Senior Poster

@FBody thats a feasible solution, but the problem I see that is that the when adding new actions/commands, the code increases a lot. I would advice to apply the command pattern for this situation, or possible thevisitor pattern

mrnutty 761 Senior Poster

Wait let me get this straight, you have a command, something like this : "assign 8 strength . And this should assign strength to 8. So in general is the format going to be like so : command number attribute ?

mrnutty 761 Senior Poster

Give us your bank account details too, so I can send you some cash, mwaaaahhaaaahaaahaaa!

You know, I was just about to but then I saw that "muhahahaha..." laugh and made me think otherwise.

mrnutty 761 Senior Poster

Oh for crying out loud. You can have me as a friend on there, but I never use it. Seems like a load of saddoes to me.

I don't get where everyone is getting the impression that I am looking for a friend from here? I'm just curious on how people look, since we all create an image based on the person's post and intelligence.

mrnutty 761 Senior Poster

Why doesn't your function prototype look like this then :

template <class T>
list<T>::iterator maxLoc(list<T>& aList);
mrnutty 761 Senior Poster

How about just using templates?

mrnutty 761 Senior Poster

Seriously, what are you a geek or something? LOL

Yeah you dont wanna add me because I'm devilishly handsome and a pathological liar. Actually I do wonder what people look like and when you see the face of people on daniweb, for example, it is more of a surprise, because the dont look what you imagined them to look like at all. I think if I revealed myself A LOT of people would be surprised,I'm actually a really nice guy =/

I for one, know that you have a kind heart, so I'm not surprised at all. Anyways, I am curious on your appearance. I don't know if I would think different of you, if I seen your face. I might, but probably not. Anyways, if you feel comfortable, feel free to post a pic or something.

mrnutty 761 Senior Poster

> I calculated that this function gives the number of sequence that sums to a number n

I seriously doubt that.

Yea your right, I calculated the mapping of each n wrong. Tried to do it with the correct value and didn't come with a definite answer, although I got an approximation. Anyways. Thanks for the correction.

mrnutty 761 Senior Poster

No I just wanted to see people face and how they looked. No need to add if its not necessary.

mrnutty 761 Senior Poster

Try using this to hash strings:

unsigned long hash(unsigned char *str){
        unsigned long hash = 5381;
        int c;

        while (c = *str++)
            hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

        return hash;
    }

Then you can incorporate this for the person's last and first name. The algorithm above was found in google.

tylerjgarland commented: While I understand his answer, the google sarcasm is not helpful. I understood hashcode, I just needed help fitting it into a closed array. +0
mrnutty 761 Senior Poster

I guess not many people around here have facebook. Anyways, happy holidays.

mrnutty 761 Senior Poster

Oh ok. you just dumped a bunch of problems on us and expected us to give you the answer so I expected some money in exchange. But now I know you actually want help. How about you show us what you got do far.

mrnutty 761 Senior Poster

Sure long as I get no stalkers lol

mrnutty 761 Senior Poster

Your definition of bar() is technically correct syntax, but your usage is illegal because you are returning a reference to a local variable

Correction. bar() returns a copy of the text "world".

mrnutty 761 Senior Poster

Anyone here have an facebook? I'm ready to reveal myself. Be aware, I'm kinda young, and I have a baby face, as I've been told. Anyways, here is firstPerson's true identity. I know i'm not very important, but you guys are like my online buddies. I feel at home here. Now anyone else has the courage to reveal themselves?

mrnutty 761 Senior Poster

I'll do it for $35 an hour?

jonsca commented: Cheap! +5
mrnutty 761 Senior Poster

Just to explain more :

@Op : you first question about the Policy::create(...)

The reason why thats there instead of simply just new classT is because of its flexibility.

When the user creates a MakeT class it could now specify how the class is created.
If it simply uses the new operator, then it could use the default Lazy class. Otherwise, for instance, the user may have a certain special way to create an object.
If so then he could simply say MakeT<Classt, SpecialWayToCreate> m; so now, each Classt is created using the SpecialWayToCreate. As you can see, this is much more flexible.


As for your second question : its called a reference-pointer. It enables the function
not only change the value the pointer points to, but also its address and where it points to.