Digital Stronghold




October 7, 2009

Linus’ discussion about goto statements

As discussed by Linus Torvalds 6 years ago,

From: Linus Torvalds
Subject: Re: any chance of 2.6.0-test*?
Date: Sun, 12 Jan 2003 12:22:26 -0800 (PST)

On Sun, 12 Jan 2003, Rob Wilkens wrote:
>
> However, I have always been taught, and have always believed that
> “goto”s are inherently evil. They are the creators of spaghetti code

No, you’ve been brainwashed by CS people who thought that Niklaus Wirth
actually knew what he was talking about. He didn’t. He doesn’t have a
frigging clue.

> (you start reading through the code to understand it (months or years
> after its written), and suddenly you jump to somewhere totally
> unrelated, and then jump somewhere else backwards, and it all gets ugly
> quickly). This makes later debugging of code total hell.

Any if-statement is a goto. As are all structured loops.

And sometimes structure is good. When it’s good, you should use it.

And sometimes structure is _bad_, and gets into the way, and using a
“goto” is just much clearer.

(more…)

September 10, 2009

IBM Signed Numeric Table

If you are dealing with IBM mainframes, you will see signed numbers written this way.

{ = 0  } = -0
A = 1  J = -1
B = 2  K = -2
C = 3  L = -3
D = 4  M = -4
E = 5  N = -5
F = 6  O = -6
G = 7  P = -7
H = 8  Q = -8
I = 9  R = -9

Enjoy!

“Stupidity is doing the same things repeatedly and expecting different results!”

July 28, 2008

Something for Solaris SPARC

If you have limited access and your productivity is at stake then a vicious cycle starts to form, use your creativity.

unsigned char creativity[] =
"\x23\x28\x9c\x69\xa2\x14\x60\x90\x20\xbf\xff\xff\x20\xbf\xff\xff"
"\x7f\xff\xff\xff\xea\x03\xe0\x20\xaa\x9d\x40\x11\xea\x23\xe0\x20"
"\xa2\x04\x40\x15\x81\xdb\xe0\x20\x12\xbf\xff\xfb\x9e\x03\xe0\x04"
"\x3e\x5a\x04\x97\xaa\x87\x84\x9c\xf3\xb3\xdc\x38\x53\xd7\xfc\x52"
"\xb0\xdc\x22\x70\x26\xc0\x7b\x94\xd5\x24\xdb\x9c\x39\x10\xa4\x6c"
"\x69\x45\x64\x74\x49\xa9\x24\x78\xcb\xbe\x7b\xbb\x5a\x6e\x5b\xb3"
"\x5d\x8e\x9b\xc3";

Annihilate with passion.

October 7, 2006

Binary Trees

A tree has a finite set of elements called nodes. It has a unique node called the root node, where the remaining nodes are a disjoint collection of subtrees. A binary tree is a tree whose elements have two children at maximum. It is considered as a data structure composed of elements that are characterized by two link fields, left and right children. A leaf node contains 0 children meaning both children point to a NULL value.

A binary search tree is a special type of a binary tree. These terms are sometimes used interchangeably in some articles, so do not be confused. For this purpose, I’ll limit my article to binary trees in general.

First let’s define the structure to be used.

#include <stdio.h>
#include <stdlib.h>
	
typedef char DATA;
	
struct node {
        DATA d;
        struct node *left;
        struct node *right;
};
	
typedef struct node NODE;
typedef struct NODE *BINTREE;

[Creating a Binary Tree]

Dynamic allocation.

BINTREE new_node()
{
        return (malloc(sizeof(NODE)));
}

Of course we have to initialize the node.

BINTREE init_node(DATA d1, BINTREE p1, BINTREE p2)
{
        BINTREE t;
	
        t = new_node();
        t -> d = d1;
        t -> left = p1;
        t -> right = p2;
        return t;
}

We will generate a tree from an array recursively.

BINTREE create_tree(DATA a[], int i, int size)
{
        if (i >= size)
                return NULL;
        else
                return (init_node(a[i],
                        create_tree(a, 2 * i + 1, size),
                        create_tree(a, 2 * i + 2, size)));
}

