mrnutty 761 Senior Poster

darn double post. Well this post is useless. Maybe there will be something here that you
would find useful. But I doubt that anything in this post will be useful to you, whoever you
are. Well, at least I made this post worth something, in my opinion of course. So anyone here doing
anything interesting projects or whatever? I tried to make the most of this post. Well thank you for
reading and wasting a few minutes or seconds of your life. Goodbye.

mrnutty 761 Senior Poster

Why would you want to do this is beyond my feeble understanding. But is this the output
you were thinking of?

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

template<typename R, typename T>
R castTo(const T& arg){
	stringstream ss;
	ss << arg;
	R val = R();
	ss >> val;
	return val;
}
class MySequence{
	string curr;
	int cntr;
	int incrementValue;
public:
	MySequence() : curr(), cntr(0), incrementValue(1){}
	explicit MySequence(int incr) : curr(), cntr(0), incrementValue(incr){}
	string nextVal(){		
		if(curr.length() < 5) 
			curr.push_back('0');
		else{
			cntr += incrementValue;
			string val = castTo<string>(cntr);
			int end = curr.size() - val.size();
			//protect against overflow
			if(end < 0) {
				curr = "0" + curr;
				end = curr.size() - val.size();
			}
			string newSeq = curr.substr(0,end) + val;
			curr = newSeq;
		}
		return curr;
	}
};
int main(){
	MySequence s;
	for(int i = 0; i < 100; ++i){
		cout << s.nextVal() << endl;
	}
}
mrnutty 761 Senior Poster

> Is there any way of checking all elements in a queue without calling pop().

Yes, if you have used a std::queue<T> with the default std::deque<> for the second template parameter.

template< typename T > void foobar( const std::queue<T>& queue )
{
    typename std::deque<T>::const_pointer begin = &queue.front() ;
    typename std::deque<T>::const_pointer end = begin + queue.size() ;
    // [begin,end) iterates over elements of the queue. for example,
    std::copy( begin, end, std::ostream_iterator<T>( std::cout, "\n" ) ) ;
}

Is that a good idea?

@OP: Do you think you need queue for your problem? Will a list do? How about a vector?
Think about the data structure you need.

mrnutty 761 Senior Poster

I can do this in a couple of minutes for a low rate of $20.00 US dollars.

mrnutty 761 Senior Poster

Memory in C++ fall into 3 category :

static memory : Memory is occupied until the end of the program
heap memory: Memory is occupied until freed
Stack memory : Memory is occupied until the end of the block, in which it was declared.

Thats a very simple description. If you want more details, then maybe you can give us
more detailed question.

mrnutty 761 Senior Poster

What do you call the set of languages that support classes, inheritance, and runtime polymorphism? Unless you're planning on sperging out some "C++ is multiparadigm" crap, or unless you're a dynamic language weenie, there's no way you can say C++ is not an OO language with a straight face. You haven't bothered explaining yourself or being helpful, and your post is designed entirely to flaunt your sense of self-importance, so back your statement up or GTFO.

I am not sure why you are always angry and aggravated? But you need to stop taking it out on the users here. Go take a vacation or something.

Anyways, C++ has OOP but is not necessarily and object oriented language. Its not "sperging
out some "C++ is multiparadigm" crap. It is what it is. C++ supports both OOP and a C-style procedural programming. Even the creater of C++ says that C++ is a multiparadigm language

For C++ to be a complete OOP language, it would have such things as garbage collection.
Its not pure OOP language because not everything is an object. One could write a C++
program without even using one object. Even main is not a function of an object.

I would like to hear your argument of why C++ is a Object Oriented Language if you don't mind.

mrnutty 761 Senior Poster

C is procedure oriented language..

C++ is a object oriented language

I beg to differ, C++ is not an OO language.

mrnutty 761 Senior Poster

Instead of creating 2 function, maximum and second maximum, just create 1 function
called sortArray. Then you can just get the maximum at element 0 or the last element,
depending if you sort it descending or ascending. You can get the
second,third,fourth... or whatever largest easy as well.

mrnutty 761 Senior Poster

First off thats not polymorphism. Second you need a forward decleration.

struct B;

struct A{
 A a;
 B b;
};

struct B{
 A a;
 B b;
};

Second, why are you using pointers? In C++ you use reference.

mrnutty 761 Senior Poster

You need to use wide char. Try this :

wchar_t *msg = L"Hello world";
wchar_t *title = L"Greeting";
MessageBox(hWnd,msg,title,MB_OK);
mrnutty 761 Senior Poster

Also remember that cosine and sinus function takes in radians and not degrees. So this

circle.x = r*cos(i) - h;
circle.y = r*sin(i) + k;

should be this :

float rad = 3.14f/180.0f * i;
circle.x = r*cos(rad) - h;
circle.y = r*sin(rad) + k;
mrnutty 761 Senior Poster

Or, you know, just keep coming back to the thread and giving the first post a -1 vote. Whatever.

Don't sweat it. Some people just don't appreciate good writing. Usually, these are immature
little kids, that hasn't gone through puberty yet, who still thinks the word 'boobies' is funny.

mrnutty 761 Senior Poster

Make your getName function a const corrected function like so :

string getName()const; //the const says that it will not change any member variable, presumably

//.cpp
string getName()const{
 return name;
}
mrnutty 761 Senior Poster

Does SuperClass have to be abstract?

mrnutty 761 Senior Poster

>>i want it to end after a character is pressed.

Use this :

vector<int> vec;
int n = 0;
//while user inputs a number, save it
while(cin >> n){ vec.push_back(n); }
 //now reset the stream
 cin.clear();
 cin.ignore(numeric_limits<streamsize>::max(),'\n'); 

 //now carry on with your code
mrnutty 761 Senior Poster

>>I was wondering if it's possible to add for example the struct below into a binary tree:

Yes it is. All you have to do is create a relational operator that compares hours as
primary and minutes as secondary. Come to think of it. A priority_queue is the
correct data structure for this problem. You can use the time as the priority_queue.
Here is an example.

#include <iostream>
#include <queue>

using namespace std;

struct Action{
	int hours;
	int minutes;
	Action(): hours(),minutes(){}
	Action(int h, int m) : hours(h), minutes(m) {}
	virtual void perform()const{
		/* perform some action */
	}
};

bool operator <(const Action& lhs, const Action& rhs){
	if( lhs.hours < rhs.hours ) return false;
	else if(lhs.hours > rhs.hours) return true;
	else return lhs.minutes < rhs.minutes;
}
void print(const Action& a){
	cout << a.hours << ":" << a.minutes << endl;
}
int main(){
	std::priority_queue<Action> actions;
	actions.push( Action(1,15) );
	actions.push( Action(0,10) );
	actions.push( Action(0,15) );
	actions.push( Action(1,20) );

	while(!actions.empty()){
		print(actions.top() );
		actions.pop();
	}
}
mrnutty 761 Senior Poster

>>Yes gcd CAN be zero if say numerator and denominator are 1.

gcd(1/1) = 1;

Why would anyone start counting from 0 and up, instead of 1 and up? Maybe its getting
too late. I'm going to sleep.

mrnutty 761 Senior Poster

I don't see why gcd would ever be 0. Its minimum should be 1, because 1 can divide into
all natural numbers evenly. Also look at the for loop condition, gcd can never reach 0.

mrnutty 761 Senior Poster

You need to pass the array as you suggested. Its the same as passing an array of ints, or whatever, its just a different type. For example :

int search(Inventory* items, int size){
 /*code to search */
}
void print(Iventory* items, int size){
  /* code to print */
}

So essentially, you need to pass in the inventory.


As for your bonus question, you can use std::vector<Inventory> items;
Google on how to use std::vector.

rObOtcOmpute commented: Nice +1
mrnutty 761 Senior Poster

.

mrnutty 761 Senior Poster

What exactly are you trying to solve? Why do you need its hex values? Are you hashing?

mrnutty 761 Senior Poster

That code is kind of confusing if you do not know the meaning of i/j.

Take i = 10 for example, and we want to see if 10 is a prime number. To do this, what
your code does is run to all numbers from j = 2 to j < i/j. If it successfuly runs through the whole loop then its a prime, if not then your "confusion2" checks if the loop ended prematurely.

Personally, that code is looks confusing to beginners, and I would suggest something like this to beginners. That is after they have learned about function.

#include <iostream>
using namespace std;


//returns true, if the 'lhs' is evenly divisible by 'rhs'
bool isEvenlyDivisible(int lhs, int rhs){
	return lhs % rhs == 0;
}

/* A number is a prime if its evenly divisionable by
 1 and its self only. Ex, 2 is a prime because 2 divides into 1 evenly 
 and 2 divideds to 2 evenly. Anything else divided into 2 has a remainder */

//function returns true if 'num' is a prime otherwise returns false
bool isPrime(int num){
	//if num < 2 then its by definition not a prime
	if(num < 2) return false;

	//initial condition, assume num is prime
	bool isAPrime = true;

	//runs through i = 2 to num, to check if any number evenly divides into 'num'
	//notice how checkFacotor is never equal to 1, or num. So if its divisible by any
	//number in between, then 'num' …
mrnutty 761 Senior Poster

ok so what is the solution to this problem? As I understand it, the point of using a function pointer is that I can replace (*pt2Func) with a string pointer - the name of my function?

No. You need to pass it a function with the same prototype as your function pointer.

mrnutty 761 Senior Poster

:cool: hi Friends,.,. !!
i want to know a perfect time consumption for a given particular program..
IN MILLISECONDs

I know the way of using following method:

start=clock();

/*...
  ...     my programming logic . . .
  ...   */

end=clock();

difference=(end-start);

But it gives time in seconds , which is not useful for me in some programs,..

Q.1
Can anyone help me to measure time in MILISECONDS ?:idea:

Q.2
Is there any TOOL which can give me perfect time consumptions ??

Please help me friends . . have a great day !

clock() gives time in milliseconds, not seconds. So you already got what you
need right there.

mrnutty 761 Senior Poster

>>cannot convert parameter 2 from 'char *' to 'char (__cdecl *)(char)'

You are trying to convert a char* to a function pointer.

mrnutty 761 Senior Poster

This compiles in visual studio 2008 express edition:

#include <iostream>
using namespace std;
int main(){	
	string name = "lol";	
}
mrnutty 761 Senior Poster

Of course we're not gonna do this, unless you pay me $20.00 dollars for this problem.

But to start you off, this is the quadratic equation :

x = -b/(2a) +-  sqrt(b^2 - 4ac) / (2a).

Since they gave you a,b, and c. You just have to plug it in though.

A word of caution this part of the equation b^2 - 4ac is called the discriminant. Make sure this is positive else the solution is imaginary.

mrnutty 761 Senior Poster

>>aDir.erase(found);

Look at string.erase function prototype. By that command, you are erasing all elements starting at the position found. What you need is this aDir.erase(found,1);

mrnutty 761 Senior Poster

There is no way in C++, but you can do something like this :

void printN(char c, int n){
	while(n--) cout << c;
}
int main(){
 int a = 1, b = 2;
 const int ADJUSTMENT = 30;
 printN(' ',ADJUSTMENT);
 cout<<a<<" "<<"+ x = "<<b<<endl;
}
mrnutty 761 Senior Poster

Is there a certain reason why you are doing this? What are you trying to solve?

mrnutty 761 Senior Poster

Make it more concise :

void InitializeBoard(char board[][3]){
	board[0][0] = '1';
	board[0][1] = '2';
	board[0][2] = '3';
	board[1][0] = '4';
	board[1][1] = '5';
	board[1][2] = '6';
	board[2][0] = '7';
	board[2][1] = '8';
	board[2][2] = '9';
}

is the same as

for(int r = 0;r != 3; ++r){
 for(int c = 0; c != 3; ++c){
    board[r][c] = '1' + 3*r+c;
 }
}

This code :

void TakeInput(char board[][3], char marker)
{
	char number;
	cout << "Please enter what number to want: ";
	cin >> number;
	for (int row = 0; row < 3; row++)
		for (int col = 0; col < 3; col++)
			if (board[row][col] == number)
				board[row][col] = marker;
}

is the same as :

int n = 0;
cin >> n;
int r = 0, c = 0;
if(n < 0 || n > 9) { /* error, invalid input */ }
r = n/4;
if(r == 0) c = n - 1;
else if(r == 1) c = n - 4;
else c = n - 7;
if(board[r][c] == 'X' || board[r][c] == 'O'){ /* error, invalid input ask again*/}

This code

char ChangeMarker(char marker)
{
	if (marker == 'X')
		return 'O';
	else
		return 'X';
}

I like this better :

return marker == 'X'? 'O' : 'X';

Your checkWin could use some renovation as well.

mrnutty 761 Senior Poster

Here is the prototype :

friend istream& operator >>(istream& stream, Data_Time& date){
 /* code to read here */
 /* return ... */
};

Make that your member function.

mrnutty 761 Senior Poster

This sounds like an h.w or an exercise, so doing this :

std::sort( name, name + SIZE );
int pos = std::binary_search(name, name +SIZE, "JOSH");

would not be beneficial.

And as pointer out, the string class overloads the relational operators, so that means all you have to do is change the type from int to string.

mrnutty 761 Senior Poster

Ughh. No thanks. I had enough discrete mathematics class at my school for my lifetime.
Anyways, looks good. Keep it up. Might read it fully, when I got the time.

mrnutty 761 Senior Poster

cant help u with this. I'm still at c program. sorry bro

Then why bother posting?

@OP, you have more than 1 issues with your code. But I'll let someone handle out the details. But to answer your question, you if statement should be :

//read as : "if choice is equal to 'D' or if choice is equal to 'd' or if ...
if ( choice == 'D' || choice == 'd' || choice ==  'R' || choice == 'r' ) {
       /* Success */
}	
else { /* face palm */ };
mrnutty 761 Senior Poster

It works differently for char. It does not print the address, but it prints all
characters that the pointer is pointing to, until it reaches a null character.

mrnutty 761 Senior Poster

Alternatively :

const int ROW_SIZE = 10;
const int COL_SIZE = 10;

int array2d[ROW_SIZE][COL_SIZE] = {0};
mrnutty 761 Senior Poster

That's cheap. I use my regular consultancy rates for lazy student homework: $250/hour with a minimum of 8 hours.

I guess thats why your rich and I'm poor.

mrnutty 761 Senior Poster
char ch = 0;
//read file character by character
while(file && file.get(ch) ){
 cout << ch;
}
mrnutty 761 Senior Poster

That will be $100 per homework please.

mrnutty 761 Senior Poster

>>OK well, to me that's a matter of opinion.. frankly I have never understood the purpose of switch statements in an object-oriented software design, and I (almost) never use them. So, I never liked them, never got used to them, and never found them easier to read or cleaner, I just find them useless and cumbersome. But you're right, they are faster, for contiguous integer case-values (or enums, of course), but not for strings (like in the OP) and most other types.<<

I guess I find this easier :

int state = event.state();
switch(state){
 PLAY_GAME : game.play(); break;
 PAUSE_GAME: even.pause(game); break;
 EXIT_GAME: event.cleanup(game); break;
}

that this :

int state = event.state();
if(state == PLAY_GAME){
  game.play();
}
else if(state == PAUSE_GAME){
  event.pause(game);
}
else if(state == EXIT_GAME){
 event.cleanup(game); break;
}

>>I like to preach awareness

Ok, I'm fine with that. But maybe mentioning that using new and delete is error prone,
especially to beginners, and thus suggesting to use smart pointers, would be a even
better post( IMO ), and not to mention a good advice.

>>Crawl -> Walk -> Run

Well, you should how to crawl, then walk, so why omit Run?

Anyways, I'm not trying to start a debate to take over OP's thread. So lets stop this.
If you want, you can send me a message.

mrnutty 761 Senior Poster

@OP: First work to get a O(n) solution to the maximum sum problem for a 1d array.
And after you get that, use that to your advantage for this problem. At best, you will
do O(n^3), if I'm not mistaken.

mrnutty 761 Senior Poster

>>it[switch statements] doesn't make the slightest difference at the end

On the contrary it does. Switch statements are easier to read, cleaner, and faster
than ifs/else's.


>>But, to be even more on the C++ side, you can use function objects

And to be even more more towards C++ side, you can ditch the new and delete command
for now, and let smart pointers handle it.

mrnutty 761 Senior Poster

Anyone of these will do for me : Metis, Adrastea, Amalthea, Thebe ...

There are 59 more that I can name. But thats good enough.

mrnutty 761 Senior Poster

@OP: You should do something like this :

int main(){
 Game game;

 while(game.isNotOver()){
  int state = game.getState();
   switch(state){
     case PAUSE_GAME : pauseGame(game); break;
     case PLAY_GAME : playGame(game); break;
     case EXIT_GAME : cleanUp(game); break;
   }
   if(!game.isNotOver){ //if game is over
      bool playAgain =  checkIfUserWantsToPlayAgain();
      if(playAgain) 
         game = Game(); //re-initialize everything
   }
 }
}
jonsca commented: I like it. +4
mrnutty 761 Senior Poster

What exactly are you trying to? You can make a template specialization if you want.

mrnutty 761 Senior Poster

Yay, I got it too work, and no my function is exactly this. What I am doing is creating two functions for creating and coloring a triangle. ColorTriangle takes the array rgb and 3 numbers represting r, g and b values then CreateTriangle takes the rgb which has been modified in ColorTriangle, but also an x, y and z coordinate. Then it creates the Triangle with those values.
Thanks for your help!

Why wouldn't you use structs for this?

struct Color{
 float red, blue, green;
 Color(float r = 0, float g = 0, float b = 0)
   : red(r), green(g), blue(b) {}
};

struct Triangle{
 float vertices[3];
 Color color;
 Triangle() : vertices() {}
 Triangle(const Color& c) : color(c), vertices(){}
};

Triangle redTriangle(){
 Color red = Color(1.0f,0.0f,0.0f);
 Triangle redTriangle(red);
 return redTriangle;
}
mrnutty 761 Senior Poster

First : Don't use pointers, as in don't do this map<T1, T2>* myTempMap.
In C++ you would use reference : map<T1, T2>& myTempMap . That way
you avoid doing stuff like (*myTempMap).blah().

Second your problem is comes from line 9, specifically this code :

(*myTempMap).insert(ins_pair(10,"NewValueInserted"));

The reason you get that compiler error is because you are inserting a string, as the second argument, where as it expects a list<string>. It doesn't matter if
bIsDigit is true or false. In template programming, the compiler
checks all code in the instantiation to make sure everything works fine. Thats
why you get that error. You can't convert string to list<string>.

mrnutty 761 Senior Poster

Ok, first how much of C++ concepts have you learned? In fact, to be more general how much of programming concepts have you learned? Any data structure training? Know
what a pure virtual base class destructor is ? Do you know when is the right
time to use the friend keyword? Do you know anything about how memory works? Or what
type of memory is used in C++? Do you know all the keyword in C++?

Well if your confident enough, then a really good way to get better is to program more. I was in your position a year and 1/2 ago. Finished my programming class. And
I was left with semi knowledge about programming, but because I Love programming, and
I have a passion for it, I went to become a better programmer by doing practice problems, learning the intricate details of C++, and went to program 2d/3d games.

So I recommend to you that you start out doing a lot of practice problems, a lot.
Then after you become comfortable with your language, then go on to incorporate graphics into your projects.

mrnutty 761 Senior Poster

A couple result from googling,
1)uno
2)duos.