Question

In: Computer Science

Explain the errors in the C code that cause the output NOT to be -10. (Hint:...

Explain the errors in the C code that cause the output NOT to be -10. (Hint: There are 2 errors):

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
  
#define SIZE 5
void *threadFunc(void *arg);
int *changingVal;
int a[SIZE]; // Assume a[] = { 13444, 3320, 31020, 3302, 31313 };

int main() {
int i;
int min;
int * changeVal = malloc (sizeof(int*));
*changeVal = -10;
// Thread creation
pthread_t thread1;
pthread_attr_t attr;
pthread_attr_init(&attr);
  
for (i = 0; i < SIZE; i++) {
a[i] = rand();
printf("%d\n", a[i]);
}
min = a[0];
  
pthread_create(&thread1,&attr,threadFunc, &changeVal);

#pragma omp parallel for num_threads(1)
for (i = 1; i < SIZE; i++) {
if (a[i] < min) {
pthread_join(thread1, NULL);
#pragma omp critical

{
// compare a[i] and min again because min
// could have been changed by another thread after   
// the comparison outside the critical section
if (a[i] < min)
min = a[i];
}
}
}
printf("Min number found:\t%d\n", min);
}

void * threadFunc(void *arg) {
changingVal = (int*) arg;
a[3] = *changingVal;
pthread_exit(0);
}

Solutions

Expert Solution

Updated Code is in BOLD!!

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
  
#define SIZE 5
void *threadFunc(void *arg);
int *changingVal;
int a[SIZE]; // Assume a[] = { 13444, 3320, 31020, 3302, 31313 };

int main() {
int i;
int min;
int * changeVal = malloc (sizeof(int*));
*changeVal = -10;
// Thread creation
pthread_t thread1;
pthread_attr_t attr;
pthread_attr_init(&attr);
  
for (i = 0; i < SIZE; i++) {
a[i] = rand();

//printf("%d\n", a[i]); We need not to print here, instead print after updation
}
min = a[0];
//pthread_create(&thread1,&attr,threadFunc, &changeVal); , This was error
pthread_create(&thread1,&attr,threadFunc, changeVal);

for (i = 0; i < SIZE; i++) {
printf("%d\n",a[i]);
}

#pragma omp parallel for num_threads(1)
for (i = 1; i < SIZE; i++) {
if (a[i] < min) {
pthread_join(thread1, NULL);
#pragma omp critical

{
// compare a[i] and min again because min
// could have been changed by another thread after   
// the comparison outside the critical section
if (a[i] < min)
min = a[i];
}
}
}
printf("Min number found:\t%d\n", min);
}

void * threadFunc(void *arg) {
changingVal = (int*) arg;
a[3] = *changingVal;
pthread_exit(0);
}

The first error was that we are passing the reference of the changeVal -

pthread_create(&thread1,&attr,threadFunc, &changeVal);  

Instead, we need to pass the value of changeVal ,i.e, -10

pthread_create(&thread1,&attr,threadFunc, changeVal);  

The second error was we are printing the value of "a" before the updation

for (i = 0; i < SIZE; i++) {
a[i] = rand();
//printf("%d\n", a[i]); Comment this to avoid this line to run!!
}

pthread_create(&thread1,&attr,threadFunc, changeVal);

Instead we will print after updation

pthread_create(&thread1,&attr,threadFunc, changeVal);

for (i = 0; i < SIZE; i++) {
printf("%d\n",a[i]);
}


Related Solutions

C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
please submit the C code( no third party library). the C code will create output to...
please submit the C code( no third party library). the C code will create output to a file, and iterate in a loop 60 times and each iteration is 1 second, and if any key from your keyboard is pressed will write a 1 in the file, for every second no key is pressed, will write a 0 into the output file.
In this python script find any bugs and errors which would cause the code to crash....
In this python script find any bugs and errors which would cause the code to crash. The code must be re-written correctly. After debugging make a separate list of all the errors you found in the script. contacts_list=[] # global variable, list of contacts_list, one string per contact def pause()     """ pauses program e.g. to view data or message """     input("press enter to continue") def load():     """ populate list with data """          contacts_list.append(('Milo ', '063847489373'))...
There are two errors in this code. Identify the errors and give the correct code that...
There are two errors in this code. Identify the errors and give the correct code that will make the program to display the following output: Rectangle: height 2.0 width 4.0 Area of the Rectangle is 8.0 ----- public interface Shape { public double getArea(); } class Rectangle implements Shape { double height; double width; public Rectangle(double height, double width) { this.height=height; this.width=width; } public double getArea() { return height*width; } public String toString() { return "Rectangle: height "+height+" width "+width;...
What is the output from each of the following segments of C++ code? Record the output...
What is the output from each of the following segments of C++ code? Record the output after the “Answer” prompt at the end of each program fragment. Assume all variables have been suitably declared. (Each problem is worth 3 points.) 1. for (int j = 8; j > 3; j -= 2)         cout << setw(5) << j;     Answer:      2. int sum = 5;    for (int k = -4; k <= 1; k++)         sum = sum...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an...
C Code Edit this code to make it output all unique letters IN LOWERCASE of an input file. For example, with this input: Raspberry Grapefruit Straw berry raspBERRY Blue$$berry apple Pine_apple raspberry The output should be: raspberry grapefruit straw berry blue berry apple pine NOTE: Words with different capitlization, like raspberry and raspBERRY count as 1 unique word, so raspberry should only be seen once in the output. #include <stdio.h> #include <stdlib.h> int main() {     //Pointer to access the...
a. What will be the output of LINE A in code “a” and output of code...
a. What will be the output of LINE A in code “a” and output of code “b”? Also write reasons for the outputs. a. #include <sys/types.h> #include <stdio.h> #include <unistd.h> int value = 3; int main() { pid_t pid; pid = fork(); if (pid = = 0) {/* child process */} value += 233; return 0; } else if (pid > 0) {/* parent process */} wait(NULL); printf(“PARENT: value = %d”, value); /* LINE A */ return 0; } }...
c# code working but output not right, I need to output all numbers like : Prime...
c# code working but output not right, I need to output all numbers like : Prime factors of 4 are: 2 x 2 here is just 2 Prime factors of 7 are: 7 Prime factors of 30 are: 2 x 3 x 5 Prime factors of 40 are: 2 x 2 x 2 x 5 here is just 2,5 Prime factors of 50 are: 2 x 5 x 5 here is just 2,5 1) How I can fix it 2)I...
provide a C code (only C please) that gives the output below: ************************************ *         Menu HW...
provide a C code (only C please) that gives the output below: ************************************ *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1. Creating polynomials * * 2. Adding polynomials * * 3. Multiplying polynomials. * * 4. Displaying polynomials * * 5. Clearing polynomials. * * 6. Quit. * *********************************** Select the option (1 through 6): 7 You should not be in this class! ************************************* *         Menu HW #4 * * POLYNOMIAL OPERATIONS * * 1. Creating polynomials...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message based on the logic below. A buyer can pay immediately or be billed. If paid immediately, then display "a 5% discount is applied." If billed, then display "a 2% discount is applied" if it is paid in 30 days. If between 30 and 60 days, display "there is no discount." If over 60 days, then display "a 3% surcharge is added to the bill."...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT