Write a Cor C++ program A6p3.c (or A6p3.cpp) that accepts one command line argum
ID: 3732746 • Letter: W
Question
Write a Cor C++ program A6p3.c (or A6p3.cpp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate a string of 60 random lower case English characters and store them in a char array. Use pthread to create n threads to convert the string into all upper case. (You may call the toupper and tolower functions declared in ctvpe.h). You should divide this conversion task among the n threads as evenly as possible. Print out both the lower case and upper case version of the string in separate lines. (That is, 60 lower case English characters in one line and another 60 corresponding upper case English characters on another line.) (35 points) (Note: if you do not use pthread to divide the conversion task among the threads, you will get zero points.)Explanation / Answer
#include <stdio.h>
int main()
{
int n;
char arr=[], c, str=[];
1. //Take input n between 2 and 4.
printf("Enter any number between 2 and 4 : ");
scanf("%d%d", &n);
2. // generate string of 60 random lowercase letters
for(var i=0; i<60; i++){
c = rand() % 26 + 97 + i; // c contains a single character
arr[] = strcat("", c);
}
// print string in lower case
printf("Lowercase string : %s",arr);
3. // create n threads
thread = malloc(sizeof(pthread_t)*n);
if (thread == NULL)
{
printf("out of memory ");
exit(1);
}
for (i = 0; i < n; i++) // creating n threads
{
if (pthread_create ( &thread[i], NULL, (void*) &info, NULL ) != 0)
{
printf("Error creating thread. ");
exit(1);
}
}
void info(){
printf("creating threads ..");
// convert string to uppercase
for(i=0; arr[i]!=''; i++)
{
// convert char to uppercase if it is in lower case
if(str[i]>='a' && str[i]<='z')
{
str[i] = str[i] - 32;
}
}
// print string in upper case
printf("Uppercase string : %s",str);
return 0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.