Question

In: Computer Science

PROGRAM DESCRIPTION: In this assignment, you are provided with working code that does the following: 1....

PROGRAM DESCRIPTION:
In this assignment, you are provided with working code that does the following:
1. You input a sentence (containing no more than 50 characters).
2. The program will read the sentence and put it into an array of characters.
3. Then, it creates one thread for each character in the sentence.
4. The goal of the program is to capitalize on each letter that has an odd index.
The given program actually does this but lacks the synchronization of the threads, so
the output is not correct. You will need to provide the synchronization using mutex
locks. Specifically, you are to (1) declare the mutex lock, (2) initialize the mutex lock, (3)
lock and unlock the mutex lock at an appropriate location that results in the code
working as expected, and (4) destroy the mutex lock. Be sure to place the mutex locks
so that your program works correctly every time. Do not remove code or functions – you
are to add the synchronization pieces only.
When compiling using the GNU C compiler, be sure to include the –lpthread flag
option.
SAMPLE OUTPUT (user input shown in bold green):
$ ./a.out
Please enter a phrase (less than 50 characters): when all else
fails, read the instructions
The original sentence is: when all else fails, read the instructions
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [3]: N
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [0]: w
The new sentence is [9]: E
The new sentence is [12]: e
The new sentence is [14]: f
The new sentence is [15]: A
The new sentence is [16]: i
2
The new sentence is [17]: L
The new sentence is [17]: L
The new sentence is [19]: ,
The new sentence is [20]:
The new sentence is [21]: R
The new sentence is [22]: e
The new sentence is [23]: A
The new sentence is [23]: A
The new sentence is [25]:
The new sentence is [26]: t
The new sentence is [27]: H
The new sentence is [27]: H
The new sentence is [27]: H
The new sentence is [29]:
The new sentence is [31]: N
The new sentence is [30]: i
The new sentence is [31]: N
The new sentence is [32]: s
The new sentence is [35]: U
The new sentence is [34]: r
The new sentence is [35]: U
The new sentence is [36]: c
The new sentence is [37]: T
The new sentence is [39]: O
The new sentence is [41]: S
The new sentence is [41]: S
The problem is that the output should look something like:
$ ./a.out
Please enter a phrase (less than 50 characters): when all else
fails, read the instructions
The original sentence is: when all else fails, read the instructions
The new sentence is [0]: w
The new sentence is [1]: H
The new sentence is [2]: e
The new sentence is [3]: N
The new sentence is [4]:
The new sentence is [5]: A
The new sentence is [6]: l
The new sentence is [7]: L
The new sentence is [8]:
The new sentence is [9]: E
The new sentence is [10]: l
The new sentence is [11]: S
The new sentence is [12]: e
The new sentence is [13]:
The new sentence is [14]: f
The new sentence is [15]: A
The new sentence is [16]: i
The new sentence is [17]: L
3
The new sentence is [18]: s
The new sentence is [19]: ,
The new sentence is [20]:
The new sentence is [21]: R
The new sentence is [22]: e
The new sentence is [23]: A
The new sentence is [24]: d
The new sentence is [25]:
The new sentence is [26]: t
The new sentence is [27]: H
The new sentence is [28]: e
The new sentence is [29]:
The new sentence is [30]: i
The new sentence is [31]: N
The new sentence is [32]: s
The new sentence is [33]: T
The new sentence is [34]: r
The new sentence is [35]: U
The new sentence is [36]: c
The new sentence is [37]: T
The new sentence is [38]: i
The new sentence is [39]: O
The new sentence is [40]: n
The new sentence is [41]: S
The new sentence is [42]:
REQUIREMENTS:
• No comments are required for this recitation assignment, except for your name at
the top of each program.
• Your program should be named “rec07.c”, without the quotes.
• Your program will be graded based largely on whether it works correctly on the
CSE machines (e.g., cse01, cse02, …, cse06), so you should make sure that
your program compiles and runs on a CSE machine.
• Although this assignment is to be submitted individually (i.e., each student will
submit his/her own source code), you may receive assistance from your TA and
even other classmates. Please remember that you are ultimately responsible for
learning and comprehending this material as the recitation assignments are given
in preparation for the minor assignments, which must be completed individually.
• Please do not share this assignment or your work with other students to allow
them the opportunity to benefit from this exercise and learn this material.
SUBMISSION:
• You will electronically submit your program to the Recitation 7 dropbox in
Canvas by the due date and time. No late recitation assignments will be
accepted.

CODE

#include <stdio.h>
#include <string.h>
#include <pthread.h>

#define SIZE 50

char sentence[2000];
int ind = 0;

char convertUppercase(char lower)
{
//Converts lowercase un uppercase
if ((lower > 96) && (lower < 123))
{
return (lower - 32);
}
else
{
return lower;
}
}

void printChar()
{
//prints the converted sentence
printf("The new sentence is [%d]: \t%c\n", ind, sentence[ind]);
ind++;
}

void *convertMessage(void *ptr)
{
// Function that each threads initiates its execution
if (ind % 2)
{
sentence[ind] = convertUppercase(sentence[ind]);
}
  
printChar();

return 0;
}

int main()
{
int i;
char buffer[SIZE];
char *p;
pthread_t ts[SIZE]; // define up to 50 threads

printf("Please enter a phrase (less than 50 characters): ");

if (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
if ((p = strchr(buffer, '\n')) != NULL)
{
*p = '\0';
}
}

strcpy(sentence, buffer); // copy string to char array
  
printf("The original sentence is: \t %s\n", sentence);

// create one thread for each character on the input word
for (i = 0; i < strlen(buffer) + 1; ++i)
{
pthread_create(&ts[i], NULL, convertMessage, NULL);
}
  
// we wait until all threads finish execution
for (i = 0; i < strlen(buffer); i++)
{
pthread_join(ts[i], NULL);
}
  
printf("\n");
  
return 0;
}

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C code with comments and screenshot for easy understanding :)

CODE-

#include <stdio.h>
#include <string.h>
#include <pthread.h>
#define SIZE 50
char sentence[2000];
int ind = 0;
pthread_t tid[50];
int counter;
pthread_mutex_t lock;
char convertUppercase(char lower)
{
//Converts lowercase un uppercase
if ((lower > 96) && (lower < 123))
{
return (lower - 32);
}
else
{
return lower;
}
}

void printChar()
{
//prints the converted sentence
printf("The new sentence is [%d]: \t%c\n", ind, sentence[ind]);
ind++;
}

void convertMessage(void ptr)
{
pthread_mutex_lock(&lock);
// Function that each threads initiates its execution
if (ind % 2)
{
sentence[ind] = convertUppercase(sentence[ind]);
}
printChar();
pthread_mutex_unlock(&lock);
return 0;
}

int main()
{
int i;
char buffer[SIZE];
char *p;
pthread_t ts[SIZE]; // define up to 50 threads
printf("Please enter a phrase (less than 50 characters): ");
if (fgets(buffer, sizeof(buffer), stdin) != NULL)
{
if ((p = strchr(buffer, '\n')) != NULL)
{
*p = '\0';
}
}
strcpy(sentence, buffer); // copy string to char array
printf("The original sentence is: \t %s\n", sentence);
// create one thread for each character on the input word
for (i = 0; i < strlen(buffer) + 1; ++i)
{
pthread_create(&ts[i], NULL, convertMessage, NULL);
}
// we wait until all threads finish execution
for (i = 0; i < strlen(buffer); i++)
{
pthread_join(ts[i], NULL);
}
printf("\n");
pthread_mutex_destroy(&lock);
return 0;
}

OUTPUT :

IF YOU HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


Related Solutions

BEFORE YOU START: Before working on this assignment you should have completed Assignment 1. PROGRAM STATEMENT...
BEFORE YOU START: Before working on this assignment you should have completed Assignment 1. PROGRAM STATEMENT AND REQUIREMENTS: You will implement in Java the WIFI troubleshooting program you created in Assignment 1 with the following additional conditions: At the end of the program, you will ask if the client wants to purchase a regular router for $50 or a super high-speed router for $200, adding the cost to the total amount the client needs to pay. Once your program calculates...
Using the provided Java program below, complete the code to do the following. You may need...
Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, and write them in reverse order to an output file. 1. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. For example, assume your package name is FuAssign5 and your main class name is FuAssignment5, and your executable files are in “C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin”. The...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
Description This assignment is a modification of the last assignment. In this assignment, you will input...
Description This assignment is a modification of the last assignment. In this assignment, you will input from a file called in.txt and output to a file called out.txt. Otherwise, the assignment will behave the same way as the last assignment. For doing this assignment, the statement doing input and output will be changed. Otherwise, the code will mostly remain unchanged. Creating Text IO Files In Eclipse, using the file menu, create the input file in.txt in project folder (not in...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays...
Assignment Description: Write a MIPS assembly language program that adds the following two integers and displays the sum and the difference. In the .data section, define two variables num1 and num2 both words. Initialize num1 to 92413 10 and num2 to D4B 16 (use 0xD4B to initialize, Note that D4B is a hexadecimal number). Your main procedure/function should load the values of num1 and num2 into two temporary registers, and display them on the console window. Then add the values...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then,...
Description of the Assignment: Write a Python program to (a) create a new empty stack. Then, (b) add items onto the stack. (c) Print the stack contents. (d) Remove an item off the stack, and (e) print the removed item. At the end, (f) print the new stack contents: Please use the following comments in your python script: # ************************************************* # COP3375 # Student's full name ( <<< your name goes here) # Week 8: Assignment 1 # ************************************************* #...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT