A perfect shuffle splits a deck of cards in two. The second half is folded evenly into the first half, so that every other card comes from the first and last half of the deck, respectively. This type of shuffle is practically impossible in real life, but for a computer simulation it’s not that difficult.
Continue reading
Author Archives: dgookin
Shift and Verify a Magic Square
Last week’s Lesson showed how to shift a column of numbers in an array grid. The array just happens to be a magic square. So the puzzle is to run the array through the confirm_magic() function, which was presented in a previous week’s Lesson. Seems easy.
Continue reading
The Perfect Shuffle
A perfect shuffle occurs when you shuffle a deck of cards so that all the cards from the second half of the deck are perfectly interspliced between the cards in the first half of a deck. Such a shuffle is illustrated in Figure 1.
Continue reading
The Square is Really Magic
Before moving off the topic of arrays and their bogus dimensions, I want to play further with a magic square. Specifically, it intrigues me that you can shift rows or columns within the square and it doesn’t affect the magical properties.
Continue reading
Pass a 2D Array to a Function (No Pointers!)
Suppose you have an array of integers which represents a magic square: All the rows and columns — even the two diagonals — add to the same total. To prove it, you create a function, confirm_magic() that processes the array and validates the math. You have just one problem . . .
Continue reading
A Single-Dimension Array Pretends to be Two-Dimensions
Multi-dimension array notation is just a handy shortcut for you, the human programmer. Internally, an array is a single-file line of values, one marching after another. The dimensional aspect helps humans organize the array’s data, but all that organization is superficial.
Continue reading
Day-of-the-Year Calculation – Solution
The solution for this month’s Exercise involves two steps. The first is to generate a random value from 1 to 365 (inclusive) as a day-of-the-year value. The second is to determine upon which month and day that value falls. That part may offer more difficulty than you anticipate.
Continue reading
Two Dimensional Arrays are a Myth
Do C language pointers frighten you? Good! They’re supposed to, mostly because few instructors bother explaining them well, but also because of the nomenclature: “Pointers point.” Regardless, if you shun pointers, as many C programmers do, you can fall back on array notation. It’s a useful alternative and a handy shortcut, but it’s completely bogus.
Continue reading
Day-of-the-Year Calculation
Today is May 1st. It’s the 121st day of the year 2017. In 2016, May 1 was the 122nd day of the year, because 2016 was a leap year. You can look up this information on a calendar or, more disappointingly, use Google. You can also write code that does the calculation for you, which is this month’s Exercise.
Continue reading
Structure Size and Memory Alignment
Though it’s possible to guess the byte size of a structure, it’s bad practice to do so. Even advanced C programmers, who know variable widths by heart, rely upon the sizeof operator to obtain a structure’s size.
Continue reading