Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include <omp.h>
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int threadNumber = 3;
  8.     float v1[5], v2[5], v3[5],v0[5];
  9.     void PrintArray(float array[5], char a);
  10.     void Average(float array[5], char a);
  11.    
  12.     cout << "Broj niti:"<< omp_get_num_threads()<<"\n";
  13.        
  14.     for(int i=0;i<5;i++)
  15.     {
  16.         v1[i]=(float)(1+i)/(1);
  17.         v2[i]=(float)(2+i)/(2);
  18.         v3[i]=(float)(3+i)/(3);
  19.     }
  20.    
  21.     omp_set_num_threads(threadNumber);
  22.  
  23.     //Ispis niti
  24.     #pragma omp parallel
  25.     {
  26.         if(omp_get_thread_num()==0)
  27.         {
  28.             PrintArray(v1,'1');
  29.             Average(v1,'1');
  30.             cout << "Thread num:"<<omp_get_thread_num()<<"\n";
  31.         }
  32.         else if(omp_get_thread_num()==1)
  33.         {
  34.             PrintArray(v2,'2');
  35.             Average(v2,'2');
  36.             cout << "Thread num:"<<omp_get_thread_num()<<"\n";
  37.         }
  38.         else if(omp_get_thread_num()==2)
  39.         {
  40.             PrintArray(v3,'3');
  41.             Average(v3,'3');
  42.             cout << "Thread num:"<<omp_get_thread_num()<<"\n";
  43.         }
  44.     }
  45.    
  46.     #pragma omp parallel for
  47.     for(int i=0;i<5;i++)
  48.     {
  49.       v0[i]=v1[i]+v2[i]+v3[i];
  50.     }
  51.    
  52.     cout<<"Rezultat:"<<"\n";
  53.     for(int i=0;i<5;i++)
  54.     {
  55.       cout<<v0[i]<<"\n";
  56.     }
  57.    
  58.     //globalna srednja
  59.     float glob = 0;
  60.     for(int i=0;i<5;i++)
  61.     {
  62.       glob = glob+v0[i];
  63.     }
  64.    
  65.     cout<<"Globalna srednja vrijednost"<<(glob)/15<<"\n";
  66.    
  67. }
  68.  
  69. void PrintArray(float array[5],char a)
  70. {
  71.     for(int i =0;i<5;i++)
  72.     {
  73.         cout << a << array[i] << "\n";
  74.     }
  75. }
  76.  
  77. void Average(float array[5],char a)
  78. {
  79.     int sum=0;
  80.     for(int i =0;i<5;i++)
  81.     {
  82.         sum = sum + array[i];
  83.     }
  84.     float avg = (float)(sum)/(5);
  85.     cout << "Srednja vrijednost:" << avg << "\n";
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement