Question

In: Computer Science

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));

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

To compile

gcc a.c -pthread

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include<pthread.h>
#include<unistd.h>
int** a;


// a structure for passing argument in thread
typedef struct {
    int row;
    int col;
}args;
int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}
void* fun(void * arg){
    args* temp = (args *)arg;
    qsort(a[temp->row],temp->col,sizeof(int),cmpfunc);
    for(int i=0;i<temp->col;i++)printf("%d,",a[temp->row][i]);
    printf("\n");
    return NULL;

}


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;
    pthread_t tid[m];
    for(int i=0;i<m;i++){
        args arg ;
        arg.row = i;
        arg.col = n;
        pthread_create(&tid[i],NULL,fun,(void *)&arg);
        usleep(1000);  // applied micro sleep so that thread returns correctly
    }
    for(int i=0;i<m;i++){
        pthread_join(tid[i],NULL);
    }
    printf("\n");
    printf("\n");
    printf("By main thread\n");
    printf("\n");
    for(i = 0; i < m; i++){
        for(j = 0; j < n; j++)
          printf("%d,", a[i][j]);
        printf("\n");
    }
    return 0;
}


Related Solutions

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));...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with...
Project 1 - Arrays - Grader The purpose of this assignment is to practice dealing with arrays and array parameters. Arrays are neither classes nor objects. As a result, they have their own way of being passed as a parameter and they also do not support the dot operator ( .), so you cannot determine how full they are. This results in some pain and suffering when coding with arrays. It is this awareness that I am trying to get...
Project Assignment The purpose of this assignment is for you to gain practice in applying the...
Project Assignment The purpose of this assignment is for you to gain practice in applying the concepts and techniques we learned in class. In order to complete the assignment, please do the following: 1. find or create a data set containing values of at least one interval or ratio variable for at least one-hundred cases (n >= 100); 1 2. provide basic descriptive statistics to summarize the central tendency and variability of the data; 3. provide at least one table...
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...
The goal of this project is to practice (Write a C Program) with a function that...
The goal of this project is to practice (Write a C Program) with a function that one of its parameter is a function.The prototype of this function is: void func ( float (*f)(float*, int), float* a, int length); This means the function: func has three parameters: float (*f)(float*, int): This parameter itself is a function: f that has two parameters and returns a floating-point number. In the body of the function: func we call the function: f with its arguments...
The Programming Language is C++ Objective: The purpose of this project is to expose you to:...
The Programming Language is C++ Objective: The purpose of this project is to expose you to: One-dimensional parallel arrays, input/output, Manipulating summation, maintenance of array elements. In addition, defining an array type and passing arrays and array elements to functions. Problem Specification: Using the structured chart below, write a program to keep records and print statistical analysis for a class of students. There are three quizzes for each student during the term. Each student is identified by a four-digit student...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators...
C++ Programming 19.2 Operator Overloading practice Write the prototypes and functions to overload the given operators in the code main.cpp //This program shows how to use the class rectangleType. #include <iostream> #include "rectangleType.h" using namespace std; int main() { rectangleType rectangle1(23, 45); //Line 1 rectangleType rectangle2(12, 10); //Line 2 rectangleType rectangle3; //Line 3 rectangleType rectangle4; //Line 4 cout << "Line 5: rectangle1: "; //Line 5 rectangle1.print(); //Line 6 cout << endl; //Line 7 cout << "Line 8: rectangle2: "; //Line...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists...
This is a C++ assignment The necessary implementations: Use arrays. Write some functions. Practice processing lists of values stored in an array. Write a modular program. Sort an array. Requirements to meet: Write a program that asks the user to enter 5 numbers. The numbers will be stored in an array. The program should then display the numbers back to the user, sorted in ascending order. Include in your program the following functions: fillArray() - accepts an array and it's...
For several decades, it was a common practice in Southern California for houses to be built...
For several decades, it was a common practice in Southern California for houses to be built with pools in the backyard (as any airplane flight which ends at a Southern California airport will reveal). Now, however, that practice may be changing, possibly because of the recent demand for landscaped homes, which experts believe help reduce pollution. A recent study examined a random sample of 161 houses built in Southern California between 1950 and 1985 and an independent, random sample of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT