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 the update separately as 5 by 12 matrices. Hint: it is dangerous to have code to print messages in your thread function(s). Note: if you do not use pthread to divide the update task among the threads, you may get zero points. A sample run of the programs in 1 and 2 is shown below. You do NOT need to submit screen shots. Instead submit your source files.
[kwang@computer][~/work/]$ ./A6p1 3 60
using 3 threads to handle 60 chars.
original random upper case string:
RUIQWCXXBFXVZFJDBWTSIBFIMMJJNRKENUUJZUIABIYANHFQDZINCPVOCGAP
off-by-one lower case string:
svjrxdyycgywagkecxutjcgjnnkkoslfovvkavjbcjzboigreajodqwpdhbq
[kwang@computer][~/work/]$ ./A6p2 4
using 4 threads.
original array:
33 36 9 30 1 29 14 16 7 34 6 18
14 2 34 11 18 11 6 17 34 33 16 35
26 11 17 13 27 39 22 21 24 19 11 25
9 24 29 15 7 35 21 9 25 16 20 3
26 14 19 9 7 24 4 21 34 9 33 22
updated array:
0 3 9 8 1 7 3 5 7 1 6 7
3 2 1 0 7 0 6 6 1 0 5 2
4 0 6 2 5 6 0 10 2 8 0 3
9 2 7 4 7 2 10 9 3 5 9 3
4 3 8 9 7 2 4 10 1 9 0 0
#include <iostream>
#include <cstdlib>
#include <pthread.h>
using namespace std;
#define COLS 12 //column size
#define ROWS 5 //row size
int SIZE_REQ;
int arr[ROWS][COLS] ;
//matrix of sqaures creation using threads
void *SQUAREMATRIX(void *threadid) {
long tid;
tid = (long)threadid;
int start = tid * SIZE_REQ;
int end=start+SIZE_REQ-1;
for(int i=0; i<ROWS; i++){
for(int j=0;j<COLS;j++){
int Pos_REQ = i*COLS + j;
if(Pos_REQ>=start&&Pos_REQ<=end)
{
arr[i][j]=arr[i][j]*arr[i][j];
}
}
}
pthread_exit(NULL);
}
int main ()
{
int n=0;
cin>>n;
if(n<=4 && n>=2)
{
pthread_t threads[n];
int rc;
long i,j;
//loading array with data
for(i=0;i<ROWS;i++)
{
for(j=0;j<COLS;j++)
{
arr[i][j]=(rand() % 49) + 1;
}
}
//displaying Array in random
for(i=0;i<ROWS;i++)
{
cout<<"\n";
for(j=0;j<COLS;j++)
{
cout<<arr[i][j]<<"\t";
}
}
SIZE_REQ = (60 + n - 1) / n; // divide by threads rounded up.
for( i = 0; i < n; i++ ) {
cout << "\nmain() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, SQUAREMATRIX, (void *)i);
if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
//Printing Resulter Array
for(i=0;i<ROWS;i++)
{
cout<<"\n";
for(j=0;j<COLS;j++)
{
cout<<arr[i][j]<<"\t";
}
}
pthread_exit(NULL);
}
else
cout<<"invalid command"; //if input is wrong
}
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]
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...
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...
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...
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...
Use C Programming DESCRIPTION Write a program that: Creates a Dimensional, integer array that holds 8 values Prompts the user to enter 8 integers between 0 and 20 [inclusive) and stores the data in the array Prints the data in array abng with a histogram of the data as shown below. There is no requirement to validate the data. You must use a constant to declare your array and control any loops you need Sample Output: Enter 8 integer values...
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++...
C++ 3. Random modification of all elements in an integer array. a) Initialize an integer array of capacity 5. b) Implement an array output function. c) Output the array to console. d) Implement an offset function that accepts an integer array as input and randomly modifies each element by +/- 5. e) Use the offset function to update the array. f) Output the modified array. Example Output (input in bold) Original: 5 1 4 9 2 Offset: 7 -2 6...
C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand how to traverse a two dimensional array Code and run a program that processes a two dimensional array Instructions: A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value. You are to code a program to determine...
In C++ Make a program that takes a positive integer 'n' and write an n x n matrix whose entries are listed as snail Example: (See picture) for n=6 Ej. Para n-6 2 2 2 20 21 22 23 24 7 19 32 33 34 258 18 31 36 35 26 9 17 30 29 28 27 10 16 15 14 13 12 11