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

are you Mayan?

No, just tired of life. Ready for infinite happiness.

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

Alright fine. But saying my code is a piece of crap is a little harsh, that was just uncalled for

That should motivate you even more to learn and write better code. But I see your ego is in the way. If it helps, your code isn't bad. The problem was just the way you used templates.

mrnutty 761 Senior Poster

Well thanks, I appreciate everybody's efforts to make me look like I'm an idiot... other than that, thanks for the advice. I was just trying to understand how to implement them, i wasn't going so much for "am i using it right?" sheesh.

Take the criticism as a help instead of brushing it off your shoulder. Every time someone points what I'm doing wrong, I take that as an advice from someone who knows more and learn and become better from it. And if you want to be a good programmer, I suggest you do the same.

mrnutty 761 Senior Poster

Truthfully, that gives absolutely no advantage, bloats code, makes it hard to read, and is probably slower( although not noticeably ). And as your friend suggest, and your ego denied, its just bad coding. Drop this like a bad habit.

mrnutty 761 Senior Poster

As said earlier, you aren't using the template correctly. In your "SetName", if a template instantiation is made of type double, then you will have a compiler error, because you can't assign a double to a string.

mrnutty 761 Senior Poster

Forgot to include the string library.

mrnutty 761 Senior Poster

No you are reading it wrong. This is what I mean :

class Point{
 int x_,y_;
public:
 Point(int x = 0, y = 0) : x_(x) , y_(y) {} 
 int getX(){ return x_; }
 int getY(){ return y_; }
 void setX(int x) { x_ = x; }
 void setY(int y) { y_ = y; }
};

//users code
Point p(0,0);
int dx = 5;
int dy = 5;
 //translate the point
p.setX( p.getX() + dx );
p.setY( p.getY() + dy );

notice how the user is using get/set function to achieve translation. So I am suggesting that the class Point just have a translate(int dx, int dy) function so that it reduces the need for get/set. Thats the general idea. Of course in the class Point it might not be wise to remove getX/setX and getY/setY altogether.

mrnutty 761 Senior Poster

As to the original question

Is it a good programming practice to not use getters and setters in trivial parts of code?

If you can you should avoid the getters and setters and create a function that already does what the user would do by using the getters and setters. Of course that doesn't mean that you should get rid of getters and setters altogether, because sometimes its not possible, just minimize it.

And as to your last post, I have no idea what you want with that.

mrnutty 761 Senior Poster

If you can, eliminate all the pointers for const-reference.

mrnutty 761 Senior Poster

For the first one, the person a_i is in the subgroup iff there are 5 pairs such that an element from the pair is a_i. So you can use that as a clue.

mrnutty 761 Senior Poster

Once you do a build from visual studio, the .exe should be in the project's directory.
You can just run that.

mrnutty 761 Senior Poster

Yea most tutorial you will find are depreciated. check out www.gamedev.net. That is a very well know game development environment. They have many resources and tutorials you can learn from. Plus they have wicked smart and experience people there.

mrnutty 761 Senior Poster

Do yo know how to generate the random numbers? take a look here for some reference.

mrnutty 761 Senior Poster

Why would it be arrayEnd + 1? From tradition, arrayEnd should already be 1-pass the end of the array. So using your function this call would be a bug :

const int S = 5;
int array[S] = {0};
display(array, array + S);

because you will be looping through array + S + 1 which is an invalid pointer.
So take that +1 out of the loop and let it only be while(arrayBegin != arrayEnd){...}

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

Programming is logical. So you need to train your self how to think logically. Some people compare programming with math because they both require perfect logic. So my suggestion is to change your mind of thought. Train yourself to think more logically. One way to do this, is to do simple practice problems. Not only it trains your mind to think logically but it also gives you experience coding. And if you don't have the passion to practice and to get better each day, then you should probably switch majors.

eman 22 commented: good comment +0
mrnutty 761 Senior Poster

I wish the world ends on 2012

