Please ignore the system"(PAUSE"); it's just there so I can see what it does. Besides, it's not a program for use for anything. I was just trying to remember the array like I said.
I added "i" instead of "j" at a rather crucial point. for(int j = 0; i < COLUMNS; ++j)
Running this with the "j" in place, it works fine.However, with the "i" there it produces a very heavy crash. This isn't so much a call for help, since I know how to fix it. But I'm curious about what happens when it compiles like that and runs, but produces such wierd results.
I hope someone understands what I mean by my question.
Thanks for any replies.
The variable i never changes, the test (i < COLUMNS) will never be false, and the for loop keeps going forever. Eventually, the app tries to use memory outside that allocated to the app by the OS.
Also, you should be using postfix (i++ instead of ++i). Right now you're only printing out four elements of the array.
No, you should be using prefix. ++var is always more efficient than var++ (unless some idiot programmer wrote a stupid pre-increment operator for the class).
prefix returns the value after increment/decrement
postfix returns the value before increment/decrement
other than this difference, the two are exactly the same.
1 2 3 4 5
int a = 5;
int b = a++; // a=6, b=5
a = 5;
b = ++a; // a=6, b=6
The reason this makes the prefix form preferable is because the postfix form may require one or more additional variables/objects to be created. This is usually only significant with complex classes that overload these operators where copying the object has significant overhead -- but for basic types like int, it doesn't really matter (though it can't hurt to get in the habit of using prefix form by default for form a good habit):
1 2 3 4 5 6 7 8 9 10 11 12
SomeClass& SomeClass::operator ++() // prefix increment operator
{
Increment(); // increment it
return *this; // return new value
}
SomeClass SomeClass::operator ++(int) // postfix increment operator
{
SomeClass r(*this); // copy object's current state
Increment(); // increment this object
return r; // return OLD value (read: this requires *another* copy)
}
here... prefix requires no copies. postfix requires 2 copies. If copying is computationally expensive, this could be a problem.
But like I say -- for types like 'int' it doesn't really matter.
Thanks a lot guys.
I know this is totally basic, but stuff just slips my mind all the time and I find myself having to reread old chapters and books.
I'm a bit of a slow learner. When I really get something it sticks forever, but it's getting to that part which is really annoying. Anyway, thanks again.
You shouldn't try to memorize how to work with multi-dimensional arrays. Rather, if you understand how they are laid out in memory, you will easily be able to figure out how to work with them.
Just a suggestion... I find a lot of college students resorting to rote memorization of things that aren't worth the time and effort. Programming is an art form in that programming is problem solving, and often times there isn't an absolute right way to solve a problem. It requires creativity.
In the case of a for loop is the post increment vs. pre increment really an issue? Isn't the compiler going to be smart enough to know that a temporary object isn't needed either way in this case?
However, for simplicity's sake, if it does not make a difference, you should always pick preincrement since it will never be less efficient than postincrement, but possibly more
efficient.