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

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.
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...
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...
Question 2. Write a MARIE program that accepts an integer from the user, and if it...
Question 2. Write a MARIE program that accepts an integer from the user, and if it is a prime number the program will output 1, otherwise, the program will output 0. Examples: If the user input is 17, the output would be 1 If the user input is 2, the output would be 1 If the user input is 15, the output would be 0 If the user input is -2, the output would be 0 You should write and...
Write a C++ program that accepts a single integer value entered by user. If the value...
Write a C++ program that accepts a single integer value entered by user. If the value entered is less than one the program prints nothing. If the user enters a positive integer n. The program prints n x n box drawn with * characters. If the user enters 1 , for example the program prints *. If the user enter a 2, it prints ** ** that is , a 2x2 box of * symbols.
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT