Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum porta, diam at accumsan mollis, purus felis varius nibh, at bibendum nulla nisl eu libero. Aliquam erat volutpat. Quisque orci felis, pharetra vel, feugiat ut, eleifend vitae, sem. Nam adipiscing blandit tortor. Nulla dignissim tincidunt leo. Nulla scelerisque fermentum ipsum. Nunc at metus. In ac pede. Donec elit libero, vestibulum id, malesuada sed, cursus vitae, ligula. Praesent consequat, urna ut laoreet convallis, libero velit convallis quam, rhoncus cursus orci ipsum id mi. Quisque malesuada semper nunc. Nulla porttitor justo ut lacus. Fusce blandit tellus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Maecenas faucibus, quam at tristique facilisis, metus magna interdum arcu, vitae lobortis dui mauris ac risus.
Fusce quam. Aenean bibendum adipiscing metus. Etiam in felis. Vivamus sit amet erat eget velit bibendum sollicitudin. Donec molestie purus ac nibh. Ut dignissim, ligula id rhoncus consectetur, leo turpis dapibus tortor, nec hendrerit turpis velit a neque. Cras eu metus. Nullam sodales, tortor a molestie posuere, pede eros dictum lectus, nec sollicitudin lorem diam et velit. Maecenas dictum neque eget purus. Curabitur ac justo et odio congue euismod. Duis vitae felis ut nunc ultrices adipiscing. Vestibulum at tellus. Nunc in purus eget libero commodo posuere.
Coming soon: A Whole New Set of Tubes.
#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;
}