Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for Matrix Arithmetic in C (some bugs may occur)

#include [stdio.h]
#include [stdlib.h]
#include [string.h]


int main() {
    //create file variables for I/O
    FILE *data = fopen("COSC450_P1_Data.txt", "r");
FILE *output = fopen("COSC450_P1_Output.txt", "w+");

//create variables
signed int input[255];
int length = 0;
int value;
int neg;
int c = 0, d = 0, d1 = 0, m;
int add;

//Check if file exist
if(data == NULL){
   printf("Error reading input file");
   exit(1);
}else{
   //read one integer at a time
   fscanf(data, "%d", &value);
   //while not end of file
   while(!feof (data)){
       //store value in array and continue reading
       input[length] = value;
       length++;
       fscanf(data, "%d", &value);
   }
   fscanf(data, "%d", &value);
   input[length] = value;
 
   fclose(data);
 
   //Create matrix
   int field = length / 5;
   int x[5][field];
   int y[field][5];
 
   long result[5][5];
 
   //loop for one dimension of matrix
   for(int a = 0; a <= field; a++){
       //loop for second dimension of matrix
       for(int b = 0; b < 5; b++){
           neg = 0;
         
              //assign array element to index
              if(input[c] < 0){
                  value = -input[c];
                  neg = 1;
              }else{
                  value = input[c];
              }
              x[b][a] = value;
              //print to file
              fprintf(output, "matrix1[%d][%d] = %d\n", b, a, input[c]);
              //increase index and repeat for second matrix
           
              y[a][b] = value;
              fprintf(output, "matrix2[%d][%d] = %d\n", a, b, input[c]);
              c++;
           
           
           
       }
     
   }
 
   for(int a = 0; a < 5; a++){
       //loop for second dimension of matrix
       for(int b = 0; b < 5; b++){
        for(int c = 0; c <= field; c++){
       //loop for second dimension of matrix
        //create product matrix
              add = add + x[a][c] * y[c][b];
        }
        result[a][b] = add;
        add = 0;
        fprintf(output, "product matrix[%d][%d] = %ld \n\n", a, b,  result[a][b]);
       }
   }
 
   //loop to sort value of product matrix
   for(int e = 0; e < 5; e++){
       for(int m = 0; m < 5; m++){
         
       //assign value to test against array elements
       value = result[e][m];
     
       //secondary loop
       for(int d = e; d < 5; d++ ){
        for(int d1 = m; d1 < 5; d1++){
        //if value is greater begin swap
        if(value > result[d][d1] ){
        value = result[d][d1];
            result[d][d1] = result[e][m];
            result[e][m] = value;
           
           
        }
        //second conditional for column shift
        if(d1 == 0){
        if(result[d][d1] < result[d-1][4]){
        neg = result[d][d1];
        result[d][d1] = result[d-1][4];
        result[d-1][4] = neg;
       
        }
        }
       
        }
       }
       
       }
     
   }
 
 
   //loop to print to file
   for(int f =0; f < 5; f++){
       for(int n = 0; n < 5; n++){
       fprintf(output, "product matrix[%d][%d] = %ld \n", f, n, result[f][n]);
       }
   }
   fclose(output);
 
 
 
 
}

return 0;
}


No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!