Byteorder.net: How's my driving?

Got something to say? I'd love to hear from you, but I'm afraid that if I publish my email address on a web page these days, people will write me lots of mail about how I can "increase my manhood" or purchase inexpensive V1@gra. So instead let's try this. Just put your message in, give your return address, and if you're not trying to sell me questionable goods, I'll do my best answer you. Trust me; it's so simple, even a former Nigerian general with lots of regretably inaccessible cash could do it.

My email address is
 and I'd like to say:

#include <stdio.h>
#include <stdlib.h>

typedef double* matrix;
typedef short coordinate; //Location of some item within a matrix

/* Write 000s to the matrix */
void matrixclear(matrix m, short width, short height) {
	short elements = width * height;
	short count = 0;

	while (count < elements) {
		m[count] = 0;
		count++;
	}
}


/* Matrices must be free'd */
matrix matrixinit(short width, short height) {
	matrix new;
        size_t elements = (size_t) width * height * sizeof(double);
	new=calloc(elements,1);
	matrixclear(new,width,height);
        return(new);
}

//Write identity matrix to m...
void genident(matrix m, short width, short height) {
	matrix tmp = matrixinit(width, height);
	int loopc = 0;
	matrixclear(m, width, height);

	while (loopc < height) {
		m[loopc * width + loopc] = 1;
		loopc++;			
	}
	memmove(m,tmp,width * height * sizeof(double));
	free(tmp);
}

/* Get an absolute location in our matrix type from x,y coordinates */
coordinate ord(short width, short x, short y) {
return (width * (y - 1) + (x - 1));
}

/* Transpose a matrix; that is, the first row becomes the first column, etc... */
void transpose(matrix m, short width, short height) {
	short looph, loopw;
	matrix tmp = matrixinit(width, height);
	for (looph = 0;looph < height;looph++) {
		for (loopw = 0;loopw < width; loopw++) {
			tmp[(looph * height) + loopw] = m[(loopw * height) + looph];	
		}
	}

memmove(m,tmp,height * width * sizeof(double));
free(tmp);

}


/* Add two matrices, (m1, m2), which must be the same size, and square */ 
matrix addmatrix_fixed(matrix m1, matrix m2, short width, short height) {
	matrix result = matrixinit(width, height);
	short index;
	for (index = 0;index < (width * height); index++) {
		result[index] = m1[index] + m2[index];
	}
	return(result);

}

/* Multiply scalar by matrix (m) */
matrix mulscalar(double scalar, matrix m, short width, short height) {
	matrix result = matrixinit(width, height);
	short index;
	for (index = 0; index < (width * height); index++) {
		result[index] = m[index] * scalar;
	}
	return(result);
}

/* Multiply two matrices, which are the same dimensions, and square */
matrix mulmatrix_fixed(matrix m1, matrix m2, short width, short height) {
	double current = 0;
	short index;
	short line = 0;
	short line2 = 0;
	matrix result = matrixinit(width,height);
	transpose(m2, width, height);
	/* Now we can multiply row by row */
	for (line = 0;line < height;line++) {
	for (line2 = 0;line2 < height; line2++) {
		for (index = 0;index < width; index++) {
printf("Adding (%f * %f) to item %d\n",m1[line + index],m2[line2 + index],(line * height + line2));
			current += m1[(line * width) + index] * m2[(line2 * width) + index];	
		}
		result[(line * height) + line2] = current;
		current = 0;
	}
	}
	return(result); 
}

/* Our user interface construct which prints a "width x height" matrix (m) to 
the screen in this form:

1.000000 0.000000 0.000000
0.000000 1.000000 0.000000
0.000000 0.000000 1.000000

*/

void printmatrix(matrix m, short width, short height) {
	short i;
	for (i = 0;i<(width * height) ;i++) {
		printf("%f ",m[i]);
		if((i + 1) % width == 0) printf("\n");
	}
}

/* The menu is messy, hardwired to only do 3x3 matrices, and basically needs ripped
out and replaced with something useful... */


int main (void) { /* There are no arguments, only Zool... */
matrix m1, m2, answer;
float scalar;
short option, height, width;

float n0, n1, n2, n3, n4, n5, n6, n7, n8;

height = 3;
width = 3;

m1 = matrixinit(width,height);
m2 = matrixinit(width,height);

printf("Whoso puteth in floating-point values for this 3x3 matrix, separated by commas,\
 is the true-born king of all Britan!\n");

/* Ugly, ugly, ugly.  Need to fix this. */
scanf("%f,%f,%f,%f,%f,%f,%f,%f,%f",&n0,&n1,&n2,&n3,&n4,&n5,&n6,&n7,&n8);
m1[0] = n0;
m1[1] = n1;
m1[2] = n2;
m1[3] = n3;
m1[4] = n4;
m1[5] = n5;
m1[6] = n6;
m1[7] = n7;
m1[8] = n8;


printf("Thank you, sir, may I have another?\n");

scanf("%f,%f,%f,%f,%f,%f,%f,%f,%f",&n0,&n1,&n2,&n3,&n4,&n5,&n6,&n7,&n8);
m2[0] = n0;
m2[1] = n1;
m2[2] = n2;
m2[3] = n3;
m2[4] = n4;
m2[5] = n5;
m2[6] = n6;
m2[7] = n7;
m2[8] = n8;


printf("A scalar, a scalar; my kingdom for a scalar:\n");

scanf("%f",&scalar);

while (1) {

printf("Matrix 1:\n");
printmatrix(m1,width,height);

printf("Matrix 2:\n");
printmatrix(m2,width,height);

printf("... and a scalar: %lf\n",scalar);

printf("Please enter one of the following: \n");

printf("1. Multiply two matrices\n");
printf("2. Multiply scalar and matrix 1\n");
printf("3. Multiply scalar and matrix 2\n");
printf("4. Transpose matrix 1\n");
printf("5. Transpose matrix 2\n");
printf("6. Add two matrices\n");
printf("7. Exit\n");

scanf("%d",&option);

switch(option) {
	case 1:
        answer = mulmatrix_fixed(m1, m2, width, height);
	printmatrix(answer,width,height); 
	free(answer);
	break;
	case 2:
	answer = mulscalar(scalar,m1,width,height);
        printmatrix(answer,width,height);
        free(answer);
	break;
	case 3:
	answer = mulscalar(scalar, m2, width, height);
        printmatrix(answer,width,height);
        free(answer);
	break;
	case 4:
	transpose(m1, width, height);
	break;
	case 5:
	transpose(m2,width, height);
	break;
	case 6:
	answer = addmatrix_fixed(m1, m2, width, height);
        printmatrix(answer,width,height);
        free(answer);
	break;
	case 7:
	printf("Exiting...\n");
	return(0);
	break;
	default:
	printf("Make another choice.\n");
}

}

free(m1);
free(m2);
free(answer);

return 0;
}