Question

In: Computer Science

The purpose of this question is to practice the pthread built in functions. The following c...

The purpose of this question is to practice the pthread built in functions.

The following c program is a simple program to make a matrix of integers and print it.

//File name: a.c

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

int** a;

int main(){

time_t t;

int m, n, i, j;       //m is the numbers of rows and n is the number of columns.

printf("Enter the number of rows, and columns: ");

scanf("%d%d", &m, &n);

printf("%d, %d\n", m, n);

srand((unsigned) time(&t));

a=(int**) malloc(m*sizeof(int*));

for(j = 0; j < n; j++)

    a[j] = (int*) malloc(n * sizeof(int*));

for(i = 0; i < m; i++)

    for(j = 0; j < n; j++)

      a[i][j] = rand() % 1000;

for(i = 0; i < m; i++){

    for(j = 0; j < n; j++)

      printf("%d,", a[i][j]);

    printf("\n");

}

return 0;

}

Your project uses pthread built-in functions based on the following conditions:

1. The program reads from the console the number of rows and the number of columns (like the above program). Therefore, the matrix has m rows and n columns.

2. The program creates m threads.

3. Each thread assigns random numbers to one row of the matrix.

4. The function main, sorts each row.

5. Each thread displays its sorted row.

6. The function: main displays the entire matrix.

Answer: ?

Solutions

Expert Solution

#include<stdio.h>

#include<pthread.h>

#include<unistd.h>

#include<stdlib.h>

#define MAX 4

void *mult(void* arg)

{

    int *data = (int *)arg;

    int k = 0, i = 0;

    int x = data[0];

    for (i = 1; i <= x; i++)

{

k += data[i]*data[i+x];

      }

    int *p = (int*)malloc(sizeof(int));

    *p = k;

    pthread_exit(p);

}

int main()

{

    int matA[MAX][MAX];

    int matB[MAX][MAX];

    int r1=MAX,c1=MAX,r2=MAX,c2=MAX,i,j,k;

printf("Enter the number of rows, and columns: ");

scanf("%d%d", &m, &n);

printf("%d, %d\n", m, n);

    for (i = 0; i < r1; i++)

            for (j = 0; j < c1; j++)

                   matA[i][j] = rand() % 10;

    for (i = 0; i < r1; i++)

            for (j = 0; j < c1; j++)

                   matB[i][j] = rand() % 10;

    for (i = 0; i < r1; i++){

        for(j = 0; j < c1; j++)

            printf("%d ",matA[i][j]);

        printf("\n");

    }

    for (i = 0; i < r2; i++){

        for(j = 0; j < c2; j++)

            printf("%d ",matB[i][j]);

        printf("\n");    

    }

    int max = r1*c2;

    pthread_t *threads;

    threads = (pthread_t*)malloc(max*sizeof(pthread_t));

    int count = 0;

    int* data = NULL;

    for (i = 0; i < r1; i++)

        for (j = 0; j < c2; j++)

               {

            data = (int *)malloc((20)*sizeof(int));

            data[0] = c1;

            for (k = 0; k < c1; k++)

                data[k+1] = matA[i][k];

            for (k = 0; k < r2; k++)

                data[k+c1+1] = matB[k][j];

                pthread_create(&threads[count++], NULL,

                               mult, (void*)(data));

}

printf ("After arranging rows in ascending order\n");

   for (i=0;i<m;++i) {

         for (j=0;j<n;++j) {

                  for (k=(j+1);k<n;++k) {

                        if (matA[i][j] > matA[i][k]) {

                           a = matA[i][j];

                           matA[i][j] = matA[i][k];

                              matA[i][k] = a;

                        }

                  }

            }

      }

      for (i=0;i<m;++i) {

            for (j=0;j<n;++j) {

                  printf (" %d",matT[i][j]);

            }

            printf ("\n");

      }

      printf ("After arranging the columns in descending order \n");

      for (j=0;j<n;++j) {

            for (i=0;i<m;++i) {

                  for (k=i+1;k<m;++k) {

                        if (matB[i][j] < matB[k][j]) {

                              a = matB[i][j];

                              matB[i][j] = matB[k][j];

                              matB[k][j] = a;

                        }

                  }

            }

      }

      for (i=0;i<m;++i) {

            for (j=0;j<n;++j) {

                  printf (" %d",matB[i][j]);

            }

            printf ("\n");

      }

}

    printf("RESULTANT MATRIX IS :- \n");

    for (i = 0; i < max; i++)

    {

      void *k;  

      pthread_join(threads[i], &k);

      int *p = (int *)k;

      printf("%d ",*p);

      if ((i + 1) % c2 == 0)

          printf("\n");

    }

  return 0;

}

}


Related Solutions

The purpose of this project is to practice the pthread built in functions. The following c...
The purpose of this project is to practice the pthread built in functions. The following c program is a simple program to make a matrix of integers and print it. //File name: a.c #include <stdio.h> #include <time.h> #include <stdlib.h> int** a; int main(){ time_t t; int m, n, i, j;       //m is the numbers of rows and n is the number of columns. printf("Enter the number of rows, and columns: "); scanf("%d%d", &m, &n); printf("%d, %d\n", m, n); srand((unsigned) time(&t));...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in...
Question Objective: The purpose of this lab is for you to become familiar with Python’s built-in text container -- class str -- and lists containing multiple strings. One of the advantages of the str class is that, to the programmer, strings in your code may be treated in a manner that is similar to how numbers are treated. Just like ints, floats, a string object (i.e., variable) may be initialized with a literal value or the contents of another string...
The purpose of this C++ programming assignment is to practice using an array. This problem is...
The purpose of this C++ programming assignment is to practice using an array. This problem is selected from the online contest problem archive, which is used mostly by college students worldwide to challenge their programming ability and to prepare themselves for attending programming contests such as the prestige ACM International Collegiate Programming Contest. For your convenience, I copied the description of the problem below with my note on the I/O and a sample executable. Background The world-known gangster Vito Deadstone...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on...
Write a C++ program (using the pthread library) that accepts a phrase of unspecified length on the command line. For example: prompt$: ./vowcon Operating Systems Class at CSUN The main() in this program should read the phrase from the terminal. This phrase can be read into a global variable. This phrase or its parts can be directly accessed from the main() and from the threads. The main() has to create two threads running functions (vow and con). The main() can...
Serial.flush() and Delay() are the two built-in functions. Place them in the following code to remove...
Serial.flush() and Delay() are the two built-in functions. Place them in the following code to remove the garbage data printing as discussed in class. Code: char data; void setup() { // put your setup code here, to run once: Serial.begin(9600); } void loop() { // put your main code here, to run repeatedly: while(Serial.available()==NULL){ data = Serial.read(); Serial.print("given character is: "); Serial.println(data); } Serial.end(); }
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT