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
Category Archives: Lesson
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
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
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
Look at the sizeof That Thing
A structure is a multi-variable, containing several variable types, all members of the same unit. Two declarations are required for a structure: The structure itself and the variable. Programmers get into trouble with structures when determining the variable’s size, using typedef as a structure shortcut, and when declaring structure pointers.
Continue reading
Surrender to the Overflow
It’s a common question beginning programmers ask: “Why use different types of variables when every number can be expressed as a float?”
Continue reading
The strftime() Function
Being a C nerd, I occasionally browse the C library to discover new functions. Such an exercise is a must, not just for C but for any programming language. That’s because programmers can become complacent and rely upon the same old tricks. Only by reviewing the libraries, or by looking at other programmers’ code, can you learn new things.
Continue reading
Reducing Roman Numerals
I’m sure a mathematical solution exists to condense Roman numeral values. The values ending in 4 or 9 can be reduced, but whatever that solution is, it’s beyond me. So for my resolution to the Roman numeral reduction problem, I rely on strings instead of math.
Continue reading
From Decimal to Roman
The C language lacks a printf() conversion character to display Roman numerals. Therefore, if your code requires input of a decimal value, say 266, and output of a Roman numeral value, which would be CCLXVI, you must craft your own function.
Continue reading
From Roman to Decimal
Roman numerals are composed of letters, so it makes sense that their “values” are input and displayed as strings. To translate from that string into an integer, your program must convert each character into its corresponding decimal value. Sounds simple, right?
Continue reading