Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

#include <stdio.

h>
#include <omp.h>
int main()
{
    int n;
   
    scanf("%d", &n);
    int arr1[n],arr2[n];
    int n1 = 1;
    double start1 = omp_get_wtime();
    while ((n1 + 5) <= n)
    {
        printf("%d ", n1);
        n1 += 5;
        printf("%d ", n1);
        n1 -= 3;
    }
    printf("\n");
     int a=0,b=0;
    int sum1 = 0, sum2 = 0;
    int i=1,j=6;
   
    double end1 = omp_get_wtime();
    printf("Sequential time is %f\n.", end1 - start1);
   
   
    omp_set_num_threads(2);

    double start2 = omp_get_wtime();


    #pragma omp sections
    {
        #pragma omp section
        {
        for (i = 1; i <=n; i=i+2)
            {
                arr1[a++]=i;
                sum1 += i;
            }
        }
        #pragma omp section
        {
           
            for (j = 6; j <=n; j+=2)
            {
                arr2[b++]=j;
                sum2 += j;
            }
        }  
    }

   
    double end2 = omp_get_wtime();
    printf("Parallel time is %f\n.", end2 - start2);
    int calc;
    if(a > b){
        calc = a;
    }
    else{
        calc = b;
    }
    int k =0;
    while(k <=calc){
        if(k < a){
            printf("%d ",arr1[k]);
        }
        if(k < b){
            printf("%d ",arr2[k]);
        }
        k++;
    }
    printf("\nSum is %d\n",sum1 + sum2);

   
}

You might also like