mrnutty 761 Senior Poster

Just interpolate between the starting height and the ending height. And you will get a smooth function. Afterwards, apply verlet integration for smooth transitioning. In short, just animate it crouching. But realize that crouching shouldn't take much cpu. So another suggestion is to have something like this :

void display(){
 //...
 if( KEY_DOWN_PRESSED && canCrouch){
    int steps  = 2;
    const int END_X_POS = 5;
    for(int i = 0; i < steps; ++i){
        crouch( END_X_POS/float(steps) );
        redisplayPlayer();
     }
 }
}
void crouch(const int delta){
 entity[0].crouching = true;
entity[0].centre_height = PLAYER_HEIGHT / delta;
entity[0].height = PLAYER_HEIGHT / (delta/2);
entity[0].gun_position.x = 0.0;
entity[0].gun_position.y = entity[0].height - 1.0;
entity[0].gun_position.z = 0.0;
}

so the idea in above is to have multiple crouch in between starting(0) and ending(5) and display its position. This is cheaper than original suggestion and probably shows decent result.

mrnutty 761 Senior Poster

Note that usually you would initialize variables in the constructors like so :

class Point{
private:
 int x_, y_;
public:
 Point(int x , int y) 
  : x_(x) , y_(y) //called an initializer list
  {
  }

};

There is something you have to remember about the above though. It will initialize data in order
of their declaration not in the order of your initializer list. Just read this sentence a couple of times a day and it will sink in.

mrnutty 761 Senior Poster

You need to show more code.

mrnutty 761 Senior Poster

use the inline keyword. Note that the compiler can choose not to respect your request.

mrnutty 761 Senior Poster

Software reasons:
- It reduces code readability.
- It produces inefficient code.
- It makes code maintenance harder
- ...probably many more.

Hardware reasons:
- It is inefficient.
- Branch statement are one of the most inefficient operations, and using many of those makes the hardware work more, when it might not be necessary to use them.

Ethical reasons:
- Have some heart. It is evil to use it
- You might not 'goto' heaven, if you use it.
Miscellaneous reasons:
- A dinosaur will pop out of you computer and eat you if you do.
- Your computer will kill it self
- I will kill you
- You will be condem to goto 'bad programmer's' hell.
- A dinosaur will pop out of your tv and eat you.

mrnutty 761 Senior Poster

>>Because NULL is defined as ((void*)0), and that cast may be a problem in some cases

care to explain...? I never had a problem switching the two.

mrnutty 761 Senior Poster

not quite, trace through your program. Line 1 is key. The fact that you are getting sef faults means that the pointer p is not valid. Check to make sure that line 40 does executes

mrnutty 761 Senior Poster

Remember that Pointer-to-objects do not get initialized to null implicitly, you have to do that by your self

mrnutty 761 Senior Poster

I think your printing loop is incorrect, try doing this :

list<nonZero>::iterator j;
	list< list<nonZero> >::iterator i;
	for(i = rows.begin(); i != rows.end(); i++)
	{
		for(j = i->begin();j != i->end(); j++)
		{
			cout<<(*j).getColumn()<<" "<<(*j).getValue()<<" ";
		}
		cout<<endl;
	}
comSysStudent commented: Helped me out a lot +2
mrnutty 761 Senior Poster

whats the problem with having an extra endl?

mrnutty 761 Senior Poster

You can create a struct whose sole purpose if to share const global variables, for example :

struct EnvironmentConstants{
 static const int TIME_STEP = 1; //in sec
 static const int SCREEN_WIDTH = 100; //in pixels
 //.. and so on
};

and call it like so :

void update(){
  foreach( Entity e : EntityList){
     update(*this,EnvironmentConstants::TIME_STEP);
  }
}
mrnutty 761 Senior Poster

For you example #2, isn't that undefined? You are( or will be) referencing a variable that is out of scope.

mrnutty 761 Senior Poster

Another approach is to create your own namespace :

#include <iostream>
using namespace std;

namespace Util{
 template<typename T>
 T& max(const T& lhs, const T& rhs){ return lhs < rhs ? rhs : lhs ; }
};
int main(){
 cout << Util::max(1,2) << endl;
}
gerard4143 commented: Like your simple solution +5
mrnutty 761 Senior Poster

Actually there is little to no reason to make operators virtual, with mild exception to operator=;

As for the syntax you can do this :

struct F{
 virtual F& operator+(const F& f)const = 0;
 //.. and so on
};
mrnutty 761 Senior Poster

You avoid using it much as possible, instead substitute reference for it. But you might eventually need to use it when you need polymorphism, although you can still use reference, in most cases, pointers might be more practical.

