mrnutty 761 Senior Poster

structs are there in C++ for backwards compatibility.
Enums are there to help you with constants and a new( but rather old) technique to help in metaprogramming.

But lets go back to the constants thing. Which would you prefer?

struct{
 enum Direction{ EAST, SOUTH, WEST, NORTH }
//...
};

//or
struct{
 const int EAST = 0;
 const int SOUTH = 1;
 const int WEST = 2;
 const int NORTH = 3;
};

IMO, the enum version is better because not only is the enum more descriptive because of the name you can give it( Direction in the above case ), but it keeps count for you.

mrnutty 761 Senior Poster

Try this.

1) Close Visual studio.
2) Right click the visual studio desktop icon
3) Select 'run as administrator'
4) Then try to re-run the program

mrnutty 761 Senior Poster
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
using namespace std;

int main(){
 cout << 
"           ___\n         /`   `'.\n        /   _..---;\n        |  /__..._/  .--.-.\n        |.'  e e | ___\\_|/____\n       (_)'--.o.--|    | |    |\n      .-( `-' = `-|____| |____|\n     /  (         |____   ____|\n     |   (        |_   | |  __|\n     |    '-.--';/'/__ | | (  `|\n     |      '.   \\    )"";--`\\ /\n     \\        ;   |--'    `;.-'\n     |`-.__ ..-'--'`;..--" << endl;

 return 0;
}
Clinton Portis commented: "E" for effort (: +10
jonsca commented: Nice one! +14
mike_2000_17 commented: Nice +14
mrnutty 761 Senior Poster

I see what you want. You want the list to be able to point to a list as well a leaf element. We'll what you
want now is a tree, or more general a graph. Google it up. But buy your definition, a regular linked list is already a match. You can think of each subset of the list as its own list

mrnutty 761 Senior Poster

IT BUILDS :) Thank you. Just what I needed as I was giving up hope for today.

No problem, glad to be able to help. Godspeed, take care

mrnutty 761 Senior Poster

You can use a try/catch block in the initializer list. Check out this site. Here is the live code as an example.

#include <iostream>

struct BadObject{
 BadObject() {}
 BadObject(int i){ throw("Why you no work 0_o?"); }
};

struct GoodObject
{
 BadObject b;
 GoodObject(int i)
   try : b( BadObject(i) )
   {}
   catch(const char *p)
   { cout << "Runtime Error: " << p << endl; }
};

int main(){
 GoodObject obj(0);

 return 0;
}
mrnutty 761 Senior Poster

Try creating simulations.

- Waves simulation
- Orbital simulations
- Pathfinding simulation

mrnutty 761 Senior Poster

Wait... I though He's an Atheist?... Are you effin kidding me?? Hahahaha!

Lol, someone caught the joke. Good eye sir!

mrnutty 761 Senior Poster

check this post

mrnutty 761 Senior Poster

- Rotate the world axis such that the cube has no rotation( i.e 'unrotate' the cube relative to the world axis)
- transform the point to be relative to the cube's position
- check if the relative point is in the un-rotated cube by doing simple bounding min-max check

mrnutty 761 Senior Poster

Its the same as checking for a point in a 2D rectangle. But depending on the view of the person, you need to determine which face of the cube to check on.

mrnutty 761 Senior Poster

First this input :

// inserting elements
    for (int i = 1; i <= 10; i++)
    {
    exampleTree.insert(i);
    }

is the worst case scenario for a binary search tree, it will be equivalent to a linked list. So perhaps you should try some predetermined values so you know what the output should be.

Second, post order does what its supposed to. It prints in reverse order because your binary tree is like this :

[1]
/  \
     [2]
    /  \
         [3]
        /  \
            ...

since there is no left node, it just keeps going right, until it hits [10] then it prints 10, then goes back to [9] and prints 9 and so on.


Third , size, height, and depth are equal because of the tree structure.

Try to do manual inputs.

mrnutty 761 Senior Poster

yupeee:D

