SlideShare a Scribd company logo
1 of 4
Download to read offline
Modify this code to use multiple threads with the same data
1.Modify the main function to implement a loop that reads 10 integers from the console (user
input) and stores these numbers in a one-dimensional (1D) array (this code will go right after the
comment that says “Add code to perform any needed initialization or to process user input”).
You should use a global array for this.
2.Implement a separatepthread function function for each one of the following operations:
a.Count and print out how many of the entered numbers are negative. This function must be
named countNegatives
b.Calculate and print the average value of all the numbers entered. This function must be named
average
c.Print the numbers in reverse order from the order in which they were entered. This function
must be named reverse
3.Modify the main function to create one pthread for each one of the functions that you
implemented in (3) above (this code will go between the comment that says “TODO: Modify
according to assignment requirements” and the “if (rc)” check).
Compile your program and run it several times. If the output of your program is garbled, you
may need to add a small delay in between creating the next thread.
#include
#include
using namespace std;
void *routineName(void *arg)
{
// TODO: Add code that implements
// the thread's functionality
cout << "Thread is running..." << endl;
return 0;
}
int main()
{
pthread_t id;
int rc;
int ints;
for(int x; x<10; x++)
{
cout << "Enter Integer: " <>
}
rc = pthread_create(&id, NULL, routineName, NULL);
if (rc){
cout << "ERROR; return code from pthread_create() is " << rc << endl;
return -1;
}
pthread_exit(0);
}
Solution
#include
#include
#include
using namespace std;
int ints[10];
//Method to count negatives in the input array
void *countNegatives(void*){
int count = 0;
for(int i=0;i<10;i++){
if(ints[i]<0){
count++;
}
}
cout<<"Number of negatives: "<=0;i--){
cout<> ints[x];
}
//Creating pthread to countNegatives
rc = pthread_create(&t1, NULL, countNegatives, NULL);
if(rc){
cout << "Error:unable to create thread," << rc << endl;
}
//Adding delay
sleep(1);
//Creating pthread to compute average
rc = pthread_create(&t2, NULL, average, NULL);
if(rc){
cout << "Error:unable to create thread," << rc << endl;
}
//Adding delay
sleep(1);
//Creating pthread to print in reverse
rc = pthread_create(&t3, NULL, reverse, NULL);
if(rc){
cout << "Error:unable to create thread," << rc << endl;
}
//Adding delay
sleep(1);
void* status;
//waiting for t1 to join
rc = pthread_join(t1,&status);
if(rc){
cout << "Error:unable to join," << rc << endl;
}
//waiting for t2 to join
rc = pthread_join(t2,&status);
if(rc){
cout << "Error:unable to join," << rc << endl;
}
//waiting for t3 to join
rc = pthread_join(t3,&status);
if(rc){
cout << "Error:unable to join," << rc << endl;
}
pthread_exit(0);
}

More Related Content

Similar to Modify this code to use multiple threads with the same data1.Modif.pdf

Lecture no 3
Lecture no 3Lecture no 3
Lecture no 3hasi071
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfmarketing413921
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsMorteza Mahdilar
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2PVS-Studio
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfjyothimuppasani1
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstackThierry Gayet
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxarjurakibulhasanrrr7
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 

Similar to Modify this code to use multiple threads with the same data1.Modif.pdf (20)

Lecture no 3
Lecture no 3Lecture no 3
Lecture no 3
 
The following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdfThe following code is an implementation of the producer consumer pro.pdf
The following code is an implementation of the producer consumer pro.pdf
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
Array Cont
Array ContArray Cont
Array Cont
 
Router Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditionsRouter Queue Simulation in C++ in MMNN and MM1 conditions
Router Queue Simulation in C++ in MMNN and MM1 conditions
 
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
Analysis of Haiku Operating System (BeOS Family) by PVS-Studio. Part 2
 
CGI.ppt
CGI.pptCGI.ppt
CGI.ppt
 
C++ Homework Help
C++ Homework HelpC++ Homework Help
C++ Homework Help
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Cc code cards
Cc code cardsCc code cards
Cc code cards
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Qe Reference
Qe ReferenceQe Reference
Qe Reference
 
Advanced+pointers
Advanced+pointersAdvanced+pointers
Advanced+pointers
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 

