(Post Under Construction)
Kernighan and Ritchie "The C Programming Language" Second Edition is an classic interpretation of the C language in the same way as Strunk and White "Elements of Style" is a classic interpretation of the English language or Wolfgang Pauli's lecture on "Wave Mechanics" is a classic representation of the math of physics. This does not mean that all of the programming in K&R C is useful today or even the best representation of the use of C (1, 2). A paraphrased statement of Pauli's famous belief about untestable physics applies to engineering software as well: If your code can't be proven wrong by someone, you have probably not produced anything of much value.
Software engineering languages have variability, flexibility and polymorphism by design There are always multiple solutions to any given problem. Without honoring this fundamental nature of exploratory science; the power and endurance of any one language is limited. There are a number of K&R solution sets provided on the web. Below is a selection of solutions to the Fahrenheit-Celsius converter code discussed in Chapter 1 of "The C Programming Language" as provided by Richard Heathfield. A C language reference is available from the GNU Project. It is particularly important to read the while and do statement sections. A number of third party GNU C tutorials are also available. Use the google search term: 'filetype:PDF gnu c programming tutorial'. Learning GNU C is one such.
This post is primarily about understanding the logic and control structures of C. The implementation of C library functions (e.g. printf) are left for another discussion. -RMF
Notes on Teaching Programming quite possibly from a 'STEM' perspective. This is a course designed for upper school or high school students at Saint Paul's Academy in Bellingham, WA. An associated web page is http://teachingprogramming.rmfmedia.com/ .
Friday, November 22, 2013
Thursday, November 21, 2013
K&R C SE Lesson 2: Introducing printf, the 'for loop', and gdb
(Post Under Construction)
This post uses an example from the first chapter of K&R C Programming Second Edition. After discussing the use of the printf function, K&R C SE demonstrates alternate code for printing a Fahrenheit-Celsius conversion table. The specific and compact use of printf arguments allows for the appropriate formatting of the results returned by a compact conversion routine inside of a for loop. This code is more compact and more accurate than the first example in Lesson 1 using the while loop:
#include <stdio.h>
/* print Fahrenheit-Celsius table */
main()
{
int fahr;
for (fahr = 0; fahr <= 300; fahr = fahr + 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0) * (fahr-32));
}
This post uses an example from the first chapter of K&R C Programming Second Edition. After discussing the use of the printf function, K&R C SE demonstrates alternate code for printing a Fahrenheit-Celsius conversion table. The specific and compact use of printf arguments allows for the appropriate formatting of the results returned by a compact conversion routine inside of a for loop. This code is more compact and more accurate than the first example in Lesson 1 using the while loop:
#include <stdio.h>
/* print Fahrenheit-Celsius table */
main()
{
int fahr;
for (fahr = 0; fahr <= 300; fahr = fahr + 20)
printf("%3d %6.1f\n", fahr, (5.0/9.0) * (fahr-32));
}
Tuesday, November 12, 2013
XCode, Text Editors, Peanut Butter
I want to wish a big thank you for the guest lecture from software engineer and WWU statistics instructor Brian Morgans who taught a fascinating class on program instruction debugging; all with a loaf of bread, some peanut butter and a knife! I think the students had a great time! In conclusion, Brian talked about sorting algorithms which are an excellent introduction to developing algorithms in programming. I recommend the Wikipedia article and links on sorting algorithms. For a fun (but noisy) visualization of sorting algorithms, see here.
We will meet again in the first two weeks of December. I have installed Xcode 3.0 on many of the MACs in the lab and will soon install the entire lab. This allows you to use the gcc and g++ commands from your bash prompt. There will be a graphical interface to XCode 3.0. More on that later. In using the Xcode installation, there may be path issues for libraries you need to address. Read The Fine Manual and e-mail me any questions. The GNU gcc and g++ manuals are here.
Subscribe to:
Posts (Atom)