#include <stdio.h>
#include <omp.h>
int main() {
int n = 10;
int a[n], b[n], c[n];
// Initialize arrays
for (int i = 0; i < n; ++i) {
a[i] = i;
b[i] = i * 2;
}
// Work sharing with parallel for
#pragma omp parallel for
for (int i = 0; i < n; ++i) {
c[i] = a[i] + b[i];
}
printf("Vector Addition using parallel for:\n"); for (int i = 0; i < n; ++i) {
printf("c[%d] = %d\n", i
, c
[i
]); }
// Work sharing with sections
#pragma omp parallel sections
{
#pragma omp section
{
for (int i = 0; i < n/2; ++i) {
c[i] *= 2;
}
}
#pragma omp section
{
for (int i = n/2; i < n; ++i) {
c[i] *= 3;
}
}
}
printf("\nVector Modification using sections:\n"); for (int i = 0; i < n; ++i) {
printf("c[%d] = %d\n", i
, c
[i
]); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxvbXAuaD4KCmludCBtYWluKCkgewogICAgaW50IG4gPSAxMDsgCiAgICBpbnQgYVtuXSwgYltuXSwgY1tuXTsKCiAgICAvLyBJbml0aWFsaXplIGFycmF5cwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBhW2ldID0gaTsKICAgICAgICBiW2ldID0gaSAqIDI7CiAgICB9CgogICAgLy8gV29yayBzaGFyaW5nIHdpdGggcGFyYWxsZWwgZm9yCiAgICAjcHJhZ21hIG9tcCBwYXJhbGxlbCBmb3IKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgY1tpXSA9IGFbaV0gKyBiW2ldOyAKICAgIH0KCiAgICBwcmludGYoIlZlY3RvciBBZGRpdGlvbiB1c2luZyBwYXJhbGxlbCBmb3I6XG4iKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgKytpKSB7CiAgICAgICAgcHJpbnRmKCJjWyVkXSA9ICVkXG4iLCBpLCBjW2ldKTsKICAgIH0KCiAgICAvLyBXb3JrIHNoYXJpbmcgd2l0aCBzZWN0aW9ucwogICAgI3ByYWdtYSBvbXAgcGFyYWxsZWwgc2VjdGlvbnMKICAgIHsKICAgICAgICAjcHJhZ21hIG9tcCBzZWN0aW9uIAogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuLzI7ICsraSkgewogICAgICAgICAgICAgICAgY1tpXSAqPSAyOyAKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICAjcHJhZ21hIG9tcCBzZWN0aW9uIAogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IG4vMjsgaSA8IG47ICsraSkgewogICAgICAgICAgICAgICAgY1tpXSAqPSAzOyAKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBwcmludGYoIlxuVmVjdG9yIE1vZGlmaWNhdGlvbiB1c2luZyBzZWN0aW9uczpcbiIpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyArK2kpIHsKICAgICAgICBwcmludGYoImNbJWRdID0gJWRcbiIsIGksIGNbaV0pOwogICAgfQoKICAgIHJldHVybiAwOwp9