The output of the programs should look like this
.
############ 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;
}
Write a C or C++ program A6pc(pp) that accepts one command line argument which is an integer n between 2 and 6 inclusiv...
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...
1. 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 39 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 convert all 60 array elements modulo 11 (i.e. take the remainder after division by 11) in place. You should divide this update task among the n threads as evenly as possible. Print the array both before and after...
Write a C++ program 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...
1. (50 pts) 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...
Write a C or C++ program A6p1.c[pp] that accepts two command line arguments n and m where n is an integer between 2 and 6 inclusive and m can be assumed to be a multiple of 60 (e.g. 60,120,etc). Generate a string of m 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 of m chars in place into an off-by-one lower case version (i.e. ‘A’→’b’, ‘B’→’c’, ‘C’→’d’,…, ‘Y’→’z’, ‘Z’→’a’). You should divide this conversion task among the n threads as evenly as possible. Print out the string both...