Question

Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusi...

Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusive. Generate a string of 60 random upper case English characters and store them somewhere (e.g. in a char array). Use pthread to create n threads to convert the string into a complementary string (‘A’<->’Z’, ‘B’<->’Y’, ‘C’<->’X’, etc). You should divide this conversion task among the n threads as evenly as possible. Print out the string both before and after conversion on two separate lines. Hint: it is dangerous to have printing code in your thread function(s). You may refer to testThread.c, testThread2.c, pthread_ex1.c, pthread_ex2.c for examples. Note: Must use pthread to divide the conversion task among the threads.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <pthread.h>

using namespace std;
static int a[5][12];
void alterArray(void argument){
for(int i=0;i<5;i++)
{
for(int j=0;j<12;j++)
{
if(a[i][j]%2==0)
{
a[i][j]=a[i][j]+1;
}
else
{
a[i][j]=a[i][j]-1;
}

}

}
}

int main()
{
int n;

cout << "Enter no between 2 and 4" << endl;
cin>> n;
for(int i=0;i<5;i++)
{
for(int j=0;j<12;j++)
{
a[i][j]=rand() % 100;
cout <<a[i][j]<<"\t";
}
cout<<"\n";
}

pthread_t thread1, thread2, thread3, thread4;
int i1,i2,i3,i4;
switch(n)
{
case 2: i1 = pthread_create( &thread1, NULL, alterArray, (void*) "thread 1");
i2 = pthread_create( &thread2, NULL, alterArray, (void*) "thread 2");
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
break;
case 3: i1 = pthread_create( &thread1, NULL, alterArray, (void*) "thread 1");
i2 = pthread_create( &thread2, NULL, alterArray, (void*) "thread 2");
i3 = pthread_create( &thread3, NULL, alterArray, (void*) "thread 3");
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
pthread_join(thread3,NULL);
break;
case 4: i1 = pthread_create( &thread1, NULL, alterArray, (void*) "thread 1");
i2 = pthread_create( &thread2, NULL, alterArray, (void*) "thread 2");
i3 = pthread_create( &thread3, NULL, alterArray, (void*) "thread 3");
i4 = pthread_create( &thread4, NULL, alterArray, (void*) "thread 4");
pthread_join(thread1,NULL);
pthread_join(thread2,NULL);
pthread_join(thread3,NULL);
pthread_join(thread4,NULL);
break;
}


for(int i=0;i<5;i++)
{
for(int j=0;j<12;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
return 0;
}

input Enter no between 2 and 4 3 77 83 86 15 93 35 86 92 49 21 6 2 27 90 59 63 26 40 26 72 36 11 68 6 7 29 82 30 62 23 67 35

Add a comment
Know the answer?
Add Answer to:
Write a C or C++ program A6p1.c(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT