Question

In: Computer Science

I have the following code #include <stdio.h> #include<string.h> #define BUFLEN 128 typedef struct { int numPhrases;...

I have the following code

#include <stdio.h>
#include<string.h>
#define BUFLEN 128 
typedef struct {
    int numPhrases;
}SyncInfo;
char buffer[BUFLEN] ;
char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"};
char localBuffer[BUFLEN];
int allVowelsPresent;
void *checker(void *param) {
int  a=0, e=0, i=0, o = 0, u= 0 ;
int* n = (int*)param; // typecasting a void* to int* 
//printf("%d\n",*n);
for (int q=0; q< (*n); ++q) { // dereferencing to get the value in int*
strcpy(localBuffer, buffer);
int length=strlen(localBuffer); // strlen is used to determine the length of localBuffer
for(int j=0 ; j<length;j++) // iterating over the whole length of localBuffer and checking if vowel present
{
    if(localBuffer[j]=='a') a=1;
    else if(localBuffer[j]=='e') e=1;
    else if(localBuffer[j]=='i') i=1;
    else if(localBuffer[j]=='o') o=1;
    else if(localBuffer[j]=='u') u=1;
}
if(a&&e&&i&&o&&u)  // if all are present ie none of the value of a, e, i, o ,u is 0 then all vowels are present 
allVowelsPresent = 1 ;
else
allVowelsPresent = 0 ;
}
}
int main() {
SyncInfo syncinfo;
syncinfo.numPhrases = 8;
SyncInfo* p = &syncinfo; // defining pointer of type SyncInfo pointing to syncinfo
void* param = &p->numPhrases; // a void pointer as we have to pass void* in checker function 
for (int i=0; i<syncinfo.numPhrases; ++i) {
strcpy(buffer, phrases[i]);
checker(param); // main() calling the checker function
printf("result of checking ’%s’: %d\n", phrases[i], allVowelsPresent) ;
}
}

In this code I didn't create a separate thread to do the checking , may someone implement and modify this. Also My checker function should have Syncinfo *s =(Syncinfo*) param please add that modification as well . The code works perfectly fine , just need to make some modification .

Solutions

Expert Solution

#include <stdio.h>
#include<string.h>
#include<pthread.h>
#define BUFLEN 128
typedef struct {
int numPhrases;
}SyncInfo;

pthread_mutex_t lock;//mutex lock handle declaration
pthread_t thread_id;//checker thread id declaration

char buffer[BUFLEN] ;
char *phrases[] = {"educated", "educated cat", "educated lion", "serious person" , "serious panda","curious student","curious art student", "obnoxious web developer"};
char localBuffer[BUFLEN];
int allVowelsPresent;

void *checker(void *param) {
pthread_mutex_lock(&lock); //it locks the mutex_lock
int a=0, e=0, i=0, o = 0, u= 0 ;
int* n = (int*)param; // typecasting a void* to int*
//printf("%d\n",*n);
for (int q=0; q< (*n); ++q) { // dereferencing to get the value in int*
strcpy(localBuffer, buffer);
int length=strlen(localBuffer); // strlen is used to determine the length of localBuffer

for(int j=0 ; j<length;j++) // iterating over the whole length of localBuffer and checking if vowel present
{
if(localBuffer[j]=='a') a=1;
else if(localBuffer[j]=='e') e=1;
else if(localBuffer[j]=='i') i=1;
else if(localBuffer[j]=='o') o=1;
else if(localBuffer[j]=='u') u=1;
}
if(a&&e&&i&&o&&u) // if all are present ie none of the value of a, e, i, o ,u is 0 then all vowels are present
allVowelsPresent = 1 ;
else
allVowelsPresent = 0 ;

}
printf("checked '%s' : result is %d\n",localBuffer,allVowelsPresent);
pthread_mutex_unlock(&lock); //it unlocks the mutex lock
}

int main() {

if (pthread_mutex_init(&lock, NULL) != 0) { //initializing mutex
printf("\n mutex init has failed\n");
return 1;
}
SyncInfo syncinfo;
syncinfo.numPhrases = 8;
SyncInfo* p = &syncinfo; // defining pointer of type SyncInfo pointing to syncinfo
void* param = &p->numPhrases; // a void pointer as we have to pass void* in checker function
for (int i=0; i<syncinfo.numPhrases; ++i) {
strcpy(buffer, phrases[i]);
pthread_create(&thread_id, NULL, checker ,param); //It creates checker thread
pthread_join(thread_id, NULL); //It deploys the thread
pthread_mutex_lock(&lock);
printf("result of checking %s %d\n", phrases[i], allVowelsPresent) ;
pthread_mutex_unlock(&lock);
}// main() calling the checker function

}

Related Solutions

#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates {...
#include <stdio.h> typedef struct Coordinates { int x; int y; } Coordinate; typedef union uCoordinates { int x; int y; } uCoordinate; // TODO - Populate 4 different Coordinates with any numbers between 1 and 99 for x & y values // using coordinate1, coordinate2, coordinate3, & coordinate4 as Coordinate names // TODO - Print to screen the x & y values of each coordinate // TODO - Replace the following with your name: Ethin Svoboda int main() { //...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...
please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating(); int main() { // declaring variables int size = 5; int jerseyNo[size]; int rating[size]; int i = 0, jno, rate; char option; /* Getting the inputs entered by the user * and populate the values into arrays */ for (i = 0; i < size; i++) { printf("Enter player %d's jersey number:", i + 1); jerseyNo[i] = getValidJerseyNumber(); printf("Enter player %d's rating:\n", i +...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef...
I need the following C code converted to java or C++ #include <stdio.h> #include <stdlib.h> typedef struct node { struct node *left; struct node *right; long data; long leftSize; } node; void btreeInsert(node *new, node **rootptr) { node *parent = NULL, *cursor; /* Find parent */ cursor = *rootptr; while (cursor != NULL) { parent = cursor; if (new->data < cursor->data) { cursor->leftSize += 1; cursor = cursor->left; } else { cursor = cursor->right; } } /* Insert node below...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr;...
#include #include #include int reverse(int); // Stack ADT Type Defintions typedef struct node { void* dataPtr; struct node* link; } STACK_NODE; typedef struct { int count; STACK_NODE* top; } STACK; /* =============== createStack ============== This algorithm creates an empty stack. Pre Nothing Post Returns pointer to a null stack -or- NULL if overflow */ STACK* createStack(void) { // Local Definitions STACK* stack; // Statements stack = (STACK*)malloc(sizeof(STACK)); if (stack) { stack->count = 0; stack->top = NULL; } // if return...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode*...
Using the following definitions for a binary tree, typedef struct bintreenode {     int data;     struct bintreenode* left;     struct bintreenode* right; } btreenode; // Used for a node in the queue. typedef struct node {     btreenode* nodePtr;     struct node* next; } node; // Used to represent the queue efficiently. typedef struct queue {     node* front;     node* back; } queue; Implement the following functions: void bfs(btreenode* root) // Prints a breadth first search traversal of the binary search tree rooted at root....
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b...
construct c program flow chart #include <stdio.h> #include <math.h> #include <string.h> #define num 6 #define b 6 #define t 6 double bmical(double w, double h){ double o; double bmi; o = pow(h,2); bmi = w/o; return bmi; } double maxheartrate(int num1, int age){ double mhr; mhr = num1 - age; return mhr; } double heartratereserve(double mhr, double rhr){ double hrr; hrr = mhr - rhr; return hrr; } double minrange(int hrr, int rhr){ double mirt; mirt = (hrr * 0.7)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT