mrnutty 761 Senior Poster

Psuedo-Code

for i = 1 To N
 if(N % i == 0) printFactor(i);
mrnutty 761 Senior Poster
mrnutty 761 Senior Poster

umm, haven't done opengl for a while, but I think you are looking for gluPerspective(..). Its areguments are the following

void gluPerspective(	GLdouble  	fovy,
 	GLdouble  	aspect,
 	GLdouble  	zNear,
 	GLdouble  	zFar);

So just specifiz zNear to be some negative number and zFar to be an adequate high number and you should be fine.

mrnutty 761 Senior Poster

>>empnum(0,"","",0.0);

remove that

mrnutty 761 Senior Poster

hi I wonder who is the smallest boy to know any low level programming language
Eg:C++ or java etc exept the qbasic

Wow that might be the most non-intellectual question I have read on this forum. You should get a prize or a cookie.

mrnutty 761 Senior Poster

It definitely can be done in linear time, just because of its special nature, that is because of the properties, 1,2,3,4 listed above.

mrnutty 761 Senior Poster

It's easier to learn vectors first. I know because I've taught it both ways, and I could tell where my students had trouble from the questions they asked.

Your a teacher? A professor? Cool, didn't know that. Essentially when you teach them about vectors, you are enhancing their intuition about what a function does, and maybe giving them insight on how to implement it as well. But this can be achieved through teaching as well. I might be biased because I wasn't taught vectors first than arrays. But since you are a teacher, you have more experience with this kind of stuff, so I'll step aside.

mrnutty 761 Senior Poster

>>would be silly to avoid

I would generally agree, but if OP is trying to learn the semantics of arrays or even C++, then he needs to get a good grasp of the basics before using something like vectors. So that he knows exactly what each function will do for him.

mrnutty 761 Senior Poster

It makes sense. To help you better, you should post some source.

mrnutty 761 Senior Poster

No body is curious enough I guess. Can anyone suggest an algorithm that runs in polynomial time first? Then maybe go on to linear timing.

mrnutty 761 Senior Poster

Problem statement: Give two sets A and B, devise and algorithm that checks if A and B are disjoint. The following information are given about the set A and B:

  1. |A| = |B| = n, that is number of elements in both sets are n
  2. A and B are not necessairly sorted
  3. All elements in A and B range from [[TEX]0[/TEX],[TEX]n^c[/TEX]] for some constant
  4. You can use O(n) space.

Ask questions if you need to or send me a Private message

mrnutty 761 Senior Poster

Oh damn, wondering how come this thread popped up in the list 1 of the page?

mrnutty 761 Senior Poster

@OP: Why would you want to do that?

mrnutty 761 Senior Poster

ummm, not sure why but how about this idea:

struct EmptyType{}

template<typename T1,typename T2 = EmptyType(),...and so on for some finite amount>
struct TupleType{
 typedef type1 T1;
 typedef type2 T2;
 //...and so on
}

int main(){
 Tuple<int,bool,char,string>
 Tuple::type1 intType = 0;
 Tuple::type2 boolType = false;
 //...
}

What exactly are you trying to do?

mrnutty 761 Senior Poster

Make the make_thirds either a static function or a standalone function.

mrnutty 761 Senior Poster

@ firstPerson,

What areas of the program/code can be made less complex and how?

I'll just touch on some few points, in no particular order:

  • Random class is useless
  • You need to encapsulate the room[] and hazard[] and functions like uppLimit, lowLimit into classes
  • Name your identifiers better. For example, upperLimit is better than uppLimit
  • You main function is too complicated. Again encapsulate the logic in an appropriate class
  • You have couple of "magic numbers" there
  • You functions are too complicated. It could be broken into simpler functions
  • rand() returns an int. RAND_MAX is a integer, therefore by the law of computer programming, at least in C++, and rand()/RAND_MAX = 0, unless in the rare case rand() = RAND_MAX, then its equal to 1.

So your main problem, is that you need to encapsulate it more. Use more classes. Hide the logic from the user. Make main simple. Simpler the better.

mrnutty 761 Senior Poster

>>Secondly the program is well structured, but only one class???

I beg to differ. Its hard to read. Global variables, overly complicated, and definitely not well structured.

mrnutty 761 Senior Poster

I think you need to look at your design again. For some reason what you are doing doesn't seem like a good way to go about it. I would suggest an alternative but, I'm not completely sure how your code works.

mrnutty 761 Senior Poster

How about you give it a try and we'll be here to guide you.

mrnutty 761 Senior Poster

There is not point for Initialize class to have the member private. You would be better of making them priviate. Also Initialize is a bad name. Call it something more meaningful and clear.

mrnutty 761 Senior Poster

almost, e = 1/0! + 1/1!...

so you have to add 1/0! = 1 to each output.


taylor series representation of the constant e

mrnutty 761 Senior Poster
George Bush 70 77
Barack Obama 70 75 80
Bill Clinton 68 70

Does the general format goes like so :

FirstName LastName score1 score2 ... scoreN

or are there only score1 and score2 for all names?

Either way you should do something like so :

struct President{
 string firstName;
 string lastName;
 President(string f = "", string l = ""): firstName(f), lastName(l){}
};

struct PresidentScoreKeeper{
 President pres;
 std::vector<int> scores;
 PresidentScoreKeeper(){}
 PresidentScoreKeeper(const President& p, const std::vector<int> s = ""): pres(p), scores(s){}
};
class PresidentScoreList{
private:
 std::vector<PresidentScoreKeeper> presidentList;
public:
 void add(const PresidentScoreKeeper& p){ presidentList.push_back(p);}
 //more functionalities
};

std::istream& operator>>(std::istream& inputStream, PresidentScoreList& president){
 string line;
 getline(inputStream,line);//KEY IDEA
 President currPresident= prasePresidentName(line);
 std::vector<int> currentPresidentScore= prasePresidentScores(line);
 president.add( PresidentScoreKeeper(currPresident,currentPresidentScore) );

 return inputStream;
}

The key thing is to use getline(inputStream,line). That is, you need to read a line by line for each line in the file instead of character by character.

mrnutty 761 Senior Poster

change this calculation function_one(calculation addition) to this calculation function_one(calculation& addition) and realize the difference.

mrnutty 761 Senior Poster

Umm...maybe something like so :

class Fraction{
private:
 int _numerator, _denominator;
public:
 Fraction(int num = 0, int denom = 1): _numerator(num), _denominator(denom){
  //make sure denominator is not 0!
 }
 int numerator()const{return _numerator;}
 int denominator()const{ return _denominator;}

 int numerator(int n){ _numerator = n;}
 int denominator(int d){ /*makre sure d != 0 */ _denominator = 0; }
 string toString()const{
   return _toStr(_numerator) + "/" + _toStr(_denominator);
 }
 void display()const{ cout << _numerator << "/" << _denominator ;}
 float value(){ return float(_numerator)/_denominator; }

private:
 string _toStr(int num)const{
  stringstream ss;
  ss << num;
  return ss.str();
 }
};

The above isn't compiled but you get the idea.

mrnutty 761 Senior Poster

If you want that then you need to have a fraction class that handles that. So that instead of just displaying its floating values, it can display the numerator and denominator. Thus ultimately, you need to keep a variable for the numerator and denominator. So instead of double re,im you need either Fraction real,imaginary or something like double realNumerator, realDenominator, imaginaryNumerator, ImaginaryDenominator and keep track of each variable. I suggest creating a fraction class that handles those for you. Or just be satisfied with floating numbers.

mrnutty 761 Senior Poster

Ex:

Absolute position: monster.setPosition(100,100); Relative position: hero.setPosition(monster.getX()+100,monster.getY()+100) So in the absolute position, the monster in place in an absolute position at (100,100), while the hero is placed relative to the monster, that is the hero is placed 100 plus the monster x position and 100 units plus the monster y position. So that whenever the monster moves, the hero position will always be 100 + monster.x() and 100+monster.y(); Get it?

mrnutty 761 Senior Poster

Before resorting to inheritance, can you state your actual problem with some actual context? Maybe you have been going about this problem wrong?

mrnutty 761 Senior Poster

Then why not use char in the first place?

mrnutty 761 Senior Poster

@OP: Forget you ever saw that code. Its useless.This insertion-sort_recur(a, n-1);//how does this line work acts like a for loop. It keeps going until it reaches the second element. Then it returns to the third element. Then to the fourth and so on...

mrnutty 761 Senior Poster
string Direction = "i";
cin>>Direction;
while((Direction != "L")||(Direction != "l"))

Make similar changes. Use Direction as an string object, not as a string array.

mrnutty 761 Senior Poster

What exactly do you need help with? Maybe a kick start would help. Here is some ideas:

class Cash{
private:
 int money[10];
public:
 enum {PENNIES,NICKELS,DIMES,QUARTERS, ONE_DOLLAR,FIVE_DOLLAR,TEN_DOLLAR,FIFTY_DOLLAR,ONE_HUNDRED_DOLLAR
public:
 Cash(){
   for(int i = 0; i < 10; ++i) money[i] = 0;
 }
 void add(const Cash& money){/*Add code here*/}
 void subtract(const Cash& money){ /* Add code here */}
 int& get(int moneyIndex){ return money[moneyIndex]; }
 int getTotalCashInPennies(){/*code goes here*/}
};

class Registar{
private:
 Cash initialCash;
public:
 int MakeChange(int iPurchaseAmount, Cash cashIn, Cash & cashOut);
 //more stuff here
};

I'm not sure if thats similar to how your requirements are but that should give you an idea.

mrnutty 761 Senior Poster

[img]http://upload.wikimedia.org/math/e/7/2/e72a9c97103eed0fe72a1975a8fd748a.png[/img]
[TEX]sin(x) = x - x^3/3! + x^5/5! - x^7/7!...[/TEX]

In C++ terms :

//approximates in 3 expansions
float sinus(float x){
 return x - (x*x*x)/(3*2*1) + (x*x*x*x*x)/(5*4*3*2*1);
}

Of course you can use a for loop and a factorial function and a power function to expand it more, but I'm not trying to give you the answer.

mrnutty 761 Senior Poster

@FBody: I don't think thats the problem, because he uses end just like you did in your for loop, for comparisons. And in the line he was pointing to, it definitely shouldn't have a bad pointer exception, if his code were working.

@OP: Can you show us that HList is? I'm guessing its like a Array<list<int>> or something? But more importantly, do you have elements stored at HList for each i?

mrnutty 761 Senior Poster

We'll the situation really didn't call for generic. And to be quite honest, using generic in this situation isn't a good design nor is it flexible.

mrnutty 761 Senior Poster

Couple of ways, one way is to do the following :

//interface
struct Conditional{
 virtual bool preCondition()const=0;
 virtual bool postCondition()const=0;
};
//adapter for default
struct ConditionalTrueAdapter: Conditional{
 bool preCondition()const{return true;}
 bool postCondition()const{return true;}
};

//interface
struct Command{
 virtual void execute()=0;
};

class PauseGameCommand : public Command{
private:
 Conditional condition;
public:
 PauseGameCommand(const Conditional& c = ConditionalTrueAdapter())
 : condition(c){}

 void execute(){
   assert(p.preCondition());
   /* code to pause game goes here */
   assert(p.postCondition());
 }
};

Use it like so :

if(user.clickedPauseGame()){   
   PauseGameCommand();
}
else if(user.clickedExitGame()){
  Conditional c = GameState.getGameCondition();
  PauseGameCommand(c); // for example, say, preCondition checks if it can be paused, and postCondition checks if everything went well
}
mrnutty 761 Senior Poster

Post the ascii art one more time, but this time wrap it under code tags. It looks like your binary search tree does not satisfy the property that all left subtree are smaller than the parent, and all right subtree are greater than the parent.

mrnutty 761 Senior Poster

try putting void stringcomp(string str); right before "int main()".
Also I hope you got a "stringrec.h" that has the prototype of the functions defined in "stringrec.cpp"

mrnutty 761 Senior Poster

Instead of this float f1,f2,f3,f4; use an array like so float point4[4] . There are more benefits to use an array than 4 different variables.
Plus its easier to maintain.

mrnutty 761 Senior Poster

The code FLATMATE resident; creates an instance of FLATMATE. It is called an object. Whenever you create a DUTY object, inside that duty object, is an instance of FLATMATE. So each DUTY object has a FLATMATE object. Its similar to this:

class DUTY{
int resident;
}

the only difference is that instead of it being an int, it is of type FLATMATE. You can initialize FLATMATE object in the constructor of DUTY.

mrnutty 761 Senior Poster

A popular way to handle large matrix is to divide the matrix into multiple chunks and work that way. Try to see if thats possible. Or post the actual problem here and surely someone can help you figure out a proper solution.

mrnutty 761 Senior Poster

>>I get your point, but how many coders won't know what BST means?
A lot! Make a poll. Follow good conventions. There is no need to abbreviate the name. Its not as clear as writing it out fully.

>>Why? I see the benefit if I wanted a general tree, but a BST is the most abstract class I need. Isn't it bad to over abstract?
No, the more abstract you get, the better your code will be in terms of reusability and maintainance. You need to start thinking about these things early so when you are on a job, you can produce the best possible code.

>> can't find any places that can fail and aren't checked somehow. Where do I need to add error checking? By error checking, I meant proper error checking. If something fails, then the user should know about it.

>>It has to be at least a friend though, right? Can you explain the difference between a member function and a friend function that makes a friend function better here?
Not if you have proper implementation. If you provide access to each element in the tree, then one can print his own data. I am not saying give the user access to the nodes, but rather have have some type of traversal.

mrnutty 761 Senior Poster
//O(n)
for (int j = 4; j < n; ++j) { 
  cin >> val; 
}

//O(n)
for (int i = 0; i < j; ++i) { 
  b = b * val;
}

//misformatted?
for (int k = 0; k < n; ++k) 
c = b + c;}
}
mrnutty 761 Senior Poster

I don't like your consistency with your naming conventions. Don't abbreviate it. Just name your variable something that we all can understand. For example, instead of BST(bull shit tree?) call it BinarySearchTree.

So as to your program, in no particular oder:

1) You need to abstract your program more. Define a *interface called Tree. Then
have BinarySearchTree inherit from Tree. This lets user do things like so :

Tree tree = FastInsertRemoveTree();
//code here

Tree anotherTree = BinarySearchTree();

Or if the user wants to change from using BinarySearchTree() to say, GeneralSearchTree() then all he simply has to do is change the rvalue constructor.

2) You need to do error checking.
3) The print function really doesn't make sense to be a member function. Make it a regular function.
4) Follow C++ convention of naming. Usually, in tree data structure, we call the function "insert" instead of "add", and "erase" instead of "remove" and "find" instead of "exists". Its not a big deal, but its better to follow conventions.
5) If you can, implement an iterator, so that will make some of your functions work well with stl.

6) For this function :

ValueType value(KeyType key) const{
   node* found = find(mroot, key);
   return found ? found->value : ValueType();
}

the problem with this is that, found->value can essentially be the same as ValueType(). That makes it a problem because when it can confuse the user whether
the function is …

mrnutty 761 Senior Poster

For Binary search trees or any data structures that are recursive in nature,
you will want to implement a recursive algorithm to solve most of its problems.Its not only easier to write, but its easier to understand as well. So with that said, consider this recursive algorithm that finds the height of a tree.

int BinaryTree:height(){
 return _height(this.root);
}
int BinaryTree::_height(const Node* root){
  if( isNull( root) ) return 0;
  int leftTreeMax = _height(root.getLeft()) + 1; //add one and move down
  int rightTreeMax = _height(root.getRight()) + 1; //add one and move down
  return std::max(leftTreeMax, rightTreeMax) + 1; //account for the root
}
mrnutty 761 Senior Poster

Thanks, my program no longer leaks memory. Do you have any ideas of what I could have done to my original destructor to make it work?

It looks like your trying to iteratively delete the nodes. Your algorithm does not transverse the entire tree, but it transverses all the way to the left, then maybe right,
and deletes that nodes, and exits. So to make your algorithm work, you would need to
modify how do the transversal, in which case, a recursive solution is easier to implement and understand.

mrnutty 761 Senior Poster

Just some technical loop holes in the requirements. No division or modulus operator
used.

#include <iostream>
using namespace std;

int myDiv(int a, int b){
	int ans = 0;
	while(b < a){ ++ans; a -=b; }
	return ans;
}
int myMod(int a, int b){
	return a - (b * int(myDiv(a,b)));
}
void printDigits(int num){
	if( abs(num) != 0 ){
		printDigits( myDiv(num,10) );
		cout << myMod(num,10) << endl;
	}
}
int main(){	
	printDigits(12345);
}
mrnutty 761 Senior Poster

Ok then Note that :

1234 = 4 * 10^0 + 3 * 10^1 + 2 * 10^2 + 1 * 10^3

And in general suppose a natural number N = a_1a_2....a_n , where a_i is a digit from 0 to 9.

Then we can represent N as follows:
N = a_1...a_n = a_n * 10^0 + a_(n-1) * 10^ 1 ... a_1 * 10^(n-1)

where the "^" means the "power of"

athar89 commented: He knows maths more than i know my girl friend +0
mrnutty 761 Senior Poster

Yea doesn't look like your destructing it properly. Try something like this :

BinarySearchTree::~BinarySearchTree(){
  _destroy(this._root);
}
void BinarySearchTree::_destroy(Node* root){
  if( isNull(root) ) return;
  else{
     _destroy(root.getLeft());
     _destroy(root.getRight());
      delete root;    
  }
}
Unimportant commented: great solution +1
mrnutty 761 Senior Poster

How about just reading it as a string? Then you have an array of numeric character.

mrnutty 761 Senior Poster

Can you settle for this :

template<typename T1, typename T2>
struct Pair : std::pair<T1,T2>{
};
template<typename T1, typename T2>
bool compareSecond(const Pair<T1,T2>& p1, const Pair<T1,T2>& p2){
 return p1.second < p2.second;
}

//use Pair instead of std::pair