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
Each pixel includes 3 colors; Red, Green and Blue. Each color uses 8 bits to store it's intensity. How many Bytes are needed for
PtichkaEL [24]

Answer: 3 bytes

Explanation:

A Pixel uses 3 colors and each of these colors uses 8 bits to store their intensity.

One pixel will therefore have:

= 8 * 3

= 24 bits

1 byte = 8 bits

24 bits will therefore be:

= 24/8

= 3 bytes

6 0
3 years ago
What is the largest place value in a 12 bit binary number?
azamat
The largest binary number that can be expressed with 12 bits is all 1s, which means 1 followed by 1 followed by 1, up to 12 times.
6 0
3 years ago
In the context of intentional security threats, _____ can erase data and wreak havoc on computers and networks but do not replic
mihalych1998 [28]

Answer:

The answer is "Option D".

Explanation:

Trojan program is a malicious code that is also known as a virus. It is a rootkit, that aims to infect the computer to hide or obscure an object. These programs are mainly used to download additional content, such as additional pieces of malware, to the infected computer, and other options are wrong that can be defined as follows:

  • In option A, It is not correct, this option infects the system.
  • In option B, It is used for infects another computer that's why it is incorrect.
  • In option C, It is incorrect because it is used in cybersecurity.  
5 0
3 years ago
If you got band from a local library, what would be the reason for it besides shouting and talking? You're answer has to fit you
VLD [36.1K]

Answer:

I would get a band from the library because I check out ten books and returned them in a week. I also would just be in the library all the time and they would get frustrated. That or I would spend hours talking to a librarian about my favorite books and they would get fed up with me.

4 0
3 years ago
Read 2 more answers
jane feels listening to music at night helps her to sleep. should jane listen to music the night before a test ​
Pavel [41]

If it help her sleep than yes! Sleep helps you to stay focused during a test.

7 0
3 years ago
Other questions:
  • One disadvantage of using the styles feature of a word processor is that you cannot modify existing styles. true or false
    14·1 answer
  • _____ is a feature that records the changes you make to a document.
    12·1 answer
  • A display that is thin, flexible, light, and easy to read in all types of light is
    9·2 answers
  • What is the advantage of using a high-level language over machine language for writing computer programs?
    15·2 answers
  • Use BlueJ to write a program that reads a sequence of data for several car objects from an input file. It stores the data in an
    5·1 answer
  • Which is not a key factor a programmer uses in selecting the language for a project?
    9·1 answer
  • Which kinds of Internet content do you think you need permission to use? Check all that apply.
    8·2 answers
  • Choose a different well-known company that you know of, and describe its direct and indirect competitors. Describe at least 2 di
    9·2 answers
  • Study the original and changed passages.
    8·2 answers
  • One advantage of a PAN​
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!