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 <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; };...
#include <stdio.h> #include <string.h> #include<stdlib.h> #include<conio.h> struct Bank_Account_Holder { int account_no; char name[80]; int balance; }; int n; void accept(struct Bank_Account_Holder[], int); void display(struct Bank_Account_Holder[], int); void save(struct Bank_Account_Holder[], int); void load(struct Bank_Account_Holder[], int); int search(struct Bank_Account_Holder[], int, int); void deposit(struct Bank_Account_Holder[], int, int, int); void withdraw(struct Bank_Account_Holder[], int, int, int); int lowBalenquiry(int,int); void main(void) { clrscr(); struct Bank_Account_Holder data[20]; int choice, account_no, amount, index; printf("NHU Banking System\n\n"); printf("Enter the count of records: "); scanf("%d", &n); accept(data, n); do {...
#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...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i =...
#include <stdlib.h> #include <stdio.h> #include <string.h> void clrScreen(int lines){     int i = 0;     for( i = 0; i < lines; ++i ){         printf("\n");     }     return; } void printRules(void){     printf("\t|*~*~*~*~*~*~*~*~*~ How to Play ~*~*~*~*~*~*~*~*~*~|\n");     printf("\t|   This is a 2 player game. Player 1 enters the   |\n");     printf("\t|   word player 2 has to guess. Player 2 gets a    |\n");     printf("\t|   number of guesses equal to twice the number    |\n");     printf("\t|   of characters. EX: If the word is 'example'    |\n");     printf("\t|   player 2 gets 14 guesses.                      |\n");     printf("\t|*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~|\n");     clrScreen(10);     return; } //------------------------------------------------------------------------------------------------------------ /*...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*);...
#include<stdio.h> #include<stdlib.h> struct listNode{ int data; struct listNode *nextptr; }; typedef struct listNode node; void insert(node*); void showList(node*); void printListBackwards(node *); int main(void) { node *list1; printf("\n Create a sorted list.."); printf("\n Enter values for the first list (-999 to end):"); list1=(node*)malloc(sizeof(node*)); //Allocate memory for the list node insert(list1); //insert values by calling the function insert showList(list1); //display values entered by user printf("\n After recursively reversing the list is :\n"); printListBackwards(list1); //print the values in reverse order using the function...
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 +...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size,...
Please paraphrase this c code #include <stdio.h> #include <stdlib.h> #include <string.h> void sortGrades(int arr[], int size, int status, char names[][20]); void printer(int grades[], int size, char names[][20]); void sortNames(char arr[][20], int size, int status, int grades[]); void nameSearch(int grades[], int size, char names[][20]); void numSearch(int grades[], int size, char names[][20]); int main() { int i; int size; int option; do { printf("\n\nInput Number of Students or 0 to exit : "); scanf("%d", &size); if (size == 0) { break; }...
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...
FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct...
FINISH print and freelist #include<stdio.h> #include <stdlib.h> struct node {      int data;      struct node  *next; }; struct node* insert(struct node* list,int d ); struct node* del(struct node* list,int d ); void print( struct node *list); void freeList(struct node* list); void copy ( struct node *q, struct node **s ); int main( ) {     int number = 0, choice = 0;     struct node *pList=NULL;     struct node *nList = NULL;         while(choice!= 4)     {                 printf("Do you want to (1)insert, (2)delete, (3)Copy (4)quit.\n");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT