10 Replies - 5427 Views - Last Post: 12 February 2011 - 12:36 AM Rate Topic: -----

#1 pricokj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 05-September 09

Matrix Multiplication

Posted 11 February 2011 - 01:29 PM

I have to perform matrix multiplication reading input from a text file. I already know how to ask for the file and print the whole file, but I don't know how to pull the numbers out of the file. The matrix is to be a square matrix. In the text file the format is size
Matrix A's values
Matrix B's values

For example, in tester.txt I may have:
2
1.0 2.0
2.0 0.0
0.9 1.1
2.3 -1.5
This creates two 2x2 matrices with the values above. How do I get these values so I can build the matrix and perform addition or multiplication, etc. I think I have to use malloc, but i'm not too familiar with this.
What I have right now is:

 
#include <stdio.h>

int main (void) {
   char filename[] = "";
   printf("Please enter the input file: ");
   scanf("%s", &filename);

   FILE *file = fopen(filename, "r");
   if (file != NULL) {
       char line [128];
       while (fgets(line, sizeof line, file) != NULL) {
          fputs(line, stdout);
        }
        fclose(file);
      }
   else
     {
         perror(filename);
     }
   return 0;
 }



Is This A Good Question/Topic? 0
  • +

Replies To: Matrix Multiplication

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: Matrix Multiplication

Posted 11 February 2011 - 01:47 PM

Use fscanf to pull the numbers into your program.
Was This Post Helpful? 1
  • +
  • -

#3 pricokj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 05-September 09

Re: Matrix Multiplication

Posted 11 February 2011 - 04:30 PM

I got this partially working, I just can't get the multiplying part right. Any help would greatly appreciated.
#include <stdio.h>

typedef struct {
   int n;
   double *entry; }
matrix;

FILE *file;
char filename[]="";
int n,i,j,k;
matrix *a;
matrix *b;
matrix *c;


int main (void) {
   
   printf("Please enter the input file: ");
   scanf("%s", &filename);

   file = fopen(filename, "r");
   if (file != NULL) {
       }
   else
    {
       printf("Error: File Not Found");
    }

fscanf(file,"%d", &n);

a=(matrix *)malloc(sizeof(matrix));
b=(matrix *)malloc(sizeof(matrix));
c=(matrix *)malloc(sizeof(matrix));

a->entry=(double *)malloc(sizeof(double)*n*n);
b->entry=(double *)malloc(sizeof(double)*n*n);
c->entry=(double *)malloc(sizeof(double)*n*n);

buildMatrix(n);
}

void buildMatrix(n) {
printf("\nMatrix A includes: \n");
for (i=0;i<n;i++) {
   for (j=0;j<n;j++) {
       fscanf(file,"%lf\t",&a->entry[i*n+j]);
       if (j<n-1)
             printf("%.2lf\t", a->entry[i*n+j]);
       else
             printf("%.2lf\n", a->entry[i*n+j]);
     }
   }
printf("\nMatrix B includes: \n");
for (i=0;i<n;i++) {
    for (j=0;j<n;j++) {
         fscanf(file,"%lf\t", &b->entry[i*n+j]);
         if (j<n-1)
            printf("%.2lf\t", b->entry[i*n+j]);
         else
            printf("%.2lf\n", b->entry[i*n+j]);
     }
   }
 }

void matrixMultiply(n) {
for(i=0;i<n;i++) {
   for(j=0;j<n;j++) {
        float sum=0.0;
        for(k=0;k<n;k++)
         {
           sum=sum+(a[i][k]*b[k][j]);  //Here's the error line
           c[i][j]=sum;
         }
    printf ("%.2lf\t",c[i][j]);
      }
   }
}



I keep getting the error that subscripted value is neither array nor pointer for line 71. How do I fix this?
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6109
  • View blog
  • Posts: 23,666
  • Joined: 23-August 08

Re: Matrix Multiplication

Posted 11 February 2011 - 05:10 PM

Do NOT cast the return value of malloc. That you are doing so is hiding the fact that you're missing the <stdlib.h> include required to USE malloc.

If you're getting errors, COPY AND PASTE THEM EXACTLY AS THEY APPEAR.

Sorry, missed the last line, but the error tells the story. Neither a nor b are arrays; they're pointers to global matrix structs.

This post has been edited by JackOfAllTrades: 11 February 2011 - 05:12 PM

Was This Post Helpful? 0
  • +
  • -

#5 pricokj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 05-September 09

Re: Matrix Multiplication

Posted 11 February 2011 - 05:16 PM

ok so I add that library to the top and then how do I rephrase so I'm not casting. Just one example and I can fix the rest.... Is the multipyMatrix function fine though?
Was This Post Helpful? 0
  • +
  • -

#6 pricokj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 05-September 09

Re: Matrix Multiplication

Posted 11 February 2011 - 05:59 PM

I have this code, but I can't get it to multiply two matrices from a text file. The error says subscripted value is neither array nor pointer. I think the problem lies in either my buildMatrix() or multiplyMatrix() functions.

#include <stdio.h>

/* Creates structure */
typedef struct {
   int n;
   double *entry; }
matrix;

/*Initializes matrix pointers, size, and the loop variables (i,j,k). Also initilizes character array. */
FILE *file;
char filename[]="";
int n,i,j,k;
matrix *a;
matrix *b;
matrix *c;


int main (void) {
   
   printf("Please enter the input file: "); /* Asks user for the name of the file that holds their matrix info */
   scanf("%s", &filename); /*Scans user's string and saves it in filename */

   file = fopen(filename, "r"); /* Opens the file the user input */
   if (file != NULL) { /*Checks if the file can be opened. */
       }
   else
    {
       printf("Error: File Not Found"); /* If file is not found, error message shows. */
    }

fscanf(file,"%d", &n); /* Scans file for integer and stores this integer in n */

/* Allocates space for matrices (a, b, and result in c) */
a=(matrix *)malloc(sizeof(matrix));
b=(matrix *)malloc(sizeof(matrix));
c=(matrix *)malloc(sizeof(matrix));

a =(double *)malloc(sizeof(double)*n*n);
b =(double *)malloc(sizeof(double)*n*n);
c =(double *)malloc(sizeof(double)*n*n);

buildMatrix(n);
}

/*Places values into matrix a and b from text file */
void buildMatrix(n) {
printf("\nMatrix A includes: \n");
for (i=0;i<n;i++) {
    fscanf(file,"%lf\t", a[i]+j);
   }

printf("\nMatrix B includes: \n");
for (j=0;j<n;j++) {
    fscanf(file,"%lf\t", b+j);
   }
 }
void matrixMultiply(n) {
for(i=0;i<n;i++) {
   for(j=0;j<n;j++) {
        float sum=0.0;
        for(k=0;k<n;k++)
         {
           sum=sum+(a[i][k]*b[k][j]);
           c[i][j]=sum;
         }
    printf ("%.2lf\t",c[i][j]);
      }
   }
}



Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6109
  • View blog
  • Posts: 23,666
  • Joined: 23-August 08

Re: Matrix Multiplication

Posted 11 February 2011 - 06:46 PM

Merged duplicate topics. Do not create a new topic when you already have one on the same subject!
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: Matrix Multiplication

Posted 11 February 2011 - 08:16 PM

// no casting here:
// a=(matrix *)malloc(sizeof(matrix));
a = malloc(sizeof(matrix));

// congratulations, you just overwrote a
a = malloc(sizeof(double)*n*n);

// I never see n assigned, do you?



Don't be afraid of functions, they're your friends. Be afraid of globals and don't use them.

e.g.
/* Define a structure */
typedef struct {
	int n, m;
	double *entry;
} matrix;

/* globals bad 
FILE *file;
char filename[]=""; // what the hell does this mean
int n,i,j,k;
matrix *a;
matrix *b;
matrix *c;
*/

matrix *matrixCreate(int n, int m) {
	matrix *mat = malloc(sizeof(matrix));
	mat->n = n;
	mat->m = m;
	mat->entry = malloc(sizeof(double)*n*m);
	return mat;
}

void matrixSet(matrix *mat, int n, int m, double value) {
	/* your code here */
}

double matrixGet(matrix *mat, int n, int m) {
	/* your code here */
}

void matrixRead(matrix *mat, FILE *file) {
	int i, j;
	for (i=0; i<mat->n; i++) {
		for (j=0; j<mat->m; j++) {
			double value;
			fscanf(file,"%lf\t", value);
			matrixSet(mat, i, j, value);
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#9 pricokj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 05-September 09

Re: Matrix Multiplication

Posted 11 February 2011 - 08:52 PM

View Postbaavgai, on 11 February 2011 - 08:16 PM, said:

// no casting here:
// a=(matrix *)malloc(sizeof(matrix));
a = malloc(sizeof(matrix));

// congratulations, you just overwrote a
a = malloc(sizeof(double)*n*n);

// I never see n assigned, do you?



Don't be afraid of functions, they're your friends. Be afraid of globals and don't use them.

e.g.
/* Define a structure */
typedef struct {
	int n, m;
	double *entry;
} matrix;

/* globals bad 
FILE *file;
char filename[]=""; // what the hell does this mean
int n,i,j,k;
matrix *a;
matrix *b;
matrix *c;
*/

matrix *matrixCreate(int n, int m) {
	matrix *mat = malloc(sizeof(matrix));
	mat->n = n;
	mat->m = m;
	mat->entry = malloc(sizeof(double)*n*m);
	return mat;
}

void matrixSet(matrix *mat, int n, int m, double value) {
	/* your code here */
}

double matrixGet(matrix *mat, int n, int m) {
	/* your code here */
}

void matrixRead(matrix *mat, FILE *file) {
	int i, j;
	for (i=0; i<mat->n; i++) {
		for (j=0; j<mat->m; j++) {
			double value;
			fscanf(file,"%lf\t", value);
			matrixSet(mat, i, j, value);
		}
	}
}



Actually the variable n comes from the text file that is read. Based on what the first line is that's the number of rows and columns to be used.
Was This Post Helpful? 0
  • +
  • -

#10 pricokj  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 05-September 09

Re: Matrix Multiplication

Posted 11 February 2011 - 09:10 PM

Data prints out how I have it right now. I just am not able to perform multiplication using the values from my text file.
Was This Post Helpful? 0
  • +
  • -

#11 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1770
  • View blog
  • Posts: 3,429
  • Joined: 30-May 10

Re: Matrix Multiplication

Posted 12 February 2011 - 12:36 AM

> Merged duplicate topics. Do not create a new topic when you already have one on the same subject!
It's a shame you couldn't have merged this thread as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1