Manipulating matrix operations (transpose, negation, addition, and mutiplication) using functions in
Posted
by user292489
on Stack Overflow
See other posts from Stack Overflow
or by user292489
Published on 2010-05-09T21:34:18Z
Indexed on
2010/05/09
22:18 UTC
Read the original article
Hit count: 537
I was trying to manipulate matrices in my input file using functions. My input file is:
A 3 3
1 2 3
4 5 6
7 8 9
B 3 3
1 0 0
0 1 0
0 0 1
C 2 3
3 5 8
-1 -2 -3
D 3 5
0 0 0 1 0
1 0 1 0 1
0 1 0 0 1
E 1 1
10
F 3 10
1 0 2 0 3 0 4 0 5 0
0 2 3 -1 -3 -4 -3 8 3 7
0 0 0 4 6 5 8 2 -1 10
I am having trouble in implementing the functions that I declared. I assumed my program will perform those operations: transpose, negate, add, and multiply matices according to the users choice:
/* once this program is compiled and executed, it will perform the basic matrix
* operations: negation, transpose, addition, and multiplication. */
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int readmatrix(FILE *input, char martixname[6],int , mat[10][10], int i, int j);
void printmatrix(char matrixname[6], int mat[10][10], int i, int j);
void Negate(char matrixname[6], int mat[10][10], int i, int j);
void add(char matrixname[6], int mat[10][10],int i, int k);
void multiply(char matrixname[], int mat[][10], char A[], int i, int k);
void transpose (char matrixname[], int mat[][10], char A[], int);
void printT(int mat[][10], int);
int selctoption();
char selectmatrix();
int main(int argc, char *argv[])
{ char matrixtype[6];
int mat[][10];
FILE *filein;
int size;
int optionop;
int matrixop;
int option;
if (argc != 2)
{ printf("Usage: executable input.\n");
exit(0);
}
filein = fopen(argv[1], "r");
if (!filein)
{ printf("ERROR: input file not found.\n");
exit (0);
}
size = readmatrix (filein, matrixtype);
printmatrix(matrix[][10], size);
option = selectoption();
matrixtype = selectmatrix();
//printf("You have: %5.2f ", deposit);
optionop = readmatrix(option, matrix[][10], size);
if (choiceop == 6)
{ printf("Thanks for using the matrix operation program.\n");
exit(0);
}
printf("Please select from the following matrix operations:\n")
printf("\t1. Print matrix\n");
printf("\t2. Negate matrix\n");
printf("\t3. Transpose matrix\n");
printf("\t4. Add matrices\n");
printf("\t5. Multiply matrices\n");
printf("\t6. Quit\n");
fclose(filein);
return 0;
}
do
{ printf("Please select option(1-%d):", optionop);
scanf("%d", &matrixop);
} while(matrixop <= 0 || matrixop > optionop);
void readmatrix (FILE *in, int mat[][10], char A[], int i, int j)
{ int i=0,j = 0;
while (fscanf(in, "%d", &mat[i][j]) != EOF)
return 0;
}
// I would appreciate anyone's feedback.
© Stack Overflow or respective owner