mrnutty 761 Senior Poster
for(int i=1; i<N; i++)
for(int j=1; j<N; j++)
for(int k=1; k <= i*j*log(j); k*=2) x=i+j+k;

Your first for-loop runs at most N times.
And for each i, it runs at most N times,
and for each j, it runs at most i*j*log(j) times

Notice i and j is always less than N, thus i*j*log(j) <= N*N*log(N)

Thus the total runtime or Big-Oh of you loop is : (N)*(N)*(N*N*log(N)) = N^4*log(N)

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

Yea after more research, I figured that was the case. Thanks for the confirmation. Thanks.

mrnutty 761 Senior Poster

Are you using solely C++. What platform? Does it have to be a portable solution? Can you use anything else?

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

If I use ProcessBuilder or Runtime.exec function, in a JApplet, does that applet needs to be signed or is there a way to get around this? I need to communicate with a C++ executable using java JApplet.

mrnutty 761 Senior Poster

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

mrnutty 761 Senior Poster

you shouldn't have to use shaders for this. Setting height is just a numerical thing. Rendering it is different. You can render it with or without shaders

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

mrnutty 761 Senior Poster

Yes, you can get your input into a string and parse it.

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

int main()
{
  <snip>
  stringstream(input) >> value;
    <snip>
}

if that fails, stringstream throws and error so testing for 0 value is not a good option. And what happens if the input is actually 0?

mrnutty 761 Senior Poster

See but thats not what you said. You mentioned nothing about mod 10.

mrnutty 761 Senior Poster

So you want something like this then.

#include <iostream>
#include <string>

using namespace std;

typedef string (&ErrorFunc)();

string myError(){ return "Failure"; }

void logFile(const std::string& id,const ErrorFunc func){
  cout << id << func() << endl;
  //log into file
}

int main(){
 logFile("Msg : ", myError );
}
mrnutty 761 Senior Poster

If you want your integer set to become set range of tenths, you just need to multiply rand()'s output by 0.1

Thats like dividing rand() by 10, which wont work as you said. He could easily insert {0.1 ... 0.9 1.0 } into a vector and just shuffle it.

mrnutty 761 Senior Poster

So in the above example, you want the message generated by myeql_error() to be logged into the file? I am assuming mysql_error() returns a string or something similar?

mrnutty 761 Senior Poster

@OP: Usually you should avoid the usage of static keyword. To further explain the problem consider what happens here :

int x = 10;
int &y = x;
y = 20; //x = 20 as well

Since y is a reference to x. Changing y will also change x. Basically, y is another name for x.

Now what happens when x is not valid, meaning when it goes out of scope? For example
this function shows such a case:

int& getFive(){
 int x = 5;
 return x;
}

and call it like so int& y = getFive(); . Now just like in the other y is a reference to x. But this time x is not valid. x is not valid because it goes out of scope and the compiler destroys it. Here is a psuedo-sequence of what happens in the above call to getFive();
1) y is declared as a reference to getFive();
2) getFive() gets executed
3) Inside getFive() the variable x gets created and initialized to 5. Since x is not declared static, you tell the compiler that it goes out of scope at the end of the function and thus should get destroyed and free the memory it occupies.
4) Now you try to return x. But the function says that return x as a reference so the compiler possibly tries to return x as a reference but you told the compiler to destroy x when …

mrnutty 761 Senior Poster

You don't need shaders. All you need to do is something like this :

for( int x = 0; x < MAP_WIDTH; ++x){
  for(int z = 0; z < MAX_HEIGHT; ++z){
    surface[x][y].setHeight( heightMap.getHeight(x,z) );
 }
}
mrnutty 761 Senior Poster

>>For example, your function is incapable of returning any value strictly between 0 and 1/RAND_MAX

Thats true. I never thought about that. But theoretically, cant we scale and shift the values to fall into range? But then that might affect the distribution probability.

mrnutty 761 Senior Poster

You are using depreciated header files, take out the .h. And int main is in these days. And you need to either use fully qualified name or use the using declaration.

#include<string>
#include<iostream>

