Create a Pthreads version of the following Pascal triangle program. #include <ma
ID: 3603298 • Letter: C
Question
Create a Pthreads version of the following Pascal triangle program.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, char *argv[]) {
assert(argc==2);
double stop = (double)atol(argv[1]);
assert(stop >= 1.0);
int result = 0;
for (double x = 0.0; x < stop; x += 1.0) {
double y = fabs(cos(x)); // 0.0<=y<=1.0
int z = (int)(10000.0 * y); // 0<=z<=10000
result = (result + z)%10000; // 0<=result<10000
#ifdef DEBUG
printf("x=%lf, y=%lf, z=%d ", x, y, z);
printf("new result : %d ", result);
fflush(stdout);
#endif
}
printf("The PIN is %d ", result);
fflush(stdout);
return 0;
}
The program should accept 2 command line arguments:
1. N, the number of rows to compute
2. the number of threads to use
Also, make a Makefile to compile and run the code.
Explanation / Answer
01
#include<stdio.h>
02
int main() {
03
char prnt = '*';
04
int i, j, s, nos = 0;
05
for (i = 9; i >= 1; (i = i - 2)) {
06
for (s = nos; s >= 1; s--) {
07
printf(" ");
08
}
09
for (j = 1; j <= i; j++) {
10
if ((i % 2) != 0 && (j % 2) != 0) {
11
printf("%2c", prnt);
12
} else {
13
printf(" ");
14
}
15
}
16
printf(" ");
17
nos++;
18
}
19
nos = 3;
20
for (i = 3; i <= 9; (i = i + 2)) {
21
for (s = nos; s >= 1; s--) {
22
printf(" ");
23
}
24
for (j = 1; j <= i; j++) {
25
26
if ((i % 2) != 0 && (j % 2) != 0) {
27
printf("%2c", prnt);
28
} else {
29
printf(" ");
30
}
31
}
32
nos--;
33
printf(" ");
34
}
35
return 0;
36
}
01
#include<stdio.h>
02
int main() {