Matrix

Hi, I have a doubt about arrays: is there any way to generalize an array? Like
int x;
array[x];

?

I need to write a program about matrix multiplication using threads (win32), I can do it with fixed matrix sizes, but not with a matrix A[m x n] and B[n x p].

Thanks.
What doubts do you have? If you know how arrays work, you know what you're trying to do can't be done. An array is an amount of memory allocated to hold x elements. x must be defined as a const unsigned int at the time of declaration of the array. The reason being is that memory must be set aside for use of that array. You can virtually shrink an array by using a counter variable and making sure you don't exceed that, but you're unable to expand an array, easily.

If you're looking for a dynamically sized container, I highly suggest looking into vectors. They're versatile and would suit your needs for a generalized array.

If you're looking for generalization in terms of the data stored, you will need to look into templates.

Depending on what exactly you need help with, I can help you further, but that's the best I can do with what you've given me.
Ahn... I need to write multiply two matrix, A[m x n] and B [n x p]. Here's my code to fixed size matrix:



#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>

#define BUF_SIZE 255
DWORD WINAPI MyThreadFunction( LPVOID lpParam );
typedef struct MyData {
    int *pontLine;
    int *pontCol;
	int result;
} MYDATA, *PMYDATA;

int main(){

	HANDLE  hThreadArray[4]; 
	DWORD   dwThreadIdArray[4];

	PMYDATA buffer;

	int m1[2][3] = {{1,2,5},{4,0,1}};
	int m2[3][2] = {{0,2},{1,4},{1,0}};
	int product[2][2] = {{0, 0}, {0, 0}};
	



	for(int i=0; i < 2; i++){/
		for(int j=0; j < 2; j++){//mesma coisa com as colunas
			(buffer[i*2+j]).pontLine = (int*) malloc(3*sizeof(int));
			(buffer[i*2+j]).pontCol = (int*) malloc(3*sizeof(int));
			for(int k=0; k < 3; k++){
				(buffer[i*2+j]).pontLine[k] = m1[i][k];
				(buffer[i*2+j]).pontCol[k]  = m2[k][j];
			}

			
				hThreadArray[i*2+j] = CreateThread( 
				NULL,                   // default security attributes
				0,                      // use default stack size  
				MyThreadFunction,       
				&buffer[i*2+j],          
				0,                      // use default creation flags 
				&dwThreadIdArray[i*2+j]);	
			}
	}

    
	WaitForMultipleObjects(4, hThreadArray, TRUE, INFINITE);
	for (int i = 0; i < 2; i++) {
		printf(" %d", buffer[i].result);
	}

	printf("\n");
	for (int i=2; i<4; i++) {
		printf(" %d", buffer[i].result);
	}
	getchar();
    return 0;
}



DWORD WINAPI MyThreadFunction( LPVOID lpParam ) 
{
    HANDLE hStdout; 
    PMYDATA pDataArray;

    TCHAR msgBuf[BUF_SIZE];
    size_t cchStringSize;
    DWORD dwChars;

  
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if( hStdout == INVALID_HANDLE_VALUE )
        return 1;

 
    pDataArray = (PMYDATA)lpParam;
	int element = 0;
	for (int k = 0; k < 3; k++){
		element = element + pDataArray->pontCol[k]*pDataArray->pontLine[k];
	}
	pDataArray->result = element;
    
	
    return 0; 
}



I know there are a simpler way to do it, but the subject is thread and the professor asked to use they(the comments were written in portuguese, so I just cut them off).

I would like to ask the user to type a matrix (first the size, them the elements) and multiply it for another matrix (that the user would type too). So I was thinking in how to do it..
Alright, so the problem is a user defined matrix of n rows and m cols. You're going to need to personally allocate the memory for these matrices then. I'm unsure how familiar you are with the new keyword, but you're going to get acquainted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main() {
   int userRows = 5, userCols = 2;
   
   int *userMatrix = new int[userRows * userCols];
   
   for (int i = 0; i < userRows; i ++)
      for (int j = 0; j < userCols; j ++) {
         std::cin >> userMatrix[i * userCols + j];
      }
      
   for (int i = 0; i < userRows; i ++) {
      for (int j = 0; j < userCols; j ++)
         std::cout << userMatrix[i * userCols + j] << " ";
      std::cout << "\n";
   }

   delete[] userMatrix; // To free the memory that was "new'd"
   
   return 0;
}
0
1
2
3
4
5
6
7
8
9
0 1
2 3
4 5
6 7
8 9


Edit: In the above, I just defined the rows/cols, but you can see how the matrix is created and accessed. You can use user defined rows/cols and you can store them as variables so you don't go out of bounds. Also, make sure you delete the new memory that you allocated.
Last edited on
I'll try it! Thank you so much =]
Topic archived. No new replies allowed.