Question

In: Other

Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...

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 49 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 square all 60 array elements. You 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.

Note: Must use pthread to divide the update task among the threads.

Solutions

Expert Solution

#include <iostream>
#include <cstdlib>
#include <pthread.h>


using namespace std;

#define COLS 12
#define ROWS 5


int chunkSize;
int n=0;
int arr[ROWS][COLS] ;
void DoSquare(void threadid) {
long tid;
  
tid = (long)threadid;
  
int start = tid * chunkSize;
int end=start+chunkSize-1;

  

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

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

int position = i*COLS + j;

if(position>=start&&position<=end)
{
arr[i][j]=arr[i][j]*arr[i][j];
}
}

}

  
  
  
pthread_exit(NULL);
}

int main () {

cin>>n;
pthread_t threads[n];
int rc;
int i,j,k;


//fill array wil data
for(i=0;i<ROWS;i++)
{
for(j=0;j<COLS;j++)
{
arr[i][j]=(rand() % 49) + 1;
}
}


//Printing Random Array
for(i=0;i<ROWS;i++)
{
cout<<"\n";
for(j=0;j<COLS;j++)
{
cout<<arr[i][j]<<"\t";
}
}

chunkSize = (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, DoSquare, (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);
}


Related Solutions

Write a C program called test that takes one command line argument, an integer N. When...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits
Write a C program that accepts a port number as a command line argument, and starts...
Write a C program that accepts a port number as a command line argument, and starts an HTTP server. This server should constantly accept() connections, read requests of the form: GET /path HTTP/1.1\r\n\r\n read the file indicated by /path, and send it over the "connect" file descriptor returned by the call to accept().
A C program that accepts a single command line argument and converts it in to binary...
A C program that accepts a single command line argument and converts it in to binary with array length of 16 bits. The array should contain the binary of the int argument. the program should also convert negative numbers. Side note the command line arg is a valid signed int.
1. Write a queue client, "LineNum," that takes an integer command line argument “n” and prints...
1. Write a queue client, "LineNum," that takes an integer command line argument “n” and prints the nth string from the first string found on standard input. [MO6.2] Please note that you would need to use a queue to implement it for full credit. You should add the strings inputted by the user to a queue using the enqueue method. Then you should remove and return "n" strings from the queue using the dequeue method. The nth string that is...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Write a Java program which reads a positive integer from the command line, then displays the...
Write a Java program which reads a positive integer from the command line, then displays the sum of all even values up to and including the value provided, followed by the sum of all odd values up to and including the value provided. validate that the command line argument is an integer greater than 0 to validate the type, you can use Integer.parseInt() with a try/catch for NumberFormatException use one or more for loops to perform the even and odd...
Given the following C function prototype which accepts an integer argument, complete the implementation of the...
Given the following C function prototype which accepts an integer argument, complete the implementation of the function in C language to return the number of 1’s in the binary representation of the number passed. For example if the number is (011100011 ) then the function should return 5 as the number of 1s. Please remember that an integer is 32 bits long. USE the following function int countOnes(int number) Write a full c program that has the header required and...
The program should be able to do the following: In Java accepts one command line parameter....
The program should be able to do the following: In Java accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers...
The Java program should be able to do the following: accepts one command line parameter. The...
The Java program should be able to do the following: accepts one command line parameter. The parameter specifies the path to a text file containing the integers to be sorted. The structure of the file is as follows: There will be multiple lines in the file (number of lines unknown). Each line will contain multiple integers, separated by a single whitespace. reads the integers from the text file in part a into an array of integers. sort the integers in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT