Question

06) Write a C program to perform matrix operations based of threads. The program accepts from the user a positive integer N. A menu is given to the user: 1. generate matrices: generates three NxN matrices A, B, C with random integer numbers between 0 and 9 2. add: matrix D- A+B+C 3. subtract: matrix D A-B-C 4. display matrices: A, B, C, D 5. display count of result values more than 8. 6. exit: terminate the program When the operaton is chosen, i Threads will be gencracdlto do the operation where althread i erforms the operation on row i of the three matrices. Moreover, count every D Make sure the threads are running concurrently in a sate manner.The program must handle all possible user or system errors with proper and clear messages and terminates the progr normally a- Show the source file in the report. b- Show the commands used to compile the source file. c- Show a sample run showing all possible mistakes.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#ifndef _HEAD_GUARD_

#define _HEAD_GUARD_

#include <stdlib.h>

#include <stdio.h>

#include <sys/types.h>

#include <pthread.h>

#include <unistd.h>

#endif

typedef struct ds {

   int mat_size;

   int **matrix_fir;

   int **matrix_sec;

int **sum_matrix;

   int count;

} st_thread;

st_thread mat;

int **input_mat(int mat_dimen)

{

   /*definition of local variables*/

   int count_1;

   int count_2;

   int **matrix;

   matrix = (int **)malloc(sizeof (int*) * mat_dimen);

   for (count_1 = 0;count_1 < mat_dimen;count_1++) {

       matrix[count_1] = (int *)malloc(sizeof(int) * mat_dimen);

       for (count_2 = 0;count_2 < mat_dimen;count_2++) {

           (void) fprintf(stdout, "Enter data matrix[%d][%d] : ", count_1, count_2);

           (void) scanf("%d", &matrix[count_1][count_2]);

       }

   }

   return matrix;

}

void create_matrix(st_thread mat;)

{

(void) fprintf(stdout, "\nENTER THE SIZE OF MATRIX:");

   (void) fscanf(stdin, "%d", &(mat.mat_size));

  

   (void) fprintf(stdout, "ENTER THE ELEMENTS OF FIRST MATRIX: \n");

   mat.matrix_fir = input_mat(mat.mat_size);

   (void) fprintf(stdout, "ENTER THE ELEMENTS OF SECOND MATRIX: \n");

   mat.matrix_sec = input_mat(mat.mat_size);

}

void *add_routine(int row)

{

for (int count_1 = 0;count_1 < mat_dimen;count_1++) {

   mat.sum_matrix[row][count_1] = matrix_fir[row][count_1] + matrox_sec[row][count_1];

}

}

void add_mat(int mat_dimen)

{

int **matrix;

pthread_t tid;

for (count_1 = 0;count_1 < mat_dimen;count_1++) {

       matrix[count_1] = (int *)malloc(sizeof(int) * mat_dimen);

for (int count_1 = 0;count_1 < mat_dimen;count_1++) {

pthread_create(&tid, NULL, add_routine(), (void *)&count_1);

}

}

int main(int argc, char *argv[])

{

   /*definition of local variables*/

   pthread_t tid;

int ch;

int **matrix;

   do {

Switch (ch)

{

case 1: create_matrix();
break;
case 2:

add_matrix();

break;
}

printf(“1 to generate the matrices.\n”);

printf(“2 to add the matrices.\n”);

printf(“3 to subtract the matrices.\n”);

printf(“4 to display the matrices.\n”);

printf(“5 ………..n”);

printf(“6 to exit.\n”);

scanf(“%d”, &ch)
} while (ch < 6 && ch >= 1);

  

   for (mat.count = 0;mat.count < mat.mat_size;mat.count++)

       pthread_create(&tid, NULL, subtraction_routine(), (void *)&mat);

   /*terminated successfully*/

   exit(EXIT_SUCCESS);

}

Kindly let me know for any particular functions.

Add a comment
Know the answer?
Add Answer to:
06) Write a C program to perform matrix operations based of threads. The program accepts from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matric...

    JAVA PROGRAM. I need to write a program in java that will perform matrix addition and both matrices have N rows and M columns, N>1 and M>1. The two matrices must be divided to four equal size sub-matrices and each sub-matrix has dimensions N/2 X M/2. I need to create four threads and each thread performs a sub-set of addition on one pair of the sub-matrices. I also need an extra thread for networking. The network part of this uses...

  • Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...

    Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: "Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z)"...

  • It is required to write a Matrix Calculator Program using C Programming Language that could only...

    It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each matrix row-wise. Your program...

  • Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming...

    Problem 3 (20 pts) It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each...

  • in c Q3) Write a program to generate three random numbers between 1 and 9 and...

    in c Q3) Write a program to generate three random numbers between 1 and 9 and display them. The user has to enter their sum as soon as possible. These two steps will be repeated continuously. An alarm will be triggered every 5 seconds to display the numb answers so far. If the user did not give a correct answer in 10 seconds, the timer will jump to the main to give an error message "Too slow response!" and terminate...

  • Write a C program to implement matrix multiplication operations. First, you need to prompt the user...

    Write a C program to implement matrix multiplication operations. First, you need to prompt the user for the size of both matrix. Then take inputs for both matrices and perform matrix multiplication operation. Finally, print the resulting matrix into the output.

  • Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D...

    Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D arrays. a. Get the number of rows and columns of both matrix A and B from the user, and check if multiplication is possible with matrix A and B. b. If matrices multiplication is possible, input the matrix A and B using arrays and multiply matrix A and B.

  • C++ programa use include<iostream> and #include<fstream> grades.txt is Heights.txt Write a C++ program to perform the...

    C++ programa use include<iostream> and #include<fstream> grades.txt is Heights.txt Write a C++ program to perform the following steps 1. Open a file called Heights.txt. The file consists of Grades.txt positive and negative values. A portion of the file is shown her → 11.5 -9.9 3.11 2. If the open operation fails, display an error 8.50 77.8 5.71 message and terminate the program. 3. Once the file is successfully opened, read the contents and write the positive values to a new...

  • I want to write a C++ program which starts 2 threads. The first thread periodically generates...

    I want to write a C++ program which starts 2 threads. The first thread periodically generates a random number and adds it to the total. The second thread periodically generates a random number and subtracts it from the total. The main thread periodically monitors the value of the total. if the total exceeds the maximum value, the program terminates and alerts the user that the maximum has been exceeded. If the total undercuts the minimus value, the program terminates and...

  • Write two programs Client and Server

    Given two integer matrices A and B, you are requested to compose a program to perform matrix addition (A + B). Both matrices have N rows and M columns; N > 1, M > 1; You need to divide both (A and B) into four equal (or close to equal) size of submatrices (A0,0, A0,1, A1,0, A1,1 and B0,0, B0,1, B1.0, B1.1)andeachsubmatrixhasdimensioncloseto(N/2)x(M/2). YouneedtocreatefourJavathreads each thread performs a subset of addition on one pair of the submatrices. Example, thread 0 performs addition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT