@Blue Shell
INDENT YOUR CODE!!!
Do you realize how hard that is to read when you have numerous nested code blocks? Let me show you what the difference between yours and properly indented code looks like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
// Unindented
void vp::list<T>::sort() {
if (size() <= 1)
return;
for (int merges = 0, k = 1; merges != 1; k *= 2) {
node *p = firstNode, *q = p;
for (int qsize = merges = 0; p; merges ++, p = q) {
for (qsize = 0; qsize < k && q; qsize ++, q = q->next);
while (qsize && q) {
if (q->value <= p->value) {
if (q->next) {
q = q->next;
q->prev->insert_before(p);
}
else {
q->insert_before(p);
q = NULL;
lastNode = lastNode->prev;
}
if (p == firstNode)
firstNode = firstNode->prev;
qsize --;
}
else
p = p->next;
}
}
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
// Indented
void vp::list<T>::sort() {
if (size() <= 1)
return;
for (int merges = 0, k = 1; merges != 1; k *= 2) {
node *p = firstNode, *q = p;
for (int qsize = merges = 0; p; merges ++, p = q) {
for (qsize = 0; qsize < k && q; qsize ++, q = q->next);
while (qsize && q) {
if (q->value <= p->value) {
if (q->next) {
q = q->next;
q->prev->insert_before(p);
}
else {
q->insert_before(p);
q = NULL;
lastNode = lastNode->prev;
}
if (p == firstNode)
firstNode = firstNode->prev;
qsize --;
}
else
p = p->next;
}
}
}
}
|
It makes a world of difference.
@sam dhillon
I know a lot of stuff can be discouraging when it comes to programming, and don't get me wrong, it's not an easy ride. There is tons of challenges and a lot of headaches and stress. But once you figure it out and it just "clicks" the reward is amazing. Stick with it and don't give up.
In regards to functions, the idea is simple, it's a block of code that can be called no matter where your program is (C++ is a linear language, i.e. reads from the top to the bottom). The nifty thing about functions, however, is that it contain information to do a certain task, let's say getting user input. Well, later on down the road, you want to make the user enter input for a different program. Instead of rewriting code to do the same thing, you can just code that function into your new program and you're one step closer to being done with that one. It also makes it easier to go back and modify it slightly so that, let's say your math was wrong on calculating an average, you change it in one place and it affects everytime that function is called.
That is pretty easy to grasp, so let's move on to the arguments/parameters. Essentially they're the same. A function has parameters, but you call a function with arguments (Confused yet? It gets better). These are pretty nifty as well. It allows one block of code to essentially communicate with another block of code. I also like to image functions as kids in a class room and parameters as them passing notes around. Once you can visualize that, the concept of parameters starts to make sense.
Now let's look at the two different types of parameters, by value and by reference. By value is rather simple. Your function, the one that "calls" the other function, takes it arguments and makes a copy of them (this is done elsewhere, but figuratively speaking). These copies are passed to the function under a parameter name so that you can use these values. Changing these values does nothing to the originals, only the ones that exist in that function. Once the function is done, it returns back to you and your variables never changed (again, there are exceptions, but that's typical).
By reference is a little difference. Instead of generating a copy, you allow the function to essentially take a hold of your variable (by passing a reference to the memory location...boring stuff). Whatever the function does to that variable, directly reflects on the variable in your function. This can be really handy for doing some kind of math on a variable, getting user input, etc. You'll find out when you need to use one or the other. Just keep your head up and ask questions if you're unsure. Don't give up, and above all else, keep your goal in front of you at all times.