Freethought & Rationalism ArchiveThe archives are read only. |
07-09-2003, 05:54 PM | #31 | |||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Next. Here’s my code Principia references next (it's the code that confuses Principia, and as we all know, if Principia can't understand it, then it MUST be spaghetti code....ROTFLMAO!) Code:
nFoundOne = 0; while (nFoundOne == 0) { nIndex = (int) GetRandomNumber(0, nLetteredTiles - 1); if (nDiscardTilesOnceChosen == 0) { // Doesn't matter if the tile has been // chosen previously because selected // tiles are placed back into the urn. nFoundOne = 1; } else if (cUrn[nIndex][2] == 'T') { // This tile has already been chosen and // discarded: it can't be selected again. nFoundOne = 0; } else if (cUrn[nIndex][2] == 'F') { // This tile has not been chosen previously. nFoundOne = 1; } } Quote:
Quote:
Let’s look at more of Principia’s “analysis”… picking up right where we left off… Quote:
Quote:
Of course I, unlike Principia, will be smart enough to comment out the cout statements in the loop when doing any type of time-sensitive measurements (every computer programmer knows that massive amounts of screen output slows execution considerably). |
|||||
07-09-2003, 06:04 PM | #32 | ||||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
It is Principia who asserted that ifs are, in and of themselves, "spaghetti code". My position is that they are not, but are instead, the fundamental selection structure in structured programming, found and used in every leading structured and object-oriented programming language in existence. Quote:
Quote:
|
||||
07-09-2003, 06:29 PM | #33 | |
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
|
|
07-09-2003, 06:53 PM | #34 | |
Veteran Member
Join Date: Jan 2001
Location: Median strip of DC beltway
Posts: 1,888
|
Quote:
Most programming is taking an algorithm that already exists and connecting it to other algorthms in a sequence. Implementing an insertion sort on a linked list day in and day out isn't creating a new algorithm. If you want to be completely pedantic about it, then I'll agree that all programming is creating a "new" algorithm. I'd also have to say that everytime someone loads a truck it's implementing a new algorithm as well. |
|
07-09-2003, 07:02 PM | #35 | ||
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Quote:
Conceptually, a 9x3 array (in C++ or C or other languages that use 0-based arrays) would appear like the following: Code:
[0, 0] [0, 1] [0, 2] [1, 0] [1, 1] [1, 2] [2, 0] [2, 1] [2, 2] [3, 0] [3, 1] [3, 2] [4, 0] [4, 1] [4, 2] [5, 0] [5, 1] [5, 2] [6, 0] [6, 1] [6, 2] [7, 0] [7, 1] [7, 2] [8, 0] [8, 1] [8, 2] This code: Code:
for (int row = 0; row < 9; ++row) { for (int col = 0; col < 3; ++col) { cout << myArray[row][col] << endl; } } Code:
for (int index = 0; index < 27; ++index) { cout << *(myArray + index) << endl; } The second form traverses the elements of the two-dimensional array one right after the other, with no stopping to move to the right. This would be “impossible” if the array elements weren’t stored linearly and contiguously. Why is this important? If the conceptual version were reality, then overshooting the second index by 1 would cause the last element’s data to written into the wrong row – the data for one row would be mixed in with the data for the next row, pushing its last data element into the next row, all the way down the line. No row would be immune from corruption. But since array elements are actually stored contiguously, in a linear manner, consistently overshooting by one simply displaces all elements by one, keeping the whole array intact. The only issue that might arise is that the very last element overshoots the array bounds by 1 element’s worth. But, as long as the compiler doesn’t override my sequence of variable declarations to try some optimization, that doesn’t occur. All of the variables are defined at the very beginning of main(), and the last one defined is the array. So the array should be the last variable that memory is reserved for and overshooting the upper bound by one element does not cause a problem. |
||
07-09-2003, 07:19 PM | #36 | |
Veteran Member
Join Date: Jan 2001
Location: USA
Posts: 1,072
|
Quote:
Code:
// backwards.cpp // Tests a stack implemented using a doubly linked list. #include <iostream> using std::cin; using std::cout; using std::endl; #include "Stack.h" int main() { char userInput[80], singleChar; int lcv = 0; Stack myStack; cout << "Enter some text: "; cin.getline(userInput, 80, '\n'); // push characters one at a time onto the stack while (userInput[lcv]) myStack.Push(userInput[lcv++]); cout << endl; // pop characters one at a time off of the stack while (singleChar = myStack.Pop()) cout << singleChar; cout << endl; return 0; } Code:
// Stack.h #ifndef STACK_H #define STACK_H #include "Node.h" class Stack { private: Node *ptrFirstNode; Node *ptrLastNode; public: Stack(); ~Stack(); void Push(char letter); char Pop(); Node * GetFirstNode(); void SetFirstNode(Node *pFirst); Node * GetLastNode(); void SetLastNode(Node *pLast); }; #endif Code:
// Stack.cpp #include "Stack.h" Stack::Stack() { SetFirstNode(0); SetLastNode(0); } Stack::~Stack() { char singleChar; while (singleChar = Pop()) ; } void Stack::Push(char letter) { Node *ptrNewest = new Node(letter); if (!GetFirstNode()) { // Pushing first item onto the stack. SetFirstNode(ptrNewest); // There is only 1 item on the stack so it // is both the first and the last item. SetLastNode(ptrNewest); } else { // Pushing item #2 or higher onto the stack. // First, create the back link from the newest // node to its previous one. Node *ptrWasLastNode = GetLastNode(); ptrNewest->SetPrev(ptrWasLastNode); // Assign the Newest Node's pointer to what // was the LastNode's Next pointer. ptrWasLastNode->SetNext(ptrNewest); // Point the LastNode pointer to the Newest Node // making it the last node in the series. SetLastNode(ptrNewest); } } char Stack::Pop() { Node *pLastNode = GetLastNode(); if (!pLastNode) // Stack is empty return 0; // Before deleting LastNode, save its data // for returning char Letter = pLastNode->GetLetter(); // NextToLastNode is needed to reassign LastNode // once what is currently the LastNode is deleted // (since it is being popped off the stack). // If the current LastNode is the first node, // then NextToLastNode will be NULL since the // first node doesn't have a previous Node. Node *ptrNextToLastNode = pLastNode->GetPrev(); // Delete the current LastNode. // Remember, if the first item is being popped, // then ptrNextToLastNode will be NULL. if (!ptrNextToLastNode) { delete pLastNode; SetLastNode(0); SetFirstNode(0); } else { delete pLastNode; // Reassign LastNode since what was the // LastNode is now gone. SetLastNode(ptrNextToLastNode); } return Letter; } void Stack::SetFirstNode(Node * pFirst) { ptrFirstNode = pFirst; } void Stack::SetLastNode(Node * pLast) { ptrLastNode = pLast; } Node * Stack::GetFirstNode() { return ptrFirstNode; } Node * Stack::GetLastNode() { return ptrLastNode; } Code:
// Node.h #ifndef NODE_H #define NODE_H class Node { private: char letter; Node *ptrNext; Node *ptrPrev; public: Node(char Letter); void SetLetter(char Letter); char GetLetter(); Node * GetNext(); void SetNext(Node *pNext); Node * GetPrev(); void SetPrev(Node *pPrev); }; #endif Code:
// Node.cpp #include "Node.h" Node::Node(char Letter) { SetLetter(Letter); SetPrev(0); SetNext(0); } void Node::SetLetter(char Letter) { letter = Letter; } char Node::GetLetter() { return letter; } Node * Node::GetNext() { return ptrNext; } void Node::SetNext(Node *pNext) { ptrNext = pNext; } Node * Node::GetPrev() { return ptrPrev; } void Node::SetPrev(Node *pPrev) { ptrPrev = pPrev; } |
|
07-09-2003, 07:33 PM | #37 |
Veteran Member
Join Date: Jan 2001
Location: Median strip of DC beltway
Posts: 1,888
|
When I say "creating" an algorithm, I mean designing a new method of doing something that has different performance, storage, or other characteristics. Contrast this with "implementing" an algorithm.
While it's not character-for-character the same, it is functionally and mathematically equivalent to practically every other doubly linked list algorithm ever written. Twenty years ago an oct tree for spatial decomposition might have been a new algorithm. Now that every 3d game uses it, the nth reimplementation isn't a new algorithm. |
07-09-2003, 10:13 PM | #38 |
Veteran Member
Join Date: Dec 2000
Location: Tucson, Arizona, USA
Posts: 1,242
|
First responsibility of any programmer is to get the damn thing to work.
|
07-09-2003, 10:56 PM | #39 | |
Contributor
Join Date: Aug 2002
Location: Ohio
Posts: 15,407
|
Jeremy Pallant wrote
Quote:
RBH |
|
07-09-2003, 11:13 PM | #40 | |
Veteran Member
Join Date: Dec 2000
Location: Tucson, Arizona, USA
Posts: 1,242
|
Quote:
Admittedly I do less and less programming these days. I am diversifying into management. |
|
Thread Tools | Search this Thread |
|