1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
nadya68 [22]
3 years ago
9

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. Genera

te 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (i.e.,int a[5][12];). Use pthread to create n threads to square all 60 array elements in place. 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.

Computers and Technology
1 answer:
victus00 [196]3 years ago
6 0

Answer:

The code is as given below. The output is attached as the attachments

Explanation:

#include <iostream>

#include <cstdlib>

#include <pthread.h>

using namespace std;

#define c 12 // defining number of columns

#define r 5 //defining number of rows

int chunk; //initializing variable chunk

int n=0; //defining variable n as 0

int arry[r][c] ; //initializing the array as 5x12

void Squared(void threadid) {

long td; //initializing the variable td

 

td = (long)threadid; //typecasting threader to long

 

int s = td * chunk; //defining variable s for the start

int e=s+chunk-1;//defining variable e for the end

for(int i=0; i<r; i++){ //initializing for loop for rows

for(int j=0;j<c;j++){ //initializing for loop for columns

int pos = i*c + j; //defining variable

if(pos>=s&&pos<=e)//condition to make sure the values in within the chunk range

{

arry[i][j]=arry[i][j]*arry[i][j]; //squaring the terms

}

}

}

pthread_exit(NULL);

}

int main () {

cin>>n; //taking number n from 2 to 4 inclusive from the user

pthread_t threads[n];

int rc; //initializing variable rc for threads

int i,j,k; //initializing variables i,j,k

//filling array with data

for(i=0;i<r;i++) //for loop for rows

{

for(j=0;j<c;j++)  //for loop for columns

{

arry[i][j]=(rand() % 49) + 1;  //Creating random integers

}

}

//Printing Random Array

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

{

cout<<"\n";

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

{

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

}

}

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

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

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

rc = pthread_create(&threads[i], NULL, Squared, (void *)i); //creating the threads

if (rc) {

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

exit(-1);

}

}

//Printing Result Array

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

{

cout<<"\n";

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

{

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

}

}

pthread_exit(NULL);

}

You might be interested in
A layout with boxes that can be used to make text easier to read."
bulgar [2K]

Answer:

I think the correct answer is (Bullet point)

5 0
3 years ago
What font is the declaration of independence?
Keith_Richards [23]
I believe it is old English text
4 0
4 years ago
1. What does the word “processing” in data pro- cessing mean?
blsea [12.9K]

Answer:

Word processing is the process of creating and editing documents on a computer. It allows a user to create documents that mimic the format and style of a typical typewriter. It requires a computer and word processing software. A printer may also be used to create a physical copy of the document.

Explanation:

6 0
3 years ago
Read 2 more answers
Whats with the bot spamming customer care numbers...kinda annoying when im trying to help people.
Norma-Jean [14]

Answer:

yes

Explanation:

5 0
3 years ago
JAVA QUESTION::
allochka39001 [22]

Answer:

3) 44 10 44

Explanation:

Given data

int [] val = { 3, 10, 44 };

The total number of parameters of given array are 3, so total length of array is also 3.

The indexing of array starts with '0', Therefore the <u>indexes</u> of array with length zero are: {0,1,2}

The value of array at index 0 is = 3

similarly

value at index 1 = 10

value at index 2 = 44

Any value of index 'i' of an array is selected using array[i].

Therefore,

val[0] is selecting the value of array located at index '0'.

val[0] = 3

val[2] is selecting the value of array located at index '2'.

val[2] = 44

Finally,

val[0] = val[2]; is copying the value placed at index 2 (44) to value placed at index 0 (3). Hence, the output would be { 44 10 44}. So 3rd option is correct.

 

8 0
4 years ago
Other questions:
  • The different between a compiler and a translaror
    9·2 answers
  • Variables are: Group of answer choices Operators that perform operations on one or more operands. Symbolic names made up by the
    15·1 answer
  • In the middle of the iteration, how should a team handle requirement changes from the customer? (1 correct answer)
    7·2 answers
  • Which of the following is an acronym? A.RAM B.Every Good Boy Does Fine. C.association D.mnemonic device
    13·1 answer
  • ZeroIt is a function that takes one argument and returns no value. The argument is a pointer to int. The function stores the val
    15·1 answer
  • Which of the following statements about certificates is true?
    14·2 answers
  • Please design a Java GUI application with two JTextField for user to enter the first name and last name. Add two JButtons with a
    9·1 answer
  • Why when I move stuff in Roblox Studio like an object or a Character. It tilts the opposite direction I'm moving it to with the
    6·2 answers
  • What is the correct term for a piece of malware that hides inside a legitimate software program or file from trusted sources
    8·1 answer
  • What does a good résumé help you do?
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!