Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Sunday, 28 April 2019

IOPP Program No.3.A

Program No.3.A

 Write an OpenMP program (for shared memory multiprocessor architecture) to sort the given list of numbers using Radix Sort. Display the appropriate environment variables to demonstrate parallel programming. 

Solution:

#include <stdio.h>
#include<omp.h>
int
main ()
{
  int b, i, j, p, q, d, z = 9, digit = 0, max,n;
  int thread;
  int a[100];
 printf("Enter the Number of Elements U Want TO enter\n");
scanf("%d",&n);
printf("Enter the Elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}

  d = 10;
  max = a[0];
  for (i = 1; i < n; i++)
    {
      if (max < a[i])
        {
          max = a[i];
        }
    }
  while (max != 0)
    {
      max = max / 10;
      digit++;
    }
#pragma omp parallel for schedule(static,1) num_threads(9)
  for (z = 0; z < digit; z++)
    {
      for (i = 0; i < n; i++)
        {
          p = a[i] % d;
          for (j = i + 1; j < n; j++)
            {
              q = a[j] % d;
              if (p > q)
                {
                  b = a[i];
                  a[i] = a[j];
                  a[j] = b;
                  p = a[i] % d;
                }

            }
        }

      d = d * 10;
    }
  printf ("The array is:\n");
  for (i = 0; i < n; i++)
    {
      printf ("%d\n", a[i]);
    }
#pragma omp parallel for schedule(static,1) num_threads(9)
  for (i = 0; i < 11; i++)
    {
      thread = omp_get_thread_num ();
      printf ("%d\t  thread:%d\n", a[i], thread);
    }

}

0 comments:

Post a Comment