int main(){
 using namespace std;

  string str = "My name is shridhar";
  cout<<"What is your name"<<str;

  char pause = 0;
  cout << "Press enter to continue...";
  cin.get(pause);
}
mrnutty 761 Senior Poster

check the documentation

mrnutty 761 Senior Poster

You should either convert the a into a string or adjust your parameters. For example :

template<typename ReturnType, typename InputType>
ReturnType convertTo(const InputType& input){
 stringstream stream;
 stream << input;
 ReturnType val = ReturnType();
 stream >> val;
 return val;
}
//...your LOGMSG function
int main(){
 int a = 100;
 string msg = "type test!!" + convertTo<string>(a) + " times";
 LOGMSG( msg );
}
mrnutty 761 Senior Poster

@Op: I'm guessing you want something like so :

double randomReal(){
 return double(rand()) / double(RAND_MAX);
}
mrnutty 761 Senior Poster

So first you need to be able to render a flat 3D surface, and for each point in the surface, you will use the heightmap as the heightvalue for that point.

mrnutty 761 Senior Poster

You need to also make sure its on fixed format. Here is an example :

void printDollars(const float amount){
 //save old states
 std::ios_base::fmtflags oldFlg = cout.setf(std::ios::fixed, std::ios::floatfield);
 int oldPrecision = cout.precision(2);

 cout << "$ " << amount << endl;

 //reset old states
 cout.setf(oldFlg);
 cout.precision(oldPrecision);

}
mrnutty 761 Senior Poster

The following is a common error:

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

int main()
{
    std::string line;

    std::cout<<"Enter a line: ";

    if (getline(std::cin, line)) {
        std::cout<<"Before: "<< line <<'\n';

        // This is wrong!
        std::transform(line.begin(), line.end(), line.begin(), std::toupper);

        std::cout<<"After:  "<< line <<'\n';
    }
}

What's the problem? Unbeknownst to many, std::toupper is overloaded. There's the familiar version in <cctype> and another version in <iostream> with a second argument for the locale. So which of these is transform supposed to pick when you haven't specified the arguments? The answer is it's ambiguous, and shouldn't compile.

The following is correct:

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

struct upper_case {
    int operator()(int c)
    {
        return std::toupper(c);
    }
};

int main()
{
    std::string line;

    std::cout<<"Enter a line: ";

    if (getline(std::cin, line)) {
        std::cout<<"Before: "<< line <<'\n';

        // This is correct
        std::transform(line.begin(), line.end(), line.begin(), upper_case());

        std::cout<<"After:  "<< line <<'\n';
    }
}

By creating a function or function object which calls the version you want explicitly, the ambiguity is removed. Alternatively, you can use the locale-friendly version:

#include <locale>

struct upper_case {
    int operator()(int c)
    {
        return std::toupper(c, std::locale());
    }
};

Thanks, didn't know that really. Under visual studio it doesn't complain, but www.codepad.org seems to complain, not sure what compiler they use. Narue, do you know what compiler reports this as an error? And looking at C++ reference it only reports one function and no overloaded function.

EDIT: Oh I see. The template version is probably included indirectly, and …

mrnutty 761 Senior Poster

That shouldn't be the case, make sure your not seeding more than once

mrnutty 761 Senior Poster

What do you mean you want to display the heightmap? I am assuming your "heightmap" is a file with bunch of numbers? How would you display that? Do you mean to print it?

mrnutty 761 Senior Poster

>>How do these suggestions support......."If a user inputs a int number, say 150..."

Easy conversion from char to int and easy error checking:

char ch = cin.get();
if(isdigit(ch)) int num = ch - '0';
else { alertError("Invaild input"); }

or for OP a better example :

char input = 0;
while( cin.get(input) ){
 if( isdigit(input) ) cout << input << endl;
 else { break; }
}
mrnutty 761 Senior Poster

Just to point out, that you shouldn't really throw an exception unless its absolutely necessary. Like the name suggest, an exception should be thrown in exceptional cases. If you can handle the exception then do it, else you have no choice but to propagate it.