How do you think you should approach it then?

mrnutty 761 Senior Poster

thx 4 ur help but I only need 1 number consisted of 4 unique digits , I need to know how to get only unique digits , thx :)

Oh wait so your problem is more like generating a 4-digit number in which each digit in that number is unique?

mrnutty 761 Senior Poster

There is probably a lot of way doing this, but here is one way.

float scale(float n, float min, float max){
 return min + fmod(n,max);
}

EDIT: might not be a good scale, although would be correctly, is not a good distribution. Ignore it.

mrnutty 761 Senior Poster

If its only 4 randomly generated numbers you need, then you can simply call rand() 4 times. It definitely should repeat just after 4 calls. Make sure you are using it correctly.

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

int main(){
 srand( time(0) );
 for(int i = 0; i < 4; ++i){
   cout << rand()%9999+1000 << "\t";
 }
 return 0;
}
mrnutty 761 Senior Poster

Firstly, the picture doesn't show the connectiveness of the nodes, that is it doesn't show if the node are connected or not, so what are the assumptions?

Assuming:

1) intNode->next points to newNode
2) intNode->prev points to null
3) newNode->prev points to intNode
4) newNode->next points to null


Now if the above assumptions are valid then you can imagine "intNode->next" as just newNode.
hence this statement, newNode->next = intNode->next is just intNode = intNode->next which is a memory leak. You can do similar analysis on the other statement

mrnutty 761 Senior Poster

"Give a man a fish, you feed him for a day. Teach a man how to fish and you feed him for lifetime"

My son, I will teach you how to fish

myk45 commented: haha, nice one. +6
mrnutty 761 Senior Poster

hmm good question. Maybe if you split up the arrays into m-segments and possibly run it parallel. Interesting question.

mrnutty 761 Senior Poster

Hint, binary search

mrnutty 761 Senior Poster

You could use a gui library, possibly QT or wxWidgets or create your own

mrnutty 761 Senior Poster

>>, BSP trees are still probably the easiest efficient solution;

Actually, for simple application, BSP isn't even necessary, simple collision check can suffice

mrnutty 761 Senior Poster

What is the problem? If you can't use std::vector, then why not use std::list? Why are you using memcpy? Just use operator= for more clarity.

mrnutty 761 Senior Poster

>>Even on an embedded system, that is hardly a memory hog.

How about calculating 100! or 1000!, putting aside the limitation of data-type of course. Anyways, usually tail recursion can be removed by compiler's optimization.

OP have you learned about functions? Try to modularize your code by using functions.

//given a positive natural number n, it returns the factorial
//of n, that is it returns n*(n-1)*...*(2)*(1)
unsigned int factorial(int n){
 unsigned int fact = n;
 while(n --> 1){ fact *=n; }
 return fact;
}
int main(){
 unsigned int input = 0;
 cin >> input ;
 cout << factorial( input ) << endl;
}

Also don't forget to comment on your code. And try to indent your code for better visualization. Other than that, good job!

mrnutty 761 Senior Poster

Means? Absolute value refers to be value having no dependence on its sign right? So what is an absolute value sign?

As in the mathematical sense of absolute value. In mathematics one would use the Bar symbol as in , |x|, using the unary + symbol, as in +x, kind of amused me a little.

mrnutty 761 Senior Poster

I kind of like the idea of unary plus being an absolute value sign. Right now, the unary plus is nothing more than extra information to the user. Maybe to hint to the user that it should always be a plus or something. It has no effect.

mrnutty 761 Senior Poster

Dont read it line by line. Instead, read it word by word. The check if each word is a valid real number. Example:

int main(){
 ifstream fileIn("input.txt");
 string word;
 unsigned int count = 0;
 while( fileIn >> word )
 {
    if( isRealNumber( word ) ){  //you need to implement isRealNumber
        ++count;
    }
 }
}

Note in the above I assume each words are separated by spaces

mrnutty 761 Senior Poster

