mrnutty 761 Senior Poster

Do something like this :

#include <iostream>
#include <ctime> //needed for time

int main(){
 srand( time(0) ); //seed random number generator
 string name[3] = { "luke" , "tony", "robert" };
 string randomName = rand() % 3; //return a number from [0,3)
}
mrnutty 761 Senior Poster

This is a simple demonstration of meta programming. It shows the idea
of meta programming. It basically calculates the factorial of a number
before runtime. When I first saw the concept of meta programming,
it blew my mind so badly. I was so surprised that one could do this.
Its soooo kewl, don't you think?

avarionist commented: I'm still working out how exactly it works but man speed glorious speed +1
mrnutty 761 Senior Poster

I like the second one, if it would compile :) And why are u incrementing m two times per a loop.

mrnutty 761 Senior Poster

wait, can you repeat your question 1 more time, in a more clear and less
anxious manner.Thanks and btw, this

BigInt number1;
          BigInt number2(3484858);
          BigInt number3("89347893478349234045");

          cout << number1 + number2 + number3 << endl;

is possible.

mrnutty 761 Senior Poster

Absolutely, you can do that. But you're still tying yourself to using a vector, because you're returning a vector<string>::iterator object. Imagine if down the road I need to use a queue instead -- now what? I'm thinking of doing that, but it still feel like I'm violating encapsulation here.

well do you think you will need queue "down the road" ?

Why do you think its violating encapsulation?


And btw, queue does not have iterators.

mrnutty 761 Senior Poster

If i'm not mistaken, you should be able to return the vector iterator.
Maybe an example will help.

#include <iostream>
#include <vector>
#include <string>
#include "print.h"

using namespace std;

class Items{
public:	
	typedef std::vector<string> ItemType;
	typedef std::vector<string>::iterator Iterator;
	typedef std::vector<string>::const_iterator constIterator;
private:
	ItemType _weapons;
	ItemType _potions;
public:	
	Items(){
		_weapons.push_back("sword"); 
		_weapons.push_back("knife");
		_weapons.push_back("club");
		_weapons.push_back("Ak47");
		
		_potions.push_back("20Plus");
		_potions.push_back("40Plus");
		_potions.push_back("100Plus");
		_potions.push_back("HealAll");
		_potions.push_back("Revive");
	}

	Iterator WeaponStartIterator(){ return _weapons.begin(); }
	Iterator WeaponEndIterator(){ return _weapons.end(); }
	constIterator WeaponStartIterator()const{ return _weapons.begin();}
	constIterator WeaponEndIterator()const{ return _weapons.end();}

	constIterator PotionStartIterator()const{ return _potions.begin(); }
	constIterator PotionEndIterator()const { return _potions.end(); }
	Iterator PotionStartIterator(){ return _potions.begin(); }
	Iterator PotionEndIterator() { return _potions.end(); }

};
void printItems(Items::constIterator start, Items::constIterator end){
	while(start != end){	cout << *start++  << " ";	}
}
int main(){
	Items itm;
	cout << "WeaponList { ";
	printItems(itm.WeaponStartIterator(),itm.WeaponEndIterator());
	cout << "}"<< endl;
	
	cout << "PotionLIst { ";
	printItems(itm.PotionStartIterator(),itm.PotionEndIterator());
	cout << "}" << endl;
}
mrnutty 761 Senior Poster

The function prototype :

int operator +(Polynomial &Poly1, Polynomial &ConstValue);

The function definition header:

int operator +(Polynomial Polynomial1, Polynomial ConstValue)
mrnutty 761 Senior Poster

There are a couple of ways you can do this.

First you need to create a BigInteger class. It basically adds and subtracts arbitrary long digits.

One way to do this is to use a string to hold the number, internally.
Then create an add function, that adds one BigInteger to another.
The add function is what you need to create. Apply the same logic step by step as you would if you're adding in paper. Give it a try.

Another way to do this is to separate the digits. For example, get
the input as a string in the BigInteger class constructor. Then internally
have 2 __int64 variable, holding the high and the low byte of the number passed in. Then work from there.

I assume for now, option 1 is easier.

mrnutty 761 Senior Poster

If you need optimization that badly, then make a look up table with cosine and sinus table.

mrnutty 761 Senior Poster

string concatenation.
If you're saying "...default***/test.txt" is the path, where the user picks
what goes in ***, then you can achieve this with string concatenation. For example :

string startPath = "...//default";
string endPath = "//test.txt";
string pathNumber = "";
string finalPath = "";
cin >> pathNumber;
if(pathNumber != "-1"){
  finalPath = startPath + pathNumber + endPath;
}
else {...}
ofstream file(finalPath.c_str());
//...

Thats the general idea, you don't have to use that much variables.

mrnutty 761 Senior Poster

Not quite sure what you mean, but maybe this will help you :

switch(option){ 
 case 1 : doCase1(); break;
 case 2 : doCase2(); break;
 case 3 :
 case 4 : doCase3And4(); break;
 default : break;
}

The idea is that if the user picks 3 or 4 it will go into case 4. If this is not what you needed then perhaps you can post an example of what you mean.

mrnutty 761 Senior Poster

>>
error C2662: 'Number::operator -' : cannot convert 'this' pointer from 'const Number' to 'Number &'

That means you are passing a const object into a non-const function.
Const-correct your functions, that is do something like this if you can :

Numbers operator+(const Number& num)const;
mrnutty 761 Senior Poster

show the whole program

mrnutty 761 Senior Poster

your idea is fine. Just don't use stl containers. use regular arrays if you wan't speed since this is all about speed.

mrnutty 761 Senior Poster

1) use code tags. /* code goes here */
2) Whats your problem. We aren't compilers. We don't just compiler the
code in our head and find errors, although we might be able to.
3) for( int n = 0; n!= 100; n++) re_read_Steps1And2();
4) Fix those things and post back neater and clearer question.

mrnutty 761 Senior Poster

This would be just like adding regular numbers, but instead of carrying when the addition of 2 digits gets 10 or bigger, you will carry when the addition of 2 numbers get 2 or bigger.

First now how to add binary numbers in paper. When you try to add binary numbers in paper, on the side, label the steps that you took
to add 2 binary numbers. Make sure to watch out for weird cases.

mrnutty 761 Senior Poster

sure :

class A{
    public:
    const static string ClassName; //declare
};
const string A::ClassName = "A";//initialize
mrnutty 761 Senior Poster

*(A+0) == A[0];
*(A+1) == A[1];
*(A+2) == A[2];
//...
*(A+n) == A[n]

mrnutty 761 Senior Poster

I have tried to compile it and I do get linker errors with PrintMessage() and for InputString(). I am using the Dev-C++ compiler and the error log calls them undefined references.

check the signature of the function.

mrnutty 761 Senior Poster

Here, less work for me to explain.

mrnutty 761 Senior Poster

First off, this code :

#define false 0
#define true 1

char s[100];
char sr[100];

Is bad already. You do not need to use define. C++ already has, "true" and "false" defined.

Next :

int main(){
    InputString();
    system("PAUSE");
    return 0;
}

No need to use a system command. Use cin.get(); Do not use global variables when local will do. Make your getInput() function return the input. Why are you using a char array. Use the string header.
Get into good habit early. I would suggest your main to look something like this :

int main(){
 string input = getInput();
 bool isPalin = isPalindrome(input);
 handleMessage(isPalin);
 return 0;
}

Next :

void InputString(int n=0)
{
     cout<<"Input the string you wish to be tested";
     cin.getline(s, 100);
     if(strlen(s)>n)
     PalindromeTest();
}

There is a lot of bad things that could happen. Instead of listing them all
I will tell you the solution. Use C++ strings. Do not use char arrays.
The whole point of this exercise is to get your logical thinking skills up.
So don't make use of the strrev function. Its a good idea to use the stl
for solutions, but thats not the point here. Create your own for loops
and apply some logic.

I hope that helped somewhat, and you take it into consideration.
Because what you got right now is not very good.

tux4life commented: Solid advice! +8
mrnutty 761 Senior Poster

Just imagine a ball falling in an uneven ground. Before the ball hits
the ground, you have to know some information. Its (x,y) position. Its
velocity vector. Its mass. And so on depending how detail the object is represented. As suggested, if the ball falls vertically, then you just take
its x position. This x position of where the ball is at time t, when falling
will be the same as the point of impact. Thus as suggested, you can just
get the terrain's height from the x position of the ball. Now all you
have to do is calculate the new velocity vector of the ball after the point
of impact. If the ball is falling from any angle, then it will be almost be
the same solution. You use the ball.x position. The calculation for the
velocity vector after the point of impact will be a little different.

mrnutty 761 Senior Poster

WE have the && operator, the || operator, but xor is only implemented
as '^' . And this causes a lot of confusion.

Let's take this case: 2 == 2 ^ 2 generates a logical operation instead of bitwise operation , therefore returning a true value.

What do yo think: Should the C++ standard have a xor logical operator? Why ?

Usually, noobs have problems with this. This is not mathematics. Its programming. Get use to its syntax.

mrnutty 761 Senior Poster

Basically, it is a block of code identifies with a name.

Not completely.

Every functions has a domain and a co-domain and its definition.

The domain is the valid range of numbers that can be accepted.
The co-domain is the valid range of numbers that the function can map to.