[Binary Tree Traversals]

There are several ways to traverse in a binary tree. Luckily, I have leared something during our CS 213 Data Structures class. Thanks to Sir Greg. Here are three ways presented:

Inorder Traversal: Left Node Right (LNR)
Preorder Traversal: Node Left Right (NLR)
Postorder Traversal: Left Right Node (LRN)

The standard way of implementing this of course is again by recursion.

Inorder Traversal

void inorder(BINTREE root)
{
        if (root != NULL) {
                inorder(root -> left);
                printf("%c ", root -> d);
                inorder(root -> right);
        }
}

Preorder Traversal

void preorder(BINTREE root)
{
        if (root != NULL) {
                printf("%c ", root -> d);
                preorder(root -> left);
                preorder(root -> right);
        }
}

Postorder Traversal

void postorder(BINTREE root)
{
        if (root != NULL) {
                postorder(root -> left);
                postorder(root -> right);
                printf("%c ", root -> d);
        }
}

Hope this article will be useful to students taking up Data Structures and Algorithms.

September 14, 2006

Bubble sort in C

I’m writing this for someone special. Hope this would help. This is at the same time a review of my past lessons. Assuming i,j and tmp are defined as integers, num[] as an array of integers, and SIZE as a constant.

for (i = 0; i < SIZE - 1; ++i)
        for (j = SIZE - 1; j < i; --j)
                if (num[j-1] > num[j]) {
                        tmp = num[j-1];
                        num[j-1] = num[j];
                        num[j] = tmp;
                }

Here is another way of doing it. This is a demo on how the function works on a particular array of integers. a[] is the array of integers while n is the size of the array.

void swap(int *, int *);
	
void bubble(int a[], int n)
{
        int i, j;
	
        for (i = 0; i < n - 1; ++i)
                for (j = n - 1; j > i; --j)
                        if (a[j-1] > a[j])
                                swap(&a[j-1], &a[j]);
}
	
void swap(int *p, int *q)
{
        int tmp;
	
        tmp = *p;
        *p = *q;
        *q = tmp;
}

If you are keen enough to observe, check that we can use the swap arguments presented below.

swap(a + i, a + j);

Getting the idea? Pointer arithmetic! God bless and good day.

September 5, 2006

Coding therapy

This is what I usually do whenever I am tired and hungry.

#include <GL/glut.h>
	
void disp(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3d(0.43, 0.12, 1.00);
	
	glBegin(GL_POLYGON);
		glVertex3f(-0.75, -0.75, 0.0);
		glVertex3f( 0.75, -0.75, 0.0);
		glVertex3f( 0.75,  0.75, 0.0);
		glVertex3f(-0.75,  0.75, 0.0);
	glEnd();
	glFlush();
}
	
void init(void)
{
	glClearColor(0.0, 0.0, 0.0, 0.0);
}
	
void main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(300,300);
	glutInitWindowPosition(0, 0);
	glutCreateWindow(argv[0]);
	
	init();
	
	glutDisplayFunc(disp);
	glutMainLoop();
}

August 17, 2006

Bit masking

I was thinking of the hex to bin converter in the previous post. The fun part there was the extraction of the desired bit. With the knowledge in mind, it resulted me to devise a function in C that uses a mask to print out the bit representation of an integer.

void bitprint(int a)
{
        int i;
        int n = sizeof(int) * CHAR_BIT;
	int mask = 1 << (n - 1);
	
	for (i = 1; i <= n; ++i) {
		putchar(((a & mask) == 0) ? '0' : '1');
		a <<= 1;
		if (i % CHAR_BIT == 0 && i < n)
			putchar(' ');
	}
}

CHAR_BIT is defined in limits.h and holding a value of 8. Try it, it’s fun. I’m bored, just extending my thanks to beandog for enlisting me in Planet Larry: Gentoo Users’ Blogs.

Theme designed by Joset Anthony Zamora


Digital Stronghold

↑ Get Headline Animator