More from mallik3000

Explain how The Capitol Building (in D.C.) is a reflection of Greco-.pdf
Explain how The Capitol Building (in D.C.) is a reflection of Greco-.pdfExplain how The Capitol Building (in D.C.) is a reflection of Greco-.pdf
Explain how The Capitol Building (in D.C.) is a reflection of Greco-.pdfmallik3000
 
Exercise 14-3GURLEY CORPORATION Comparative Condensed Balance Sh.pdf
Exercise 14-3GURLEY CORPORATION Comparative Condensed Balance Sh.pdfExercise 14-3GURLEY CORPORATION Comparative Condensed Balance Sh.pdf
Exercise 14-3GURLEY CORPORATION Comparative Condensed Balance Sh.pdfmallik3000
 
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdfestion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdfmallik3000
 
Discuss the difference between the two levels of moral development. .pdf
Discuss the difference between the two levels of moral development. .pdfDiscuss the difference between the two levels of moral development. .pdf
Discuss the difference between the two levels of moral development. .pdfmallik3000
 
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdf
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdfDiels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdf
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdfmallik3000
 
Create a Balance Sheet to record the following transactions for Tayl.pdf
Create a Balance Sheet to record the following transactions for Tayl.pdfCreate a Balance Sheet to record the following transactions for Tayl.pdf
Create a Balance Sheet to record the following transactions for Tayl.pdfmallik3000
 
Compare Plato and Aristotles philosophies of mathematics and relat.pdf
Compare Plato and Aristotles philosophies of mathematics and relat.pdfCompare Plato and Aristotles philosophies of mathematics and relat.pdf
Compare Plato and Aristotles philosophies of mathematics and relat.pdfmallik3000
 
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdf
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdfChoose one of the evolutions of Critical Incident Technique (CIT) an.pdf
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdfmallik3000
 
Change the creature in this java program to a different one .pdf
Change the creature in this java program to a different one .pdfChange the creature in this java program to a different one .pdf
Change the creature in this java program to a different one .pdfmallik3000
 
Canon Corporation had the following static budget at the beginning o.pdf
Canon Corporation had the following static budget at the beginning o.pdfCanon Corporation had the following static budget at the beginning o.pdf
Canon Corporation had the following static budget at the beginning o.pdfmallik3000
 
Can someone please prove this equation is an identity. Cos^2.pdf
Can someone please prove this equation is an identity. Cos^2.pdfCan someone please prove this equation is an identity. Cos^2.pdf
Can someone please prove this equation is an identity. Cos^2.pdfmallik3000
 
Write a program that finds the max binary tree height. (This is an ex.pdf
Write a program that finds the max binary tree height. (This is an ex.pdfWrite a program that finds the max binary tree height. (This is an ex.pdf
Write a program that finds the max binary tree height. (This is an ex.pdfmallik3000
 
What happens when the JVM encounters a wait () callSolution=.pdf
What happens when the JVM encounters a wait () callSolution=.pdfWhat happens when the JVM encounters a wait () callSolution=.pdf
What happens when the JVM encounters a wait () callSolution=.pdfmallik3000
 
Write a program in c++ that maintains a telephone directory. The Tel.pdf
Write a program in c++ that maintains a telephone directory. The Tel.pdfWrite a program in c++ that maintains a telephone directory. The Tel.pdf
Write a program in c++ that maintains a telephone directory. The Tel.pdfmallik3000
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfUsing the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfmallik3000
 
Why are supplies and inventory not considered plant assetsSolut.pdf
Why are supplies and inventory not considered plant assetsSolut.pdfWhy are supplies and inventory not considered plant assetsSolut.pdf
Why are supplies and inventory not considered plant assetsSolut.pdfmallik3000
 
What is the major purpose of the Federal Reserve System What is the.pdf
What is the major purpose of the Federal Reserve System What is the.pdfWhat is the major purpose of the Federal Reserve System What is the.pdf
What is the major purpose of the Federal Reserve System What is the.pdfmallik3000
 
What is the role of culture in leader development What culture fact.pdf
What is the role of culture in leader development What culture fact.pdfWhat is the role of culture in leader development What culture fact.pdf
What is the role of culture in leader development What culture fact.pdfmallik3000
 
What methods can IT use to make sure its initiatives have the suppor.pdf
What methods can IT use to make sure its initiatives have the suppor.pdfWhat methods can IT use to make sure its initiatives have the suppor.pdf
What methods can IT use to make sure its initiatives have the suppor.pdfmallik3000
 
What is IT infrastructure, and what are the stages and drivers of IT.pdf
What is IT infrastructure, and what are the stages and drivers of IT.pdfWhat is IT infrastructure, and what are the stages and drivers of IT.pdf
What is IT infrastructure, and what are the stages and drivers of IT.pdfmallik3000
 

More from mallik3000 (20)

Explain how The Capitol Building (in D.C.) is a reflection of Greco-.pdf
Explain how The Capitol Building (in D.C.) is a reflection of Greco-.pdfExplain how The Capitol Building (in D.C.) is a reflection of Greco-.pdf
Explain how The Capitol Building (in D.C.) is a reflection of Greco-.pdf
 
Exercise 14-3GURLEY CORPORATION Comparative Condensed Balance Sh.pdf
Exercise 14-3GURLEY CORPORATION Comparative Condensed Balance Sh.pdfExercise 14-3GURLEY CORPORATION Comparative Condensed Balance Sh.pdf
Exercise 14-3GURLEY CORPORATION Comparative Condensed Balance Sh.pdf
 
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdfestion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
estion 5 of 34 Sapling Learning Which is the correct name of the fo.pdf
 
Discuss the difference between the two levels of moral development. .pdf
Discuss the difference between the two levels of moral development. .pdfDiscuss the difference between the two levels of moral development. .pdf
Discuss the difference between the two levels of moral development. .pdf
 
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdf
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdfDiels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdf
Diels-Alder Post-lab questions F17. 1) Why do the methylene protons.pdf
 
Create a Balance Sheet to record the following transactions for Tayl.pdf
Create a Balance Sheet to record the following transactions for Tayl.pdfCreate a Balance Sheet to record the following transactions for Tayl.pdf
Create a Balance Sheet to record the following transactions for Tayl.pdf
 
Compare Plato and Aristotles philosophies of mathematics and relat.pdf
Compare Plato and Aristotles philosophies of mathematics and relat.pdfCompare Plato and Aristotles philosophies of mathematics and relat.pdf
Compare Plato and Aristotles philosophies of mathematics and relat.pdf
 
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdf
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdfChoose one of the evolutions of Critical Incident Technique (CIT) an.pdf
Choose one of the evolutions of Critical Incident Technique (CIT) an.pdf
 
Change the creature in this java program to a different one .pdf
Change the creature in this java program to a different one .pdfChange the creature in this java program to a different one .pdf
Change the creature in this java program to a different one .pdf
 
Canon Corporation had the following static budget at the beginning o.pdf
Canon Corporation had the following static budget at the beginning o.pdfCanon Corporation had the following static budget at the beginning o.pdf
Canon Corporation had the following static budget at the beginning o.pdf
 
Can someone please prove this equation is an identity. Cos^2.pdf
Can someone please prove this equation is an identity. Cos^2.pdfCan someone please prove this equation is an identity. Cos^2.pdf
Can someone please prove this equation is an identity. Cos^2.pdf
 
Write a program that finds the max binary tree height. (This is an ex.pdf
Write a program that finds the max binary tree height. (This is an ex.pdfWrite a program that finds the max binary tree height. (This is an ex.pdf
Write a program that finds the max binary tree height. (This is an ex.pdf
 
What happens when the JVM encounters a wait () callSolution=.pdf
What happens when the JVM encounters a wait () callSolution=.pdfWhat happens when the JVM encounters a wait () callSolution=.pdf
What happens when the JVM encounters a wait () callSolution=.pdf
 
Write a program in c++ that maintains a telephone directory. The Tel.pdf
Write a program in c++ that maintains a telephone directory. The Tel.pdfWrite a program in c++ that maintains a telephone directory. The Tel.pdf
Write a program in c++ that maintains a telephone directory. The Tel.pdf
 
Using the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdfUsing the C++ programming language1. Implement the UnsortedList cl.pdf
Using the C++ programming language1. Implement the UnsortedList cl.pdf
 
Why are supplies and inventory not considered plant assetsSolut.pdf
Why are supplies and inventory not considered plant assetsSolut.pdfWhy are supplies and inventory not considered plant assetsSolut.pdf
Why are supplies and inventory not considered plant assetsSolut.pdf
 
What is the major purpose of the Federal Reserve System What is the.pdf
What is the major purpose of the Federal Reserve System What is the.pdfWhat is the major purpose of the Federal Reserve System What is the.pdf
What is the major purpose of the Federal Reserve System What is the.pdf
 
What is the role of culture in leader development What culture fact.pdf
What is the role of culture in leader development What culture fact.pdfWhat is the role of culture in leader development What culture fact.pdf
What is the role of culture in leader development What culture fact.pdf
 
What methods can IT use to make sure its initiatives have the suppor.pdf
What methods can IT use to make sure its initiatives have the suppor.pdfWhat methods can IT use to make sure its initiatives have the suppor.pdf
What methods can IT use to make sure its initiatives have the suppor.pdf
 
What is IT infrastructure, and what are the stages and drivers of IT.pdf
What is IT infrastructure, and what are the stages and drivers of IT.pdfWhat is IT infrastructure, and what are the stages and drivers of IT.pdf
What is IT infrastructure, and what are the stages and drivers of IT.pdf
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxAnaBeatriceAblay2
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptxENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
ENGLISH5 QUARTER4 MODULE1 WEEK1-3 How Visual and Multimedia Elements.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

Modify this code to use multiple threads with the same data1.Modif.pdf

  • 1. Modify this code to use multiple threads with the same data 1.Modify the main function to implement a loop that reads 10 integers from the console (user input) and stores these numbers in a one-dimensional (1D) array (this code will go right after the comment that says “Add code to perform any needed initialization or to process user input”). You should use a global array for this. 2.Implement a separatepthread function function for each one of the following operations: a.Count and print out how many of the entered numbers are negative. This function must be named countNegatives b.Calculate and print the average value of all the numbers entered. This function must be named average c.Print the numbers in reverse order from the order in which they were entered. This function must be named reverse 3.Modify the main function to create one pthread for each one of the functions that you implemented in (3) above (this code will go between the comment that says “TODO: Modify according to assignment requirements” and the “if (rc)” check). Compile your program and run it several times. If the output of your program is garbled, you may need to add a small delay in between creating the next thread. #include #include using namespace std; void *routineName(void *arg) { // TODO: Add code that implements // the thread's functionality cout << "Thread is running..." << endl; return 0; } int main() { pthread_t id; int rc; int ints; for(int x; x<10; x++)
  • 2. { cout << "Enter Integer: " <> } rc = pthread_create(&id, NULL, routineName, NULL); if (rc){ cout << "ERROR; return code from pthread_create() is " << rc << endl; return -1; } pthread_exit(0); } Solution #include #include #include using namespace std; int ints[10]; //Method to count negatives in the input array void *countNegatives(void*){ int count = 0; for(int i=0;i<10;i++){ if(ints[i]<0){ count++; } } cout<<"Number of negatives: "<=0;i--){ cout<> ints[x]; } //Creating pthread to countNegatives rc = pthread_create(&t1, NULL, countNegatives, NULL); if(rc){ cout << "Error:unable to create thread," << rc << endl;
  • 3. } //Adding delay sleep(1); //Creating pthread to compute average rc = pthread_create(&t2, NULL, average, NULL); if(rc){ cout << "Error:unable to create thread," << rc << endl; } //Adding delay sleep(1); //Creating pthread to print in reverse rc = pthread_create(&t3, NULL, reverse, NULL); if(rc){ cout << "Error:unable to create thread," << rc << endl; } //Adding delay sleep(1); void* status; //waiting for t1 to join rc = pthread_join(t1,&status); if(rc){ cout << "Error:unable to join," << rc << endl; } //waiting for t2 to join rc = pthread_join(t2,&status); if(rc){ cout << "Error:unable to join," << rc << endl; } //waiting for t3 to join rc = pthread_join(t3,&status); if(rc){ cout << "Error:unable to join," << rc << endl; } pthread_exit(0);
  • 4. }