Question

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

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 before and after conversion on two separate lines. Hint: it is dangerous to have code to print messages in your thread function(s). You may refer to testThread.c, testThread2.c, pthread_ex1.c, pthread_ex2.c for examples. Note: if you do not use pthread to divide the conversion task among the threads, you may get zero points.


Has to contain two line arguments, not just one.

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

/ CPP Program to find sum of array

#include <iostream>

#include <pthread.h>

#include <cstdlib>

#include <stdio.h>

// size of array

#define MAX 60

using namespace std;


char a[MAX];

int part = 0;

int MAX_THREAD;

void *convert_to_lowercase(void *arg)

{

   

   // Each thread will convert 1/MAX_THREAD part of the array

   int thread_part = part++;

   

   for (int i = thread_part *(MAX / MAX_THREAD); i < (thread_part + 1) *(MAX / MAX_THREAD); i++)

      {

         //if the character is Z then it should be converted to a

         if (a[i] == 90)

            a[i] = 97;

         else

            a[i] = a[i] + 33;

      }

}


// Driver Code

int main(int argc, char **argv)

{

   if (argc == 2)

      sscanf(argv[1], "%d", &MAX_THREAD);

   else

      {

         cout << "The number of arguments is invalid" << endl;

         return -1;

      }

   if (MAX_THREAD < 3 || MAX_THREAD > 5)

      {

         cout << "The number of threads is invalid" << endl;

         return -1;

      }

   for (int i = 0; i < MAX; i++)

      //create a string of random upper case characters and storing it in an char array

   a[i] = (char)(64 + rand() % 26 + 1);

   cout << "The String before conversion is: " << endl;

   //print the string with random characters

   for (int i = 0; i < MAX; i++)

      cout << a[i];

   cout << endl;

   pthread_t threads[MAX_THREAD];

   

   // Creating 4 threads

   for (int i = 0; i < MAX_THREAD; i++)

      pthread_create(&threads[i], NULL, convert_to_lowercase, (void*) NULL);

   

   // joining 4 threads i.e. waiting for all 4 threads to complete

   for (int i = 0; i < MAX_THREAD; i++)

      pthread_join(threads[i], NULL);

   cout << "The String after conversion is: " << endl;

   for (int i = 0; i < MAX; i++)

      cout << a[i];

   cout << endl;

   return 0;

}


answered by: codegates
Add a comment
Know the answer?
Add Answer to:
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
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