mrnutty 761 Senior Poster

You need to deference you pointers

mrnutty 761 Senior Poster

Surprised none mentioned the use of const.

float computeAverage(const float data[], const int DATA_SIZE){
   return std::accumulate(data, data + DATA_SIZE, 0)/ float(DATA_SIZE);
}
mrnutty 761 Senior Poster

Line 49 is wrong for sure, it probably should be:

total += data[i*width + j]; //or maybe "data[j*height + i]"

At line 62, the ImageData array was never initialized, so it won't contain the actual pixel values, you should probably use data as above.

For the rest, I think you have it correct, both the mean and stddev.

I think it should be, data[height * i + j]

mrnutty 761 Senior Poster
if(y) //verify that y is not NULL before deleting it.
       delete y; //this should be safe.

just note, deleting NULL is defined and does nothing.

mrnutty 761 Senior Poster

It doesn't work because for an element n to be less than m, both x and y fields of n has to be lower than of m's( by your construct). You should test x first then y. Like so :

if(p1.x < p2.x) return true;
 else return p1.y < p2.y
mrnutty 761 Senior Poster

You have to assign a static const variable in order to be accepted as an array size.Try using a template function and pass it a constant integral when calling it.

template <int size> int* func ()  { //example
   int array[size];
   return array;
 }

Don't do that. That code is undefined since you are returning a pointer to a variable that
will be destroyed at the end of its scope.


@OP you would want to use either a container or use dynamic memory allocation, for the variable modeCounter. In fact, just changed this line int modeCounter[S]; to this std::vector<int> modeCounter(S,0) and it should all work fine.

mrnutty 761 Senior Poster

You never intialized sec. Anyways, you should be doing something like this :

class Vector2F{
private:
 float pos[2];
public:
 Vector2F() : pos(){}
 Vector2F(float x, float y) { pos[0] = x; pos[1] = y; }
 float getX(){ return pos[0]; }
 float getY(){ return pox[1]; }
 void setX(float x){ pos[0] = x; }
 void setY(float y){ pos[1] = y; }
 void translate(float dx, float dy){ pos[0] += dx; pos[1] += dy;}
};

class Circle{
private:
 Vector2F position;
 Vector2F velocity;
public:
 explicit Circle(float x = 0, float y = 0) : position(x,y){}; 
 void draw(BITMAP *screen){
   //default color is black, you can encapsulate color if you want also,
   circlefill(screen, position.getX(), position.getY(), makecol(0,0,0) );
 }
 void move(float dx, float dy ){
   position.translate(dx,dy);
   draw();
 }
}circle;

void update(Circle& circle, float dt){
 //use some sort of numerical update method
}
void display(){
 //..stuff
 circle.draw( screen );
 //calculate dt, i.e time difference
 const int UPDATE_INTERVAL = 100; //milliseconds
 if( timer.getElapsed() > UPDATE_INTERVAL){   
   update(circle, 1.0f ); //
 }
}
mrnutty 761 Senior Poster

So why aren't you using std::vector<DNA> again ?

mrnutty 761 Senior Poster
averaged[i];
if(i>highest)

You probably want

if( averaged[i] > averaged[highest] ){ highest = i; }
mrnutty 761 Senior Poster

Since they are raw pointers you would have to deallocate each pointer manually. If you want to use something like m_vPopulation2 = m_vParentPop2 then you should instead use smart pointers instead of raw pointers.

If both of the vectors are the same size then you can do this :

for(int i = 0; m_vParentPop2.size(); ++i){
  DNA* elem = m_vPopulation2[i];
  m_vPopulation2[i] = new DNA(*(m_vParentPop2[i]); //make a copy of DNA
  delete elem; //delete the old element
}
mrnutty 761 Senior Poster

The first thing I see is this :

int main()
{
	//called functions
	void userData(string studentName, int numOfClasses, char letterGrade);
	double calData(int totalPoints, int numOfClasses);
	void displayData(string studentName,char letterGrade, double gpa);
	getch();
	return 0;
}

which is wrong and should be this :

int main()
{
	//called functions
	userData(studentName, numOfClasses, letterGrade);
	calData(totalPoints, numOfClasses);
	displayData(studentName,letterGrade, gpa);
	getch();
	return 0;
}

There are definitely other problems but that should get you started.

mrnutty 761 Senior Poster

Or you can get the input as a string and just access the first digit from there

mrnutty 761 Senior Poster

1 is consistent throughout the entire program in all the arrays would that still affect it?

If you ever do monthAverage[0], monthSum[0], or monthInput[0] it will be garbage!
But your problem is with your for loop condition. In all of your for loop you use i <= someVariable. The problematic point is the <=. Using that, your for loop would loop through array[1] to array[constSize], while in C++, the valid range for arrays is
array[0] to array[constSize-1].

mrnutty 761 Senior Poster

Dude just use if( x % 100 == 0){ x= 1; } else ++x;

mrnutty 761 Senior Poster

Well if you want to stick through it and fight it, then we'll gladly try to assist. Just post your problem statement.

mrnutty 761 Senior Poster

Are you really trying to use meta-programming to create an array of site t?

mrnutty 761 Senior Poster

use can use string.erase with isspace

mrnutty 761 Senior Poster

Just an example :

#include <iostream>
#include <vector>

using namespace std;

struct Person{
 int age_;
 float gpa_;
 Person(int age = 0, float gpa = 0.0f) : age_(age), gpa_(gpa) {}
};

ostream& operator << (ostream& stream, const Person& p){
    return stream << "[" << p.age_ << "," << p.gpa_ << "]";
}

template<typename ForwardIterator>
void print(ForwardIterator begin, ForwardIterator end){
 while( begin != end) cout << *begin++ << " ";
 cout << endl;
}

bool ageComparison(const Person& l, const Person& r){
 return l.age_ < r.age_;
}
int main(){
 std::vector<Person> students;
 students.push_back( Person(18,3.5f) );
 students.push_back( Person(20, 2.9f) );
 students.push_back( Person(11, 4.0f) );
 cout << "Original : ";
 print( students.begin(), students.end() );
 std::sort( students.begin(), students.end(), ageComparison );
 print( students.begin(), students.end() );
}
mrnutty 761 Senior Poster

http://oopweb.com/Assembly/Documents/SPIM/Volume/Floating%20Point%20Instructions.htm

It basically converts a single precision into double precision and puts it in an appropriate register.

mrnutty 761 Senior Poster

You have to find the start and end position of the quoted text. In C++, using std::string you can use the following call to find the first quote position stringVariable.find_first_of("\"")//find the first '"' . So you can use "\"" to represent a quote character.

mrnutty 761 Senior Poster

??? I'm glad you know what that means. It just looks like spam to me... I'm tempted to report it for not being written in English.

这么明显的错误你都发现不了?你到底是不是C++编程人员 is in Chinese and maps to Such an obvious mistake you can not find it? You in the end is not C + + programmers in english.

mrnutty 761 Senior Poster

Actually forget the map approach. For small set of data it might be better to use just a sorted array. For example :

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <algorithm>

using namespace std;

class KeywordDictionary{		
private:
	std::vector<string> keyWords;
public:
	KeywordDictionary(){ _init();}
	bool isKeyword(const std::string& word)const{ return _isKeyword(word); }
private:
	void _init(){
		keyWords.push_back("if");
		keyWords.push_back("int");
		keyWords.push_back("for");
		//and more
		std::sort(keyWords.begin(),keyWords.end());
	}
	//check if the passed word is a keyword
	//The function does not care about lower/upper case
	bool _isKeyword(const std::string& word)const{		
		struct NoCaseCompare{
			bool operator()(const std::string& lhs, const std::string& rhs){
				string lhsCpy(lhs), rhsCpy(rhs);
				std::transform(lhsCpy.begin(),lhsCpy.end(),lhsCpy.begin(),tolower);
				std::transform(rhsCpy.begin(),rhsCpy.end(),rhsCpy.begin(),tolower);
				return lhsCpy < rhsCpy;
			}
		};
		return std::binary_search(keyWords.begin(),keyWords.end(),word,NoCaseCompare());
	}

};
int main() {  	
	KeywordDictionary keywords;
	cout << keywords.isKeyword("IF") << " " << keywords.isKeyword("For") << " " << keywords.isKeyword("null") << endl;
}

and later on, you can change the underlying implementation without breaking the clients code, if you choose to.

mrnutty 761 Senior Poster

Just create a std::map with all of the reserved words, and check for them using that.
And for your post script, unique just means there is only one of.

mrnutty 761 Senior Poster

Yes, can you use std::string instead of char arrays? Is there some restriction that says you can't? If you can use std::string, then it will be easy as this :

std::string str = "2011006+0000";
std::string::size_type pos = str.find_first_of("+");
if(pos == std::string::npos) { cout << "Not found\n"; return -1; }
else{ cout << str.substr(0,pos) << endl; }
mrnutty 761 Senior Poster

First of, can you use std::string?

mrnutty 761 Senior Poster

From wiki :

- First two bytes are identifier( type of image )
- Next 4 bytes are Size of BMP
- Next 4 bytes are unused
- Next 4 bytes are offset where Pixel array is
- Next 4 bytes are number of bytes in DIB header
- Next 4 bytes is the width of the bitmap
- Next 4 bytes is the height of the bitmap

The highlighted part is what you need. So the other, just read it into a temporary variable. I suggest you read it into a char. A char in C++ is 1 byte usually. So think a little and you should get it.

mrnutty 761 Senior Poster

Print out your "input" and see if its correctly formatted. Make sure you account for the spaces. Have you learned about classes and structures?

>>

I don't want to cheat. This is my major and I have a passion for it, but I can not find this answer in my book

Give you respect for this. Not many new posters have this mentality.

mrnutty 761 Senior Poster

Check out wiki_bitmap. It has good explanation of how bitmaps are formatted. From there you can determine how to read the width/height, and the starting offset of where the actual pixels starts from.

mrnutty 761 Senior Poster

is it faulty because the variable might get changed before the byte extraction?

mrnutty 761 Senior Poster

it would be :

int main(){
	char c1 = 0x12;
	char c2 = 0x34;
    char c3 = 0x56;
    char c4 = 0x78;
	//assume that sizeof(int) = 4 * sizeof(char) which is true for most
	int combined = ((c1 << 24)| (c2 << 16) | (c3 << 8) | (c4 << 0 )); 
	cout << "Combined = " << showbase << hex << combined << endl;

	int highPart = (combined >>24) & c1;
    int midC2 = (combined >> 16) & c2;
	int midC3 = (combined >> 8) & c3;
	int lowPart =  (combined >> 0) & c4;
	cout << highPart << " " << midC2 << " " << midC3 << " " << lowPart << endl;
}
mrnutty 761 Senior Poster

For the toInt(char c), it has to do with the ascii value. Assume that the variable c
is = '9'. If we subtract '0' from '9', that is '9' - '0' = 9. We get the integer nine.
We want to do this because '9' != 9, that is '9' has a different ascii value.

To reverse it you would do something like this :

int main(){
	char c1 = 0x56;
	char c2 = 0x78;
	int combined = (c1 << 8) | (c2 << 0 ); //assume that sizeof(int) = 4 * sizeof(char) which is true for most
	cout << "Combined = " << showbase << hex << combined << endl;
	int highPart = (combined >>8) & c1;
	int lowPart =  combined & c2;
	cout << "HighPart = " << highPart << "\n" << "LowPart = " << lowPart << endl;
}
mrnutty 761 Senior Poster

The thing is reading 24 bytes and putting it into a number is a hassle because C++ native data type on a 32 bit platform the biggest you will get is 8 bytes, from a double. If it was bits that you wanted to read then you can fit 64 bits into a double.

So If you can limit your self to just read upto 8 bytes then it wont be hard, in fact you can do something like this :

//simple conversion, note no error checking...
inline int toInt(char c){ 
	return c - '0'; 
}

//reads numOfBytes from data begining at the offset index in data
//if not enough bytes are available it returns what it can read
//prints a error message if an the size of the return type is less than the number
//of bytes requested or if the number of bytes requested is greater than the number of
template<typename T>
T readBytes(const std::vector<char>& data, size_t numOfBytes,  size_t offset = 0 ){
        assert(numOfBytes > 0 && <= 8);
	T result = T();
	//change to something more appropriate if needed, for example throw an exception or log error?
	if(sizeof(T) < numOfBytes) cout << "Warning overflow can occurr\n";
	if(data.size() < numOfBytes/4) cout << "Warning not enough data\n";

	if(numOfBytes == 1) result = toInt(data[0]);
	else if(numOfBytes == 2) result = (toInt(data[0]) << 8) | (toInt(data[1]) << 0);
	else if(numOfBytes == 3) result = (toInt(data[0]) << 16) | (toInt(data[1]) << 8) | (toInt(data[2]) << 0);
	//..more else if or …
mrnutty 761 Senior Poster

Are you interpreting the data in std::vector<char> as big-endian or little-endian? To reverse the process you an just shift right and use the bitwise and operator.

So just to get this straight, you have a std::vector<char> full of whole unsigned digits correct? And if the user wants to read say 4 bytes( an int ) then you want to develop an algorithm so that the function reads 4 bytes from the char-vector into a variable and then returns the variable. Is that right?

mrnutty 761 Senior Poster

then overload it. One for functions. And one with a variable argument list.

mrnutty 761 Senior Poster

Not quite but in this case you're perfectly right.I know it works that way though i was curious if i cand do it this way around.Thank you.

yes you can use inline files