Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
Another perspective is that most computing types train themselves to organize hierarchically, we should write code in the same manner: a simple base (main program) that calls a few modules (5-9) that intern call modules until the entire program can be viewed as a tree.
Churchill: Madam, would you sleep with me for five million pounds? Socialite: My goodness, Mr. Churchill… Well, I suppose… we would have to discuss terms, of course… Churchill: Would you sleep with me for five pounds? Socialite: Mr. Churchill, what kind of woman do you think I am?! Churchill: Madam, we’ve already established that. Now we are haggling about the price.
The purpose of this "joke" was to point out that efficiency is a continuum -- not a single point. Since we do not chose to code solutions in optimized machine language, we clearly are willing to sacrifice some efficiency. How much is up the individual and the case.
Based on that observation, the fact that I have recommended modular code, and structured programming shows that efficiecy of run time is less important to me that code reliabilty, provability and ease of reading/debugging.
Return to Class Main Page
Example: Suppose you need to add random amounts to a total based on a value. Here are three examples that do that:
if (1 == i) { total = total + 17; } elseif (2 == i) { total = total + 29; } elseif (3 == i) { total = total + 99; }
switch (i) { // switch Case 1: total = total + 17; break; Case 2: total = total + 29; break; Case 3: total = total + 99; break; } // switch
int ary[4] {0, 17, 29, 99}; total = total + ary[i];
Example: You wish to test each of the 8 locations around the current location in an array.
for (i=-1; i<2; i++) { //for i for (j=-1; j<2; j++) { // for j if ((0 < ary[i][j) && (0<>i) && (j<>j)) { // sum up value total = total + ary[i][j]; } // sum up value } // for j } //for i
int i[7] {-1, -1, -1, 0, 0, 1, 1, 1}; int j[7] {-1, 0, 1, -1, 1, -1, 0, 1}; /* assume we are processing ary[x][y] */ for (k=0; 8>k; k++) { // for k if (0 < ary[x+i[k]][y+j[k]]) { // add it up total = total + ary[x+i[k]][y+j[k]]; } // add it up } // for k
Return to Class Main Page
F E D E F G D C A 9 A C D G D B 8 7 6 7 8 B D F C 8 5 4 3 4 5 8 C F E A 7 4 2 1 2 4 7 A E D 9 6 3 1 0 1 3 6 9 D E A 7 4 2 1 2 4 7 A E F C 8 5 4 3 4 5 8 C F D B 8 7 6 7 8 B D G D C A 9 A C D G F E D E F id dist dist points -- ------- ----- ---------------------------------------------------------- 0 sqrt(0) 0 = 0,0 1 sqrt(1) 1 = 1,0 0,1 -1,0 0,-1 2 sqrt(2) 1.414 = 1,1 -1,1 -1,-1 1,-1 3 sqrt(4) 2 = 2,0 0,2 -2,0 0,-2 4 sqrt(5) 2.236 = 2,1 1,2 -1,2 -2,1 -2,-1 -1,-2 1,-2 2,-1 5 sqrt(8) 2.828 = 2,2 -2,2 -2,-2 2,-2 6 sqrt(9) 3 = 3,0 0,3 -3,0 -3,-3 7 sqrt(10) 3.162 = 3,1 1,3 -1,3 -3,1 -3,-1 -1,-3 1,-3 3,-1 8 sqrt(13) 3.606 = 3,2 2,3 -2,3 -3,2 -3,-2 -2,-3 2,-3 3,-2 9 sqrt(16) 4 = 4,0 0,4 -4,0 -4,-4 A sqrt(17) 4.123 = 4,1 1,4 -1,4 -4,1 -4,-1 -1,-4 1,-4 4,-1 B sqrt(18) 4.243 = 3,3 -3,3 -3,-3 3,-3 C sqrt(20) 4.472 = 4,2 2,4 -2,4 -4,2 -4,-2 -2,-4 2,-4 4,-2 D sqrt(25) 5 = 5,0 4,3 3,4 0,5 -3,4 -4,3 -5,0 -4,-3 -3,-4 0,-5 3,-4 4,-3 E sqrt(26) 5.099 = 5,1 1,5 -1,5 -5,1 -5,-1 -1,-5 1,-5 5,-1 F sqrt(29) 5.385 = 5,2 2,5 -2,5 -5,2 -5,-2 -2,-5 2,-5 5,-2 G sqrt(32) 5.657 = 4,4 -4,4 -4,-4 4,-4As you can see, a spiralis a real pain in the you know what to implement. If you tried to do this programatically, the calculations would kill you. With offset arrays:
x[]={ 0, 1, 0,-1, 0, 1,-1,-1, 1, 2, 0,-2, 0, 2, 1,-1,-2,-2,-1, 1, 2 ... y[]={ 0, 0, 1, 0,-1, 1, 1,-1,-1, 0, 2, 0,-2, 1, 2, 2, 1,-1,-2,-2,-1 ... for (k=0; 17>k; k++) { // loop for each spot of the spiral process ary[i+x[k][j+y[k]] } // loop for each spot of the spiral
Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
Look at the class references page for sample links to cheat sheets (if you find better send them to class).
Return to Class Main Page
Return to Class Main Page
Return to Class Main Page
The statements and opinions included in these pages are those of only. Any statements and
opinions included in these pages are not those of Louisiana
State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Isaac Traxler