In last week’s Lesson, I covered the ftell() function, which returns the current file position indicator. That indicator can be manipulated, allowing you to control how a file is read in a non-sequential way.
Continue reading
Category Archives: Lesson
Messing with File Access
Like reading a book, sequential file access starts at the file’s beginning (or top) and reads bytes one after the other until the nefarious EOF is encountered. You probably don’t think about how that works, and you don’t need to. The operating system handles the job of reading a file; your C code is simply along for the ride.
Continue reading
To Increment Before or After
Say you have a for loop that increments one notch each time it repeats. I code such a loop in this fashion:
for(x=0;x<10;x++)
More common, however, programmers use this approach:
for(x=0;x<10;++x)
The difference is in how the variable x is incremented. I put the ++ after the x, but most coders put it before. What the deal?
Continue reading
Skipping
Of the two C language keyword looping statements, for is the most traditional and probably the most popular. It’s also the most frustrating for beginners because of its many parts. But eventually, a comfort level arises with using the for loop, which is sad because it’s more powerful than a simple counter.
Continue reading
C Language Neat Tricks #2 (Part II)
You could call it a neat trick or just something so oddball insane that few programmers would dare breech the territory: A variable that holds the address of a function; a function pointer.
Continue reading
C Language Neat Tricks #2 (Part I)
Like a variable in the C language, a function has an address — a location in memory. This fact shouldn’t be surprising to you. After all, the function has to sit somewhere. And my guess is that location doesn’t change as the program runs.
Continue reading
C Language Neat Tricks #1
The C language holds the potential of being utterly terrifying, which is something I admire greatly. Many beginners, as well as foolhardy travelers from other languages, treat a lot of the C language constructs as absolutes when, in fact, they aren’t. Therefore I present this week’s Lesson, the first in an ongoing series about strange, beautiful, and frightening things in the C language.
Continue reading
More Heavily Weighted Random Numbers
One area where weighted random numbers come in to play is when doing a simulation. In a recent set of Exercises (May and June 2015), your mission was to craft a bowling game simulator. Like most simulations, the bowlings game simulator made use of random numbers. The problem in this situation is most decent bowlers don’t roll randomly.
Continue reading
Weighted Random Numbers
Random numbers are useful when simulating information and they add a degree of unpredictability to computer games. The problem programmers run into, especially with simulation, is that not everything is truly random. In many cases, some numbers need to be more random than others. The solution is to generate weighted random numbers.
Continue reading
Random Numbers with Decimals
The rand() function returns a pseudo-random number as a long int value. That value helps your code generate random numbers in the long int range. It can also be manipulated to yield random real number values, but that process involves . . . math.
Continue reading