You're a goober.
The best exercise is just programming constantly. I program at work, program when I get home, and whenever I'm not programming, I'm thinking about it. I think as long as you get a solid 4-12 hours a day at it, you're bound to improve considerably, which is of course, just like anything else. One of my favorite past-times is creating libraries of mechanisms. A mechanism in this sense, is an atomic operation that is often reused. Start with the smallest mechanisms and then build bigger mechanisms with these. Here's an example:
How often have you done this?:
Code:
int Foo(int new_value){
int old_value = value;
value = new_value;
return old_value;
}
This could be considered a mechanism, and it is a very common operation. Templated we get:
Code:
template <typename number>
number cycle(number& value, number new_value){
number old_value = value;
value = new_value;
return old_value;
}
Now we can rewrite Foo() like this:
Code:
int Foo(int new_value){
return cycle(value, new_value);
}
Now consider this operation:
Code:
void Swap(double a, double b){
double temp = a;
a = b;
b = temp;
}
Of course, the STL has the swap() function, but bear with me. We can reuse the cycle() mechanism since it does half of a swap:
Code:
template <typename number>
void swap(number& a, number& b){
b = cycle(a, b);
}
Finally, let's consider where you need to an arrays elements over to place a value somewhere in the middle, much like a linked list. Not a very efficient operation, but then sometimes it must be done. Here's a simple solution using the swap mechanism:
Code:
template <typename type>
type shift_back(type array[], int start, int stop, type io = 0){
int iter = start;
while(iter < stop){
swap(array[iter++], io);
}
return io;
}
template <typename type>
type shift_back(type array[], int length, type io = 0){
return shift_back(array, 0, length, io);
}
template <typename type>
type shift_forward(type array[], int start, int stop, type io = 0){
int iter = stop;
while(--iter >= start){
swap(array[iter], io);
}
return io;
}
template <typename type>
type shift_forward(type array[], int length, type io = 0){
return shift_forward(array, 0, length, io);
}
And now let's put it "practical" to use.
Code:
int main(int argc, char *argv[])
{
char message[81] = {0}, c = ' ';
strncpy(message,
"News Flash :: News Flash :: News Flash :: News Flash :: News Flash :: News Flash :: News Flash ::", 80);
while( !kbhit() )
{
printf("\n\n\n\n\n\n\n%s", message);
c = shift_forward(message, 80, c);
Sleep(100);
clrscr();
}
getch();
c = ' ';
while( !kbhit() )
{
printf("\n\n\n\n\n\n\n%s", message);
c = shift_back(message, 20, 60, c);
Sleep(100);
clrscr();
}
return 0;
}
So mechanisms are the nuts and bolts of a program. The more you capture, the more generic and effortless programming will be for you.
I hope that was helpful to you. Happy coding.