ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Can somebody help with this code:
Task: Square matrix clockwise rotation using 2*N Threads 1)first quater i.e N/2 (4,7)->(11,6) ..2) second quater (11,16)->(10,13),so on and so forth...
Thanks,
Mozala
Input matrix 4x4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Matrix 4x4 after rotation:
13 2 3 1
5 10 6 8
9 11 7 12
16 14 15 4
Code:
#include <pthread.h>
#include <iostream>
#define MAX_ROW 10
#define MAX_COL 10
using namespace std;
struct matrix_index_type
{
int row;
int col;
};
struct matrix_index_type matrix_index[MAX_ROW][MAX_COL];
int aux[MAX_COL];
int matrix[MAX_ROW][MAX_COL];
/*-------------------Read Matrix------------------*/
void read_matrix(int matrix[][MAX_COL])
{
int i,j;
for(i = 0; i < MAX_ROW; i ++)
{
for(j = 0; j < MAX_COL; j ++)
{
cin<<matrix[i][j];
}
}
}
/*--------------PRINT OUT MATRIX-------------------*/
output_matrix(int matrix[][MAX_COL])
{
int i,j;
for(i = 0; i < MAX_ROW; i ++)
{
for(j = 0; j < MAX_COL; j ++)
{
cout<<matrix[i][j]<<endl;
}
cout<<"\n";
}
cout<<"Finished printing.."<<endl;
}
/*---------------------Rotate---------------------*/
void *rotate( void * index)
{
int i,j,x;
struct matrix_index_type *pindex;
pindex = (struct matrix_index_type *) index;
i = pindex->row;
j = pindex->col;
aux[MAX_COL]=matrix[i][j];
pthread_mutex_unlock(MD[j])
aux[MAX_COL]=matrix[i][j];
pthread_mutex_unlock(MD[i]);
pthread_mutex_lock(SD[j])
for (x=0;x<MAX_COL;x++)
{
pthread_exit(NULL);
}
}
/*-------------------MAIN PROGRAM-----------------*/
int main()
{
int Imatrix[][MAX_COL]={1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16};
int i, j;
int index[MAX_ROW][MAX_COL];
pthread_t ptd[MAX_ROW][MAX_COL];
read_matrix(Imatrix);
cout<<"Input matrix is:\n"<<endl;
output_matrix(Imatrix);
for(i = 0; i < MAX_ROW; i ++)
{
for(j = 0; j < MAX_COL; j ++)
{
matrix_index[i][j].row = i;
matrix_index[i][j].col = j;
pthread_create( &ptd[i][j],NULL,rotate, (void*) &matrix_index[i][j]);
}
}
for(i = 0; i < MAX_ROW; i ++)
{
for(j = 0; j < MAX_COL; j ++)
{
pthread_join( ptd[i][j], NULL);
}
}
cout<<"Rotated matrix is:\n";
output_matrix(Imatrix);
}
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
return 0;
}
What exactly is your problem? What is MD? You seem to be using a mutex without initializing it, I'm not sure that's allowed. Furthermore, in rotate, you're unlocking a lock before it's locked.
What exactly is your problem? What is MD? You seem to be using a mutex without initializing it, I'm not sure that's allowed. Furthermore, in rotate, you're unlocking a lock before it's locked.
Ok ,first thing first
Task: Rotate Main and Secondary diagonal(SD) of the Square matrix clockwise,in other words main will be in the place of secondary and the latter the first place ,so having integers in the matrix my will be runned by 2*N threads meaning i have N*N dimension right? and then to start with the upper half of the main diagonal(MD)will be rotate to the place of the upper half part of SD by the first quarter [1/4(2*N)]of the threads ,the second 1/4 of the threads will take the upper half part of the SD to the lower half part of the main and the third 1/4 of threads will do the same thing so on ,so in which case i have to use global variable and use mutex to avoid race condition right? anyway to cut the long story short....
Rotate the elements of both diagonals in the clock direction.
Input matrix 4x4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Matrix 4x4 after rotation:
13 2 3 1
5 10 6 8
9 11 7 12
16 14 15 4
Here is a kinda of updated code than the first one..im still tryuing to work on it but i have a problem with rotate it and then at same time taking care of mutex and its kinda confusing even i feel like im close to the end...thats why i need help..my head banging.,.
Code:
#include <pthread.h>
#include <iostream>
#define M 10
#define N 10
using namespace std;
struct matrix_index_type
{
int row;
int col;
};
struct matrix_index_type matrix_index[M][N];
int aux[N];
pthread_mutex_t SD[j];
pthread_mutex_t MD[i];
/*-------------------Read Matrix------------------*/
void read_matrix(int matrix[][N])
{
int i,j;
for(i = 0; i < M; i ++)
{
for(j = 0; j < M; j ++)
{
cin<<matrix[i][j];
}
}
}
/*--------------PRINT OUT MATRIX-------------------*/
output_matrix(int matrix[][N])
{
int i,j;
for(i = 0; i < M; i ++)
{
for(j = 0; j < N; j ++)
{
cout<<matrix[i][j]<<endl;
}
cout<<"\n";
}
cout<<"Finished printing.."<<endl;
}
/*---------------------Rotate---------------------*/
void *rotate( void * index)
{
int i,j,x;
struct matrix_index_type *pindex;
pindex = (struct matrix_index_type *) index;
i = pindex->row;
j = pindex->col;
for(i = 0; i < N-1; i++)
{
for(j = 0; j <N-1; j++)
{
pthread_mutex_lock(MD[i]);
aux[N]=matrix[i][j];
pthread_mutex_unlock(MD[i]);
pthread_mutex_lock(SD[j]);
if(i < N/2) && (j<N/2)
{
matrix[i][j+N/2]=aux[N];
}
if(i > N/2) && (j > N/2)
{
matrix[i][j+N/2]=aux[N];
}
}
}
for(i = 0; i < N-1; i++)
{
for(j = N-1; j > 0; j--)
{
pthread_mutex_lock(SD[j])
aux[N]=matrix[i][j];
pthread_mutex_unlock(SD[j]);
pthread_mutex_lock(MD[i]);
if(i < N/2) && (j > N/2)
{
matrix[i][j+N/2]=aux[N];
}
if(i > N/2) && (j < N/2)
{
matrix[i][j+N/2]=aux[N];
}
}
}
for (x=0;x<MAX_COL;x++)
{
pthread_exit(NULL);
}
}
/*-------------------MAIN PROGRAM-----------------*/
int main()
{
int Imatrix[][N]={1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16};
int i, j;
int index[M][N];
pthread_t ptd[M][N];
read_matrix(Imatrix);
cout<<"Input matrix before being been rotated is:\n"<<endl;
output_matrix(Imatrix);
for(i = 0; i < N; i ++)
{
for(j = 0; j < N; j ++)
{
matrix_index[i][j].row = i;
matrix_index[i][j].col = j;
pthread_create( &ptd[i][j],NULL,rotate, (void*) &matrix_index[i][j]);
}
}
for(i = 0; i < M; i ++)
{
for(j = 0; j < N; j ++)
{
pthread_join( ptd[i][j], NULL);
}
}
cout<<"Rotated matrix is:\n";
output_matrix(Imatrix);
}
pthread_mutex_destroy(&mutex);
pthread_exit(NULL);
return 0;
}
What exactly is your problem? What is MD? You seem to be using a mutex without initializing it, I'm not sure that's allowed. Furthermore, in rotate, you're unlocking a lock before it's locked.
Ok ,first thing first
Task: Rotate Main and Secondary diagonal(SD) of the Square matrix clockwise,in other words main will be in the place of secondary and the latter the first place ,so having integers in the matrix my will be runned by 2*N threads meaning i have N*N dimension right? and then to start with the upper half of the main diagonal(MD)will be rotate to the place of the upper half part of SD by the first quarter [1/4(2*N)]of the threads ,the second 1/4 of the threads will take the upper half part of the SD to the lower half part of the main and the third 1/4 of threads will do the same thing so on ,so in which case i have to use global variable and use mutex to avoid race condition right? anyway to cut the long story short....
Rotate the elements of both diagonals in the clock direction.
Input matrix 4x4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Matrix 4x4 after rotation:
13 2 3 1
5 10 6 8
9 11 7 12
16 14 15 4
Here is a kinda of updated code than the first one..im still tryuing to work on it but i have a problem with rotate it and then at same time taking care of mutex and its kinda confusing even i feel like im close to the end...thats why i need help..my head banging.,.
Code:
#include <pthread.h>
#include <iostream>
#define M 10
#define N 10
using namespace std;
struct matrix_index_type
{
int row;
int col;
};
struct matrix_index_type matrix_index[M][N];
int aux[N];
/*-------------------Read Matrix------------------*/
void read_matrix(int matrix[][N])
{
int i,j;
for(i = 0; i < M; i ++)
{
for(j = 0; j < M; j ++)
{
cin<<matrix[i][j];
}
}
}
/*--------------PRINT OUT MATRIX-------------------*/
output_matrix(int matrix[][N])
{
int i,j;
for(i = 0; i < M; i ++)
{
for(j = 0; j < N; j ++)
{
cout<<matrix[i][j]<<endl;
}
cout<<"\n";
}
cout<<"Finished printing.."<<endl;
}
/*---------------------Rotate---------------------*/
void *rotate( void * index)
{
int i,j,x;
struct matrix_index_type *pindex;
pindex = (struct matrix_index_type *) index;
i = pindex->row;
j = pindex->col;
for(i = 0; i < N-1; i++)
{
for(j = 0; j <N-1; j++)
{
pthread_mutex_lock(MD[i]);
aux[N]=matrix[i][j];
pthread_mutex_unlock(MD[i]);
pthread_mutex_lock(SD[j]);
if(i < N/2) && (j<N/2)
{
matrix[i][j+N/2]=aux[N];
}
if(i > N/2) && (j > N/2)
{
matrix[i][j+N/2]=aux[N];
}
}
}
for(i = 0; i < N-1; i++)
{
for(j = N-1; j > 0; j--)
{
pthread_mutex_lock(SD[j])
aux[N]=matrix[i][j];
pthread_mutex_unlock(SD[j]);
pthread_mutex_lock(MD[i]);
if(i < N/2) && (j > N/2)
{
matrix[i][j+N/2]=aux[N];
}
if(i > N/2) && (j < N/2)
{
matrix[i][j+N/2]=aux[N];
}
}
}
for (x=0;x<MAX_COL;x++)
{
pthread_exit(NULL);
}
}
/*-------------------MAIN PROGRAM-----------------*/
int main()
{
int Imatrix[][N]={1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16};
int i, j;
int index[M][N];
pthread_t ptd[M][N];
pthread_mutex_t SD[j];
pthread_mutex_t MD[i];
read_matrix(Imatrix);
cout<<"Input matrix before being been rotated is:\n"<<endl;
output_matrix(Imatrix);
/*Initialize mutexes */
for (int i = 0; i < N; i++)
{
pthread_mutex_init(&MD[i], NULL);
}
for (j = 0; j < N; j++)
{
pthread_mutex_init(&SD[j], NULL);
}
/* Creating Threads */
for(i = 0; i < N; i ++)
{
for(j = 0; j < N; j ++)
{
matrix_index[i][j].row = i;
matrix_index[i][j].col = j;
pthread_create( &ptd[i][j],NULL,rotate, (void*) &matrix_index[i][j]);
}
}
/*Waiting for all threads completion*/
for(i = 0; i < M; i ++)
{
for(j = 0; j < N; j ++)
{
pthread_join( ptd[i][j], NULL);
}
}
cout<<"Rotated matrix is:\n";
output_matrix(Imatrix);
}
pthread_mutex_destroy(&SD[j]);
pthread_mutex_destroy(&MD[j]);
pthread_exit(NULL);
return 0;
}
Even if it was ,this forum is to help one with the code that has already been written ,right?But strictly not to write the whole code for ppl right?So whats ur score ?U have no idea how to help ,then u better save ur breath ,For God sake!!,for easier ones...
I've not looked at your code but you might find it easier to tackle it in two steps:
1) Solve it with a standard function that performs the rotations
2) Then put each invokation of the rotate function into its own thread.
Its the placing of the parentheses that I am questioning and wether the if condition is valid.
ie if(something) && (something)
does this evaluate to
if( something && something)??
[edit]
better still cache N/2.
Code:
{
//cache the int value of N/2 within the scope of the braces
int half_n = N/2;
for(i = 0; i < N-1; i++)
{
for(j = 0; j <N-1; j++)
{
pthread_mutex_lock(MD[i]);
aux[N]=matrix[i][j];
pthread_mutex_unlock(MD[i]);
pthread_mutex_lock(SD[j]);
if(i < half_n && j<half_n)
{
matrix[i][j+half_n]=aux[N];
}
if(i > half_n && j > half_n)
{
matrix[i][j+half_n]=aux[N];
}
}
}
}//scope of N/2 ends
[editedit]
Quote:
U r comedian indeed
Even if it was ,this forum is to help one with the code that has already been written ,right?But strictly not to write the whole code for ppl right?So whats ur score ?U have no idea how to help ,then u better save ur breath ,For God sake!!,for easier ones...
Comments like the above neither help the forum or make people want to help you
Quote:
...U have no idea how to help
forgive me if I am wrong but from what I see you have dumped code and not explained what your problem is!
Quote:
Originally Posted by http://lordandmaker.co.uk/forumhelp/forumhelp.htm
...
- Body
This should be easy to read and in proper English. Re-read it when you've finished, to make sure it makes sense. Cut out any background information that isn't necessary, but put in all the useful information you have. If you're not sure whether something is useful or not, put it at the bottom.
Say what went wrong, and when. Describe what you've tried, and the results of that.
Describe the symptoms of the problem as accurately as possible, and don't make guesses as to what might be causing the problem. The closer the information you give the forum is to the raw information you see, the more likely you are to get a relevant answer
Ask accurate questions. Asking "i need some information about ______" will not get you a useful answer. You need to specify what information you want and, if necessary, why. If your question can be answered with a 'yes' or 'no', you will likely get that answer.
...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.