Question

Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12]

1.      Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to convert all 60 array elements modulo 11 (i.e. take the remainder after division by 11) in placeYou should divide this update task among the n threads as evenly as possible. Print the array both before and after the update separately as 5 by 12 matrices. Hint: it is dangerous to have code to print messages in your thread function(s). Note: if you do not use pthread to divide the update task among the threads, you may get zero points. A sample run of the programs in 1 and 2 is shown below. You do NOT need to submit screen shots. Instead submit your source files.

[kwang@computer][~/work/]$ ./A6p1 3 60

using 3 threads to handle 60 chars.

original random upper case string:

RUIQWCXXBFXVZFJDBWTSIBFIMMJJNRKENUUJZUIABIYANHFQDZINCPVOCGAP

off-by-one lower case string:

svjrxdyycgywagkecxutjcgjnnkkoslfovvkavjbcjzboigreajodqwpdhbq

[kwang@computer][~/work/]$ ./A6p2 4

using 4 threads.

original array:

33           36            9               30            1               29            14            16            7               34            6               18            

14           2               34            11            18            11            6               17            34            33            16            35            

26           11            17            13            27            39            22            21            24            19            11            25            

9              24            29            15            7               35            21            9               25            16            20            3               

26           14            19            9               7               24            4               21            34            9               33            22            

updated array:

0               3               9               8               1               7               3               5               7               1               6               7               

3               2               1               0               7               0               6               6               1               0               5               2               

4               0               6               2               5               6               0               10            2               8               0               3               

9               2               7               4               7               2               10            9               3               5               9               3               

4               3               8               9               7               2               4               10            1               9               0               0


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

#include <iostream>

#include <cstdlib>

#include <pthread.h>


using namespace std;


#define COLS 12 //column size

#define ROWS 5 //row size


int SIZE_REQ;

int arr[ROWS][COLS] ;


//matrix of sqaures creation using threads

void *SQUAREMATRIX(void *threadid) {

  long tid;

  

  tid = (long)threadid;

  

  int start = tid * SIZE_REQ;

  int end=start+SIZE_REQ-1;

  for(int i=0; i<ROWS; i++){

    

    for(int j=0;j<COLS;j++){

      

      int Pos_REQ = i*COLS + j;

      

      if(Pos_REQ>=start&&Pos_REQ<=end)

        {

          arr[i][j]=arr[i][j]*arr[i][j];

        }

    }

    

  }

  

  pthread_exit(NULL);

}



int main ()

{

  int n=0;

  

  cin>>n;

  if(n<=4 && n>=2)

    {

      pthread_t threads[n];

      int rc;

      long i,j;

      

      

      //loading array with data

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

        {

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

            {

              arr[i][j]=(rand() % 49) + 1;

            }

        }

      

      

      //displaying Array in random

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

        {

          cout<<"\n";

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

            {

              cout<<arr[i][j]<<"\t";

            }

        }

      

      SIZE_REQ = (60 + n - 1) / n; // divide by threads rounded up.

      

      

      

      

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

        cout << "\nmain() : creating thread, " << i << endl;

        rc = pthread_create(&threads[i], NULL, SQUAREMATRIX, (void *)i);

        

        if (rc) {

          cout << "Error:unable to create thread," << rc << endl;

          exit(-1);

        }

      }

      //Printing Resulter Array

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

        {

          cout<<"\n";

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

            {

              cout<<arr[i][j]<<"\t";

            }

        }

      pthread_exit(NULL);

    }

  else

    cout<<"invalid command"; //if input is wrong

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
Write a C or C++ program A6p2.c[pp] that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 39 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12]
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
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