12 Replies - 516 Views - Last Post: 26 March 2012 - 03:19 PM Rate Topic: -----

#1 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

Questions regarding arrays and matrices

Posted 25 March 2012 - 12:19 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Questions regarding arrays and matrices

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,141
  • Joined: 08-August 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

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.
Was This Post Helpful? 0
  • +
  • -

#4 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

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:

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

Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

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];
   }


Was This Post Helpful? 1
  • +
  • -

#6 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

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?
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,637
  • Joined: 16-October 07

Re: Questions regarding arrays and matrices

Posted 26 March 2012 - 11:48 AM

Can your write the code for reading one integer from a file? It's pretty much the same process for the rest of the values, repeated.

You want fscanf. Indeed, there's enough code you should be able to write it.
Was This Post Helpful? 0
  • +
  • -

#8 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

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");


Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2934
  • View blog
  • Posts: 10,141
  • Joined: 08-August 08

Re: Questions regarding arrays and matrices

Posted 26 March 2012 - 01:13 PM

Does that work for you? I doubt it. Look here.
Was This Post Helpful? 1
  • +
  • -

#10 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 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]); 


Was This Post Helpful? 0
  • +
  • -

#11 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

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.
Was This Post Helpful? 1
  • +
  • -

#12 erkant  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 108
  • Joined: 26-October 10

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:

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

Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

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
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.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1