Question
  1. Write a C or C++ program A6pc(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). Note: if you do not use pthread to divide the conversion task among the threads, you may get zero points.
  2. 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. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. 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. Note: if you do not use pthread to divide the update task among the threads, you may get zero points

The output of the programs should look like this

. [kwang@talon-transfer-2.local)(~/work/temp/testc/A6new] $ A6p1 2 using 2 threads . original upper case string: CAVZTWKMOQUNAF

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

############ part 1

#include<stdio.h>
#include<iostream>
#include<stdlib.h> //for rand()
#include <pthread.h> //for pthread
#include<time.h>
using namespace std;


char arr[60];
int mark=0;
int size=0;

void swap(int start, int endd)
{
int ii;
int j;
for(ii=start;ii<=endd;ii++)
{
j=int(arr[ii]);
j=90-(j-65);
arr[ii]=char(j);
}
}
void *fun(void *thread_id)
{

swap(mark,mark+size-1);
mark=mark+size;

pthread_exit(NULL);
}

int main()
{
int n,i,num;
cout << "Enter any number between 2 and 6 inclusive: ";
cin>>n;
if(n<2 || n>6)
{
cout<<"Invalid Input";
exit(0);
}
cout<<"Number of threads used: "<<n;
cout<<"\n";
size=60/n; // number of elements of array "char[arr]" to be handled by each thread
srand(time(0));
for(i=0;i<60;i++)
{
num=(rand() % 25)+65; // ASCII value of 'A' to 'Z' is 65 to 90
arr[i]=char(num);
}

cout<<"Original Array: ";
for(i=0;i<60;i++)
{
cout<<arr[i]<<" ";

}
pthread_t thread[n];
int x;
for( i = 0; i < n; i++ )
{
x = pthread_create(&thread[i], NULL, fun, (void *)i);

if (x)
{
cout << "Error: Thread could not be created," << x << endl;
exit(-1);
}
}

cout<<"\n";
cout<<"\n";
cout<<"Modified Array: ";
for(i=0;i<60;i++)
{
cout<<arr[i]<<" ";

}
pthread_exit(NULL);

return 0;
}

############################### part 2

#include<stdio.h>
#include<iostream>
#include<stdlib.h> //for rand()
#include <pthread.h> //for pthread
#include<time.h>
using namespace std;


int arr[5][12];
int arr2[60];
int mark=0;
int size=0;

void fun2(int start, int endd)
{
int ii;
int j;
for(ii=start;ii<=endd;ii++)
{
arr2[ii]=arr2[ii]*arr2[ii];
}
}
void *fun(void *thread_id)
{

fun2(mark,mark+size-1);
mark=mark+size;

pthread_exit(NULL);
}

int main()
{
int n,i,y,num,a,b;
cout << "Enter any number between 2 and 4 inclusive: ";
cin>>n;
if(n<2 || n>4)
{
cout<<"Invalid Input";
exit(0);
}
size=60/n;
srand(time(0));
for(i=0;i<60;i++)
{
num=(rand() % 49); // Random number generation b/w 1-49
a=int(i/12);
b=i-(12*a);
arr[a][b]=num;
arr2[i]=num;
}

cout<<"Original Array: ";
for(i=0;i<6;i++)
{
for(y=0;y<12;y++)
{
cout<<arr[i][y]<<" ";
}
}

pthread_t thread[n];
int x;
for( i = 0; i < n; i++ )
{
x = pthread_create(&thread[i], NULL, fun, (void *)i);

if (x)
{
cout << "Error: Thread could not be created," << x << endl;
exit(-1);
}
}

cout<<"\n";
cout<<"\n";
for(i=0;i<60;i++)
{

a=int(i/12);
b=i-(12*a);
arr[a][b]=arr2[i];
}

cout<<"Modified Array: ";
for(i=0;i<6;i++)
{
for(y=0;y<12;y++)
{
cout<<arr[i][y]<<" ";
}
}
pthread_exit(NULL);

return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C or C++ program A6pc(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusiv...
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