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.
#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;
}
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...
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...
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...
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. 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 or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the fork function to produce two processes. In the child process print out the lower case version of the string. In the parent process print out the reversed upper case version of the string (e.g. child: “abc”; parent: ”CBA”). You may call the toupper and tolower functions in the header <ctype.h> if you wish. Specify in the output whether the parent or child process is...
From previous homework you are already familiar with the math function f defined on positive integers as f(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Given any integer var, iteratively applying this function f allows you to produce a list of integers starting from var and ending with 1. For example, when var is 6, this list of integers is 6,3,5,8,4,2,1, which has a length of 7 because this list contains 7 integers (call this list the Collatz list for 6). Write a C or C++...
Write a bash script that accepts one command line argument 'a' which is an integer between 1 and 50 inclusive. It should output a list of integers from 'a' and ending with '1' according to the following iteration rule. ( fx = x/2 if x is even; fx=3x+1 if x is odd )
Write a bash script question.sh that accepts one command line argument which is supposed to be a positive integer n. The script should print all odd integers from 1 through n. Write a C or C++ program question.c(pp) to read from stdin as many integers as there are available and then print the squares of all these integers. You should test your code by “./question.sh <n> | ./question” assuming the compiled C/C++ program is question. See the following for a...
Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits