problems with the multiplication of the matrices. help! please. thanks!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int inputexponent(){
//INPUTS THE EXPONENT
char exp[100];
// fgets(exp, sizeof(exp), stdin);
int exponent1;
// exponent1 = atoi(exp);
int extra;
while(exp[extra] != '\n'){
exp[extra] = getchar();
extra = extra + 1;
if(exp[extra] = ' '){
return 0;
}
}
return exponent1;
}
int countelements(char inputmatrix2[]){
//COUNT NUMBER OF ELEMENTS
int x = 0;
int counter = 1;
while(inputmatrix2[x] != '\n'){
if (inputmatrix2[x] != ' '){
switch(inputmatrix2[x-1]){
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.': break;
case ' ': counter = counter + 1; break;
}
}
x = x + 1;
}
return counter;
}
int inputmatrixpart2(double **matrix2, int cols2, int numberofelements1){
//INPUT 2ND TO LAST ROW OF MATRIX
char inputmatrix2[500];
int e = 1;
int f = 1;
int g = 0;
int numelements = 0;
int checkelements;
char separator1[] = " ";
char *result1 = NULL;
while(e != numberofelements1){
fgets(inputmatrix2, sizeof(inputmatrix2), stdin);
numelements = countelements(inputmatrix2); //CHECKS IF MATRIX IS SQUARE
if(numelements != cols2){
return 0;
}
checkelements = elementchecker(inputmatrix2); //CHECKS IF INPUT IS VALID(NUMBERS ONLY)
if(checkelements == 0){
return 0;
}
result1 = strtok(inputmatrix2, separator1);
while(result1 != NULL){
matrix2[f][g] = atof(result1);
g = g + 1;
result1 = strtok(NULL, separator1);
}
g = 0;
f = f + 1;
e = e + 1;
}
return 1;
}
int elementchecker(char inputmatrix3[]){
int g = 0;
while(inputmatrix3[g] != '\n'){
if((inputmatrix3[g] >= 'a')&&(inputmatrix3[g] <= 'z')){
return 0;
}else if ((inputmatrix3[g] >= 'A')&&(inputmatrix3[g] <= 'Z')){
return 0;
}
g = g + 1;
}
return 1;
}
void printmatrix(double **matrix1, int cols1, int rows1){
// PRINTS THE WHOLE MATRIX
printf("= \n");
int c = 0;
int d = 0;
while(c < rows1){
while(d < cols1){
printf("%0.2lf ", matrix1[c][d]);
d = d + 1;
}
printf("\n");
d = 0;
c = c + 1;
}
}
int main(){
//GETS THE FIRST ROW OF ELEMENTS
char inputmatrix[500];
fgets(inputmatrix, sizeof(inputmatrix), stdin);
int numberofelements;
numberofelements = countelements(inputmatrix); //COUNTS THE NUMBER OF ELEMENTS IN THE FIRST ROW
// -----------------------------------------------------------------------------------
//INITIALIZE INPUT MATRIX
int cols = numberofelements;
int rows = numberofelements;
double **matrix;
matrix = (double **)malloc(rows * sizeof(double*));
int a = 0;
for(; a < rows; a++){
matrix[a] = (double *)malloc(cols * sizeof(double));
}
//INITIALIZE EXTRA MATRIX
double **extra;
extra = (double **)malloc(rows * sizeof(double*));
int m = 0;
for(; m < rows; m++){
extra[m] = (double *)malloc(cols * sizeof(double));
}
//INITIALIZE PRODUCT MATRIX
double **product;
product = (double **)malloc(rows * sizeof(double*));
int n = 0;
for(; n < rows; n++){
product[n] = (double *)malloc(cols * sizeof(double));
}
// -----------------------------------------------------------------------------------
//INPUT ELEMENTS INTO MATRIX (1ST ROW ONLY)
char separator[] = " ";
char *result = NULL;
result = strtok(inputmatrix, separator);
int b = 0;
while(result != NULL ) {
matrix[0][b] = atof(result);
b = b + 1;
result = strtok(NULL, separator);
}
// -----------------------------------------------------------------------------------
//INPUT ELEMENTS INTO MATRIX (2ND TO LAST ROW)
int terminator;
terminator = inputmatrixpart2(matrix, cols, numberofelements);
if(terminator == 0){ // CHECKS IF THE INPUT IS A SQUARE MATRIX, IF NOT, TERMINATE
return 0;
}
// -----------------------------------------------------------------------------------
int exponent;
exponent = inputexponent(); //GET EXPONENT
if(exponent == 0){
return 0;
}
//EXTRA MATRIX GETS(COPIES) CONTENTS OF INPUT MATRIX
int o = 0;
int p = 0;
for(;o < rows; o++){
for(;p < cols; p++){
extra[o][p] = matrix[o][p];
}
p = 0;
}
// -----------------------------------------------------------------------------------
//EXPONENTIATION PROCESS (ERROR!!!)
int z = 0;
int q = 0;
int r = 0;
int s = 0;
int t = 0;
int w = 0;
int x = 0;
while(z <= exponent){
for(;q < rows; q++){
while(t < cols){
for(;r < cols; r++){
product[q][r] = ((product[q][r]) + ((matrix[q][s]) * (extra[t][r])));
}
r = 0;
s = s + 1;
t = t + 1;
}
t = 0;
s = 0;
}
z = z + 1;
q = 0;
w = 0;
x = 0;
for(;w < rows; w++){
for(;x < cols; x++){
extra[w][x] = product[w][x];
}
x = 0;
}
}
printmatrix(product, cols, rows); //PRINT PRODUCT MATRIX
// -----------------------------------------------------------------------------------
//FREE THE MEMORY ALLOCATION
int h;
for(h = rows-1; h >= 0; h--){
free(matrix[h]);
}
free(matrix);
int i;
for(i = rows-1; i >= 0; i--){
free(extra[i]);
}
free(extra);
int j;
for(j = rows-1; j >= 0; j--){
free(product[j]);
}
free(product);
// -----------------------------------------------------------------------------------
printf("\n");
return 0; // END
}