Question

The C++ program below contains a function that adds a specific row of two matrices and...

The C++ program below contains a function that adds a specific row of two matrices and store the result in the corresponding row of a third matrix. This is done by a loop to invokes a function that adds a single row of the two matrices. By the end of the loop all rows should be added and stored in the result matrix. The rows are added sequentially one after the other. You are required to modify the program below to use C++11 threads to add the rows of the two matrices using multithreading, such that all the rows are added concurrently. You will need to join on the running threads in the correct location of the code to be able to wait for all the threads to finish before you start printing the results.

THE PROGRAM:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>


void add_matrix_row (int * mat1,int * mat2, int * result, int cols)
{
for ( int c = 0 ; c < cols ; c++)
result[c] = mat1[c]+mat2[c];
}

int main ()
{
int rows = 3;
int cols = 4;
int array1[3][4]= {{100,20,3,50}, {40,33,56,2}, {150,23,17,22}};
int array2[3][4]= {{56,13,9,100},{22,15,55,60}, {19,200,27,14}};
int result[3][4];
for ( int r = 0 ; r < rows ; r++)
add_matrix_row(array1[r],array2[r],result[r],cols);

for ( int r = 0 ; r < rows ; r++) {
for ( int c = 0 ; c < cols ; c++) printf ("%4d ",array1[r][c]);
printf ("\n");
}
printf (" + \n");
for ( int r = 0 ; r < rows ; r++){
for ( int c = 0 ; c < cols ; c++) printf ("%4d ",array2[r][c]);
printf ("\n");
}
printf (" = \n");
for ( int r = 0 ; r < rows ; r++) {
for ( int c = 0 ; c < cols ; c++) printf ("%4d ",result[r][c]);
printf ("\n");
}
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <thread>
using namespace std;

// One thread per row
#define THREADS_NUMBER 3


void add_matrix_row (int * mat1, int * mat2, int * result, int cols)
{
    for ( int c = 0 ; c < cols ; c++){
            result[c] = mat1[c] + mat2[c];
        }
}

int main ()
{
    int rows = 3;
    int cols = 4;
    int array1[3][4]= {{100,20,3,50}, {40,33,56,2}, {150,23,17,22}};
    int array2[3][4]= {{56,13,9,100},{22,15,55,60}, {19,200,27,14}};
    int result[3][4];

//    for ( int r = 0 ; r < rows ; r++)
//       add_matrix_row(array1[r],array2[r],result[r],cols);

    for ( int r = 0 ; r < rows ; r++)
    {
        for ( int c = 0 ; c < cols ; c++) printf ("%4d ",array1[r][c]);
        printf ("\n");
    }
    printf (" + \n");
    for ( int r = 0 ; r < rows ; r++)
    {
        for ( int c = 0 ; c < cols ; c++) printf ("%4d ",array2[r][c]);
        printf ("\n");
    }
    printf (" = \n");

    // Create an array of threads
    std::thread threads[THREADS_NUMBER];
    for (int i = 0; i < THREADS_NUMBER; i++) {
       // Initialize each thread with the function responsible of adding row of the matrices
        threads[i] = std::thread(add_matrix_row, array1[i], array2[i], result[i], cols);
    }

    // Waiting for join threads after compute
    for (int i = 0; i < THREADS_NUMBER; i++) {
            threads[i].join();
    }

    for ( int r = 0 ; r < rows ; r++)
    {
        for ( int c = 0 ; c < cols ; c++) printf ("%4d ",result[r][c]);
        printf ("\n");
    }
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
The C++ program below contains a function that adds a specific row of two matrices and...
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
  • Here's the main function of a C program that Reads in two matrices by prompting the...

    Here's the main function of a C program that Reads in two matrices by prompting the user for their dimensions, dynamically allocating memory from the heap, and reading the values into the memory using row major ordering for the matrix values. Multiplies the two matrices together and puts the result into another dynamically allocated piece of memory (after checking that the dimensions are appropriate for matric multiplication). Outputs the two input and the result matrices. Write the implementations of the...

  • The objective is to use each row of the first column as a starting point to...

    The objective is to use each row of the first column as a starting point to come up with a sum of absolute value of differences . For each row, a sum will be calculated, therefore, 5 sums must be calculated. Each sum will be placed into the array, sumList[ ]. #include <stdio.h> #include <stdlib.h> #include <math.h> #define ROWS 5 #define COLS 6 void calcSums(int topog[ROWS][COLS], int sumList[ROWS] ); int main() { int topography[ROWS][COLS] = { { 3011, 2800, 2852,...

  • 06) Write a C program to perform matrix operations based of threads. The program accepts from...

    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...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given....

    ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...

  • Last assignment: C programming Submission must include: a function, an array, and a menu system Goal...

    Last assignment: C programming Submission must include: a function, an array, and a menu system Goal is to create a program that will generate a random exercise for the target area selected and the number of times it will be done (all of the options except cardio) or the amount of time doing the task.(the cardio option), but I'm having trouble figuring out how to generate a random option using an array. This is what I have so far... #include...

  • Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row...

    Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row major order. Tries to ensure even spacing. void print_grid(int * my_array, int rows, int cols){ int i,j; for(i=0; i<rows; i++){ for(j=0; j<cols; j++){ if(my_array[i*cols + j]!=-1){ printf(" %d ", my_array[i*cols + j]); } else{ printf("%d ", my_array[i*cols + j]); } } printf("\n"); } } int main(int argc, char *argv[]) { int seed,rows,cols;...

  • Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...

    Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program A3a without modification: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]); void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]); void getStudents(int COLS, char students[COLS][20]); void printStudents(int COLS, char students[COLS][20]); void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]); void printFinalGrades(int COLS, char Fgrades[]); int main() {    srand(time(0));    int stu = 0, assign = 0;...

  • MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds...

    MUST BE PROCEDURAL CODE. Write a program in C++ that reads two matrices and: 1) adds them (if compatible) and prints their sum, 2) subtracts them (if compatible) and prints their difference, and 3) multiplies them (if compatible) and prints their product. Prompt the user for the names of two matrix input files (see format below) and place the output in a file of the user’s choosing. The format of the input files should contain the number of rows, followed...

  • 1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer...

    1. (50 pts) Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string ('A'<>'Z', 'B'<->'Y', 'C''X', etc). You should divide this conversion task among the n threads as evenly as possible, Print out the...

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