The definition of a function gives some explicit way to convert the input into an output.

For example, consider the function F: R - >R, where F(x) = x*x.
The function domain is R( all real numbers), the function co-domain
is R(all-real numbers) and its definition is F(x) = x*x.

Now in code its very similar :

int F(x){ return x*x; };

Now the domain is implicitly stated as all real numbers. So we do not check if x is an imaginary numbers( that and we can't anyways).
The co-domain is all real number and is implicitly stated by the return type of the function, int. The definition is x*x, which is in the
body of the function.

So that is a function.
It has a domain, the valid range of numbers the function can accept.
It has a co-domain, the valid ranges of numbers the function can map to or "spit out".
It has its rule, or its definition.

mrnutty 761 Senior Poster

>>I did not mean operator=, I surely would have said that if I meant it and in fact because of the nature of operator= and operator+= they would return the same type, both const references because the return the object that was assigned to.

I'm sorry, Right now I see no good reason to return a constant reference for the += operator. It looks like it would obfuscate the code.
For example, If you return a constant reference for the += operator, then you are allowed to do this :

Foo f; //Foo(): val(0){}
Foo g = Foo(1); //Foo(int val);
Foo h = Foo(2);

cout << (F += g).getX();
cout << (((F += g)+=h)).getX();

It just looks like you are able to do more crazy code. So it would be nice if you can give some good reasoning to return a const reference for overloading the += operator.

And thanks for the other explanation. Didn't realize what you mean exactly by your first post.

mrnutty 761 Senior Poster

>>If I erase the nodes from the list (using list::erase) will this delete the actual objects themselves, or just the pointers on the list.

It will delete the object, you don't need to worry about freeing the memory any longer.

mrnutty 761 Senior Poster

Let me emphasize this a little more," First I would ask, "WHY?" Why take the slowest sort and slow it down further with the overhead of more function calls and additional memory usage? And, you will have a sort that may overflow the stack on sorting a large array ".

mrnutty 761 Senior Poster

It can be good to return references to objects when you can, for instance the return from operator+= can be a reference.

Maybe you meant, when overloading the = operator. It doesn't make sense to return a reference when overloading the += operator.

It is not possible to implement operator+ returning a reference and correctly handle memory.

Curious on why not.

mrnutty 761 Senior Poster

Its there to help you with name conflicts.

For example, take this code :

namespace A{ void f(){} }
namespace B{ void f(){} }

They are fine even though f() is defined twice. You can use either one like so, A::f() or B::f(). Although there is a little more details about namespace, I'll let you study those.

mrnutty 761 Senior Poster

>> You should always try to write the fastest, smallest code possible before attempting to make it simple and correct

LOL...

mrnutty 761 Senior Poster
cin >> operation;
if(operation == 'X') break;
mrnutty 761 Senior Poster

If yor wan't to sort it based on its z-values then use this predicate:

template<typename Object>
struct PointerComp{
 bool operator()(const Object* lhs, const Object* rhs)const{
  return lhs->getZ() < rhs->getZ();
}
};
mrnutty 761 Senior Poster

Dude, I practically gave you the answer. Try something like this :

~DLList()
{        //Type is any data type
 	ListNode<Type> *temp = head;
	  while(temp != NULL){
  	  head = head->next;
	  delete temp;
                  temp = head;
	}
	length = 0;
}
mrnutty 761 Senior Poster

Use binary predicate :

template<typename Object>
struct PointerComp{
 bool operator()(const Object* lhs, const Object* rhs)const{
  return *lhs < *rhs;
 }
};
//....
std::vector<game_object*> objects;
//...
std::sort(objects.begin(),object.end(),PointerComp<game_object>());
mrnutty 761 Senior Poster

This is not java, setting head and tail to NULL will not free the memory. Thus you have a memory leak. Try something like this :

void destroy(){
 Node *pos = _head;
 while(pos != NULL){
   increment(_head); //make head point to next element
   release(pos); //delete the memory pointed by pos.
   pos = _head; 
 }
}
mrnutty 761 Senior Poster

What is the signature of this function? glColor3f is this a function you have specified yourself? If so it seems as though the function is expecting an int where you have provided an array. Did you forget a '*'?

nope. glColor3f is the API's function. He needs to show more code.

mrnutty 761 Senior Poster

I am assuming you mean the first word that starts with 'w'.

If its a structural sentence, then find the first 'w' Then check if the character before it is a space. If so then its what you are looking for. Be sure to watch out, if w is found at index 0. Then thats your answer.

mrnutty 761 Senior Poster

Lets examine this for a second :

while(!isEmpty()){
	ListNode<char *> *temp = head;
	head = head->next;
	head->previous = NULL;
	delete temp;
}

Assume that the list contains, A,B,C. Where A-C can be anything.
Now your delete function will do this :

//Iteration # 1

1) while(!isEmpty()) is true go the code block will get executed.
2) ListNode<Type> *temp = head; Now Type is any data type. Now temp points to the head which points to A.
3) head = head->next; now head points to B
4) head->previous = NULL; now head->previous points to A right? So you are setting A to null. Remember temp points to A, so temp now is null. This is called a memory leak.
5) delete temp; now you delete a null, which does nothing.

//Iteration # 2
//now the list is {null B C }, where null means it does not exist
1) while(!isEmpty()) this is true so the block of code gets executed
2) ListNode<Type> *temp = head; Now temp points to the head which points to B.
3) head = head->next; now head points to C
4) head->previous = NULL; now head->previous points to B right? So you are setting B to null. Remember temp points to B, so temp now is null. This is called a memory leak.
5) delete temp; Now you delete a null, which does nothing.

//Iteration # 3
//now the list …

sid78669 commented: Simply Marvelous! +2
mrnutty 761 Senior Poster

i found many replacements of opengl like
darkgdk
DirectX
Allegro
etc...

ANd the one i found is somewat similar to wat i want
that is sfml . I guess i want some other similar programs like sfmls

Try using an engine that supports opengl. It reduces many things
you have to do, like drawing stuff, animating, making shadows, and so on.

mrnutty 761 Senior Poster

Well its pretty simple ,
ITs like in opengl , handling the primitives like circle , sphere is difficult and we have to dereive our own algorithm.
Is there any libraries which can directly do that , by just calling the cicirle primitive or something analogus to that.

If your using glut with opengl, then glut has stuff like :

glutSolidSphere(...), glutWireSphere(...) and more types of solids.

If not the try goggling it and see what comes about. If all fails
then write your own library, and re-use it everytime you need it.
It would be good practice.

mrnutty 761 Senior Poster

Usually its not a lot, it should be manageable by one person. Can you show some examples of what you mean? Maybe we can help rethink your
design.

mrnutty 761 Senior Poster

See if something like this would work for you :

string toString(int num){
	stringstream strm;
	strm << num;
	return strm.str();
}

Then you can just use each digit like so :

string num = toString(125);
//num[0] == '1';
//num[1] == '2';
//num[2] == '5';

/* or if you want the int value */
int firstDig = num[0] - '0';
int secDig = num[1]  - '0';
int thirdDigit = num[2] - '0';

The " num[*] - '0' "; is a conversion from a char digit to a int digit. You
can create a function that does this to help make it look better :

int toIntDigit(char aDigit){ return aDigit - '0'; }
/* use it like so */
string num = toString(500);
int firstDigit = toIntDigit(num[0]);

Just throwing an idea out there.

mrnutty 761 Senior Poster

If the bounding box does not rotate, then I'm thinking you can
compare the distance between the box. Just like you would in a circle to
circle collision test. What do you think. I can't really test it out, but I am guessing you can.

mrnutty 761 Senior Poster

yes you need to clear the stream. Use something like this :

#include <limits>
#include <string>
//...
void waitForInput(){
 std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
 string dummy;
 getline(dummy,'\n');
}
mrnutty 761 Senior Poster

Are the boxes rotated, will they ever rotate?

mrnutty 761 Senior Poster

There is a lot to learn. As most things, it will take time and practice. So don't expect it get it soon. There are many comprehensible tutorials about them over the net, so go search them. And maybe come back with a more specific question.

mrnutty 761 Senior Poster

So, I am guessing you learned about structs and classes? If so then
go ahead and make a Residential and Commercial class. In fact you can
use some inheritance, and polymorphism here, but I guess we can keep it simple.

jonsca commented: Yes, I think it needs a queue, a finite state machine and some template programming too. +2
mrnutty 761 Senior Poster

Yes. Classes is one way. For example here is a simple Type.

class Int{
  int val;
 public:
  Int()  {val = 0;}
  Int(const int& initVal) { val = initVal; }
 /* more functions */
};

You could make it just like a int variable, but it has its difference. Similarly, you can Imagine
a class called say, Fraction{ ... } that acts like a fraction, but internally holds a numerator and an
denominator, both type int. But you treat the Fraction class like its a regular data type. So you might multiply
it to another fraction, or add and so on.

mrnutty 761 Senior Poster

Is there another variable named "b" ? Show more code.

mrnutty 761 Senior Poster

I don't think so. In "ccccc" there are only 5, but your formula would product (2^5)-1 = 31.

Oh, wait, should have read the title, he needs only the distinct ones.
I'll try to formulate something.