I have several questions about arrays and matrices. First of all I have a matrix composed of complex numbers with the size of 64x64. I have all the values, and I need to assign those values to the matrix. I want to ask whether there is any shortcut method for entering my already chosen value to that matrix, instead of just typing them one by one in my source code, which will be a huge code.

My second question is whether I can multiply two-dimensional array (matrix), with a one-dimensional array in C? If yes, will it will be a simple straightforward code?

Thanks in advance.

# Questions regarding arrays and matrices

Page 1 of 1## 12 Replies - 840 Views - Last Post: 26 March 2012 - 03:19 PM

##
**Replies To:** Questions regarding arrays and matrices

### #2

## Re: Questions regarding arrays and matrices

Posted 25 March 2012 - 12:24 PM

If two matrices can be multiplied then they can be multiplied using C.

If your values are in a file then you could use that to populate your matrix.

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

What does that mean, you "have all the values"? You have them in what form? If you have them in a file, you can write a program that reads the values from the file and loads the array (matrix).

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.

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

What I mean with "have all the values" is that I have made the needed mathematical calculations, and I have the results. So I guess if I write all the results in a simple .txt file, with spaces between each row element, and new line for each new row, I can give those values to the matrix. I know how to read and write, simply use files in C++, but as long as I know it is a bit different in C. If someone can help me with assigning the values from a .txt file to a matrix, I would be glad.

About the second question, I want to multiply a two-dimensional array with a one-dimensional array, and store the values in another one-dimensional 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:

This code seems stupid to me, therefore I asked whether a one-dimensional array can be multiplied with two-dimensional array. If this won't work, I guess changing the one-dimensional arrays to the two-dimensional ones will solve the problem.

About the second question, I want to multiply a two-dimensional array with a one-dimensional array, and store the values in another one-dimensional 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 one-dimensional array can be multiplied with two-dimensional array. If this won't work, I guess changing the one-dimensional arrays to the two-dimensional 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

Why does it seem stupid? It should be no different than you would do on paper. Just one correction (result[i]):

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

Thank you very much r.stiltskin. And any help or reference about passing values from a .txt file to a matrix in C?

### #8

## Re: Questions regarding arrays and matrices

Posted 26 March 2012 - 12:15 PM

Will this one be okay?

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

Thanks! So I guess I will need to write it like that.

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

You should check if the file opened correctly before attempting to get input from it. You should also check the return value of fscanf against EOF so you don't continue calling fscanf once you hit the EOF.

### #12

## Re: Questions regarding arrays and matrices

Posted 26 March 2012 - 02:35 PM

Thanks for the information vividexstance. I managed to write code for checking whether the file is opened correctly, but I don't know how to write the code about EOF.

I have this:

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

If you do this you'll be reading two values from the file on each iteration, storing the first one in matrix[i][j] and then overwriting it with the second value in the same array element

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:

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.

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.

Page 1 of 1