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
URGENT!!!
LenaWriter [7]

Answer:

transaction

Explanation:

8 0
3 years ago
If a friend changes the password on your phone how do you get into it if you dont know the password
kotegsom [21]
Ask your friend to tell you the password
3 0
3 years ago
Assignment 2: Room area
Y_Kistochka [10]

Answer:

a = float(input("Enter Side A: "))

b = float(input("Enter Side B: "))

c = float(input("Enter Side C: "))

d = float(input("Enter Side D: "))

e = float(input("Enter Side E: "))

area1 = 1.0* a * b

area2 = (a - c) * (d - e -b)

area3 = 0.5 * (a - c) * e

print ("Room Area: " + str(area1 + area2 + area3))

Explanation:

happy to help ^3^

5 0
3 years ago
Who is "TheBrain" He doesn't Give Answers, Or Questions, Keeps liking Profiles and.. Has Thousands of followers.. is he a Mod? ​
ss7ja [257]

Nope, he is a BOT not a MOD. He is made to run for this purpose only. He used to give answers in brainly.in but now he is used for deleting useless answers and questions, to welcome a new user and acts as a server bot.

You can also call "TheBrain" as AI

8 0
3 years ago
Select the correct answer.
True [87]

Answer:

office access cars

Explanation:

<em><u>HOPE</u></em><em><u> </u></em><em><u>THIS</u></em><em><u> </u></em><em><u>WILL</u></em><em><u> </u></em><em><u>HELP</u></em><em><u> </u></em><em><u>U</u></em><em><u>,</u></em><em><u> </u></em><em><u>MARK</u></em><em><u> </u></em><em><u>ME</u></em><em><u> </u></em><em><u>AS</u></em><em><u> </u></em><em><u>A</u></em><em><u> </u></em><em><u>BRAINLIST</u></em>

3 0
3 years ago
Other questions:
  • 10 facts about turbines
    11·2 answers
  • How many rows and columns does ms-excel 2007 have???
    10·1 answer
  • Cryptolocker is an example of what type of malware?
    11·1 answer
  • A smart refrigerator can use what to detect when you are running low on milk, and then send a reminder to you on a wireless.
    8·1 answer
  • Amazon SWF is restricts you to use a specific programming language when setting up a work flow
    8·1 answer
  • Use the drop-down menus to complete statements about audio file formats.
    10·1 answer
  • A computer is defined by 4 specific criteri. Select all 4.*
    10·1 answer
  • What does the word collaborative mean?
    13·2 answers
  • Find out about the different technological solutions available for interconnecting LANs to from larger networks such as wide are
    5·1 answer
  • Select the WRONG statement about Slide Transitions.
    5·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!