Introduction to Programming and C++

Contents Previous Topic Next Topic

A programming language is designed so that it may be interpreted by the compiler without any ambiguity. Because these instructions indicate what a CPU should do, it is possible that it may be less obvious for humans to determine what is going on. In some cases, it may be easy to determine what the program is doing. In other cases, it may not be so obvious. In most cases, the programmer who writes the code believes that it is obvious, and the programmer could not be more wrong. Take your last project from ECE 150, and by just examining the code, try to determine what the code should do.

As another example of obscure C++ code, consider Code Fragment 1 which presents some reasonably efficient code which does...something.

Code Fragment 1.

	for ( int i = 0, * ptr = a; i < N; ++i ) {
		*(ptr++) = -1;
	}

An expert programmer will immediately notice that this code initializes each entry of an array of N integers to -1, however, this may not be as obvious to the average programmer, or even a good programmer.

Comments allow the author of code to indicate what he or she has done by using English. Comments are ignored by the compiler but are invaluable to anyone reading the code (including the original author in the future).

There are two types of comments:

Single-Line Comments

Two slashes, //, indicate that the compiler should ignore anything on this line after and including these symbols. This is demonstrated in Code Fragment 2.

Code Fragment 2.

	// initialize each entry of the array 'a' to -1

	for ( int i = 0, * ptr = a; i < N; ++i ) {
		// assign and increment the pointer
		*(ptr++) = -1;
	}

Block Comments

The compiler also ignores anything between and including the symbols /* and */. This is more useful to describe how a particular function works. For example, consider the function shown in Code Fragment 3.

Code Fragment 3.

/* double sacos( double x )
 Small-Angle Cosine
  
 This function uses the Taylor series to
 approximate cos(x) for small values of x
 by using

                1  2
   cos(x) ~ 1 - - x
                2
                                     -5
 The absolute error is less than 10
 for |x| < 0.1 . */

double sacos( double x ) {
	// 2nd-order Taylor approximation
	return 1 - 0.5*x*x;
}

Sometimes, attention is drawn to comments by adding extra stars, as is shown in Code Fragment 4.

Code Fragment 4.

/********************************************
 * double sacos( double x )
 * Small-Angle Cosine
 *
 * This function uses the Taylor series to
 * approximate cos(x) for small values of x
 * by using
 *
 *                1  2
 *   cos(x) ~ 1 - - x
 *                2
 *                                   -5
 * The absolute error is less than 10
 * for |x| < 0.1 .
 *******************************************/

double sacos( double x ) {
	// 2nd-order Taylor approximation
	return 1 - 0.5*x*x;
}

Examples

The C code shown in Program 1 could use some comments. Please don't try to compile this with Dev C++: it won't work.

Program 1. The Twelve Days of Christmas.

#include <stdio.h>
main(t,_,a)char *a;{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,
main(-86,0,a+1)+a)):1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n' ')# \
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
  :0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

You may, if you wish, use the name and try to guess what it does. And no, it's not just one verse. If you do not have a C compiler, you may view the output here.

There is, however, one spelling mistake. If you wish, you may try to correct it.


Contents Previous Topic Top Next Topic