It is sad to say that Christopher Hitchens died yesterday, failing the battle against cancer. He was a great author and even better person. It is because of him, I learned the many arguments against religion. He will be missed. My sincere condolence goes out to him. May he rest in peace in heaven.

mrnutty 761 Senior Poster

Use std::strings

int main(){
 string label,topcode,toperand;
 ifstream fileIn("input.txt");
 while( fileIn >> label >> topcode >> toperand){
   cout << "Label = " << label << endl;
   cout << "TopCode = "  << topcode << endl;
   cout << "Toperand = " << toperand << endl;
 }
}
mrnutty 761 Senior Poster

You should use the getline function, which reads the first line from input. Then you can use the first character to determine the proper equation to use. Here is an example.

string line;
 //while there is a line to get
while( getline( fileIn, line) ){
 switch( line[0] ){ //check the first letter of the line
   case 'M' : useEquationM(line); break;
   case 'N' : useEquationN(line); break;
   default: throwError("Invalid options!"); break;
 }
}
mrnutty 761 Senior Poster

Your logic seems false.

Line 3: You create a node t
Line 16: You search for the int value then then updated t's frequency? But t is a new node not necessarily inside the binary tree!

What you want is a helper function that returns the Node who has the value x. Then increment that node's frequency.

tree_node * _find(const tree_node& root,const int val){
 if(!tree_node) return NULL;
 else if(val > root.value()) _find( root.rightNode(),val);
 else if(val < root.value()) _find( root.leftNode(),val);
 else{ return root; /* found */ }
}

void insert(int val){
  tree_node *n = _find(val);
  if(n != NULL){
    /* added previously, so just updated count */
       n->incrementCount();
  }else{ 
      /* not found */
      _insertHelper( val ); //just inserts a new node with val into the tree
  }
}
mrnutty 761 Senior Poster

so.....whats the question 0_o?

mrnutty 761 Senior Poster

Seriously how hard is it to perform a google search?

Just pick one of the top ones. Sorry for sounding rude, but sometimes independency is what should be utilized to increase productivity

mrnutty 761 Senior Poster

If you want to redirect the output from terminal you use the '>' operator to a file. As in ./app < inputFile > outputFile

mrnutty 761 Senior Poster

whats your removeStores function looks like

mrnutty 761 Senior Poster

If your on linux you can use the '<' operator. As in ./app < fileName it gets the input from the file instead of direct terminal.

mrnutty 761 Senior Poster

You can think of static variable as global variables but with scope

mrnutty 761 Senior Poster

That was my original thought too, but I dismissed it because it would be horribly sloooow.

But adding leading 0's infront assumes you know the maximum number of resource

mrnutty 761 Senior Poster

You adding it twice here :

for (int day = 0; day < 7; day += 1)
for (int temps = 0; temps < 2; temps += 1)
tot += tempav[day][0]; 
{
cout << " Total Col 1 " << tot << endl;

}

just do this:

for (int day = 0; day < 7; day += 1){
 tot += tempav[day][0]; 
}
mrnutty 761 Senior Poster

Another option is to supply your comparison method to std::sort:

int getDigitFromLine(const std::string& line){
  const string digits = "0123456789";
  size_t beg = line.find_first_of(digits);
  size_t end = line.find_first_of(digits);
  string digitString = line.substr(beg, end-beg + 1);
  return atoi( digitString.c_str() ); 
}
bool mycomp(const std::string& lhs, const std::string& rhs){
  int lhsNumber = getDigitFromLine(lhs);
  int rhsNumber = getDigitFromLine(rhs);
 return lhsNumber < rhsNumber;
}
int main(){
 //populate vector
 std::sort( vec.begin(), vec.end(), mycomp);
}
mrnutty 761 Senior Poster

How about you make a helper function to reduce work.

class Logger{
private:
 ostream *stream;
public:
 Logger(ostream& strm) { stream = &strm; }
 void log(const std::string& msg){  _log(msg); }
private:
 void _log(const std::string& msg){ (*stream) << msg << endl; }
};

And so now you just call _log(msg) instead.

mrnutty 761 Senior Poster

When you delete a node from a graph, to update your adjacency matrix you can either remove the column and row associated for that row. For example

Let node = 1 2 3
And its initial adjacency matrix be:

  1 2 3
1 0 0 0
2 0 0 0
3 0 0 0

if you delete node 2, your adjacency matrix should remove all reference for node 2. Hence afterwards it should look like

1  3
1 0  0
3 0  0

Or you can take another approach, a flag approach, for example have -1 to mean deleted

1  2 3
1  0 -1 0
2 -1 -1 -1
3  0 -1 0
mrnutty 761 Senior Poster

std::cin.getline(const_cast<char *>(name.data()), MAX_NAME_SIZE); is undefined behavior because you are not allowed to modified the pointer-to-const-char returned by data.

You could easily just use the c-style approach and convert that into a string. Or you can do something like so.

string str(' ',MAX_SIZE);
cin.getline( &(str[0]) , str.size() );
mrnutty 761 Senior Poster

The only part you seem to need help on is how to properly "increase in the potential for them to receive a high five from others ". The other problem is fairly trivial.

To increase a user's potential of receiving a high-five you first have to note that to increase the probability of a user you need to uniformly decrease the probability of other users and add that to the current user. Also when picking the person to high-five you need to take into account the probability of picking each person. The easiest way to do this is to use some sort of range concept. For example
user one has a range from [1-50] and user two has range from [51,100], so you get the maximum range which is from user two with value of 100, then you get the minimum range from the users, which user one has with a value of 1, then you generate a random number from min to max. And check which user's range it falls under. Furthermore, you then increase the range for whichever user gave the high-five. For example, if user one gave high five to user two, then you could change the range of user on from [1-50] to [1-51] and then adjust the range of user two to [52-100]. Remember these range represents the probability so you need to make sure that add up correctly.

mrnutty 761 Senior Poster

Test this :

#include <iostream>
using namespace std;

int main(int argc, char * argv[]){
  for(int i = 0; i < argc; ++i){
    cout << "Argument[" << i < "] = " << argv[i] << endl;;
 }
}

You can call it like ./ExecutableName arg1 arg2 arg3

mrnutty 761 Senior Poster

Couple of options.

1) Read it into a string and work from there
2) Split the digits and work from there
- to split the digits you can use the divide and modulus operator

And I am guessing your avoiding arrays because either your assignment says you cannot use them or the more probable reason is that you don't know how to use them well. If so then you need to seriously reconsider learning them and redoing this problem using array. BTW if you use method 1 above then internally your still using an array.

mrnutty 761 Senior Poster

Yup tolower return the 'lower' version of the argument passed if valid. IT does not change the argument to its lower-cased version

mrnutty 761 Senior Poster

And that, of course, is how std::max is coded.

Looking at the source code, its has a extra check to make sure its less than comparable and has multiple overloads

mrnutty 761 Senior Poster

Suppose I have a simple program to find the largest of two numbers of different datatypes and return the largest.I'm implementing it as a template to support generic datatypes

template<class T,class U>
T max(T t,U u)
return (t>u?t:u);

int main()
{
  int x=70;
  float y=100.5;
  max(x,y);
  return 0
}

Here the second generic datatype is the largest , but since return type is given as 1st type T(integer in this case ) the result gets truncated.Is there any other way to make the system identify and pass the return type also ? So that return type can also change dynamically depending on input. Please help me

It doesn't really make sense to compare two different types in general because two types might not be comparable to each other. Make it as suggested.

template<typename T>
T max(const T& lhs, const T& rhs){
   return lhs < rhs ? rhs : lhs;
}
mrnutty 761 Senior Poster

>>Whitespace is largely insignificant in C++, your suggestion won't change anything.

Yea I guess your right, I thought the compiler might have seen omp_get_max_threads as a function instead of a function call because the next character afterwards is a whitespace instead of a '('. It was just a wild guess.