Questions regarding arrays and matrices
Page 1 of 112 Replies  654 Views  Last Post: 26 March 2012  03:19 PM
#1
Questions regarding arrays and matrices
Posted 25 March 2012  12:19 PM
My second question is whether I can multiply twodimensional array (matrix), with a onedimensional array in C? If yes, will it will be a simple straightforward code?
Thanks in advance.
Replies To: Questions regarding arrays and matrices
#2
Re: Questions regarding arrays and matrices
Posted 25 March 2012  12:24 PM
If your values are in a file then you could use that to populate your matrix.
#3
Re: Questions regarding arrays and matrices
Posted 25 March 2012  12:24 PM
The answer to the second question is yes, depending on what you consider "straightforward". If you understand matrix multiplication and you understand C, it's straightforward.
#4
Re: Questions regarding arrays and matrices
Posted 26 March 2012  03:11 AM
About the second question, I want to multiply a twodimensional array with a onedimensional array, and store the values in another onedimensional array. Like in math, if you multiply a matrix 64x64 with another matrix of size 64x1, you will receive another matrix with the size 64x1. This is what I am asking:
int matrix[64][64], array[64], result[64] = {0}; int i, j; // say that values for the matrix and array are already given... for(i = 0; i < 64; i++) for(j = 0; j < 64; j++) { result[j] += matrix[i][j] * array[j]; }
This code seems stupid to me, therefore I asked whether a onedimensional array can be multiplied with twodimensional array. If this won't work, I guess changing the onedimensional arrays to the twodimensional ones will solve the problem.
This post has been edited by erkant: 26 March 2012  03:54 AM
#5
Re: Questions regarding arrays and matrices
Posted 26 March 2012  08:45 AM
int matrix[64][64], array[64], result[64] = {0}; int i, j; // say that values for the matrix and array are already given... for(i = 0; i < 64; i++) for(j = 0; j < 64; j++) { result[i] += matrix[i][j] * array[j]; }
#6
Re: Questions regarding arrays and matrices
Posted 26 March 2012  11:29 AM
#8
Re: Questions regarding arrays and matrices
Posted 26 March 2012  12:15 PM
int i, j; FILE * valuesFile; valuesFile = fopen("velues.txt","r"); for(i = 0; i < 64; i++) for(j = 0; j < 64; j++) fscanf(valuesFile, "%d");
#10
Re: Questions regarding arrays and matrices
Posted 26 March 2012  01:47 PM
for(i = 0; i < 64; i++) for(j = 0; j < 64; j++) fscanf(valuesFile, "%d", &matrix[i][j]);
#11
Re: Questions regarding arrays and matrices
Posted 26 March 2012  02:17 PM
#12
Re: Questions regarding arrays and matrices
Posted 26 March 2012  02:35 PM
I have this:
int i, j; FILE *valuesFile; valuesFile = fopen("values.txt","r"); if(valuesFile == NULL) { fprintf(stderr, "Can't open the file values.txt!\n"); exit(1); } for(i = 0; i < 64; i++) for(j = 0; j < 64; j++) { if(fscanf(valuesFile, "%d", &matrix[i][j]) != EOF) fscanf(valuesFile, "%d", &matrix[i][j]; }
This post has been edited by erkant: 26 March 2012  02:36 PM
#13
Re: Questions regarding arrays and matrices
Posted 26 March 2012  03:19 PM
for(i = 0; i < 64; i++) for(j = 0; j < 64; j++) { if(fscanf(valuesFile, "%d", &matrix[i][j]) != EOF) // reading here fscanf(valuesFile, "%d", &matrix[i][j]; // and again here }
In this situation if you want to discontinue the input operation you have two nested loops to break out of. Also, in that situation you need some way to signal that your matrix is invalid, so you need to save the return value of fscanf in a variable that you can refer to later. You could do this:
int flag = 0; for(i = 0; i < 64; i++) { for(j = 0; j < 64; j++) { if( (flag = fscanf(valuesFile, "%d", &matrix[i][j])) == EOF) { break; // this breaks out of the inner loop } } if( flag == EOF ) { break; // this breaks out of the outer loop } }
Now after the input is finished you can test the value of flag. If flag == EOF you know the input operation failed somewhere along the way and you can have code to deal with that situation.
