Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

Program to find mode of the given array

Posted By: Daisy Brown     Category: C Programming     Views: 5949

Program to find mode of the given array

Code for Program to find mode of the given array in C Programming

#include<stdio.h>
#include "shared.h"
#include "forkjoin.h"
#include "barrier.h"int main()
{
        int pid, nproc, *bar, k, *a, size, i, *j, *s, *max, *no;
        int shmid1, shmid2, sh3, sh4, sh5, sh6;
        float s1;

        printf("Enter the size of the array :: ");
        scanf("%d",&size);

        a=(int *)create_memory(size*2,&shmid1);
        no=(int *)create_memory(size*2,&sh4);
        max=(int *)create_memory(2,&sh5);
        bar=(int *)create_memory(2,&shmid2);
        *max=0;
        j=(int *)create_memory(2,&sh6);
        *j=0;                       

        printf("Enter the elements of the array :: \n");
        for(i=0;i<size;i++)
                scanf("%d",&a[i]);

        nproc=size;
        barrier_init(bar,nproc);
        s=(int *)create_memory(2,&sh3);
        *s=0;

        pid=create_process(&nproc);
        no[pid]=1;
        for(i=0;i<size;i++)
                if(pid!=i && a[i]==a[pid])
                        no[pid]+=1;

        barrier(bar);

        if(*max < no[pid])
                *max=no[pid];

        barrier(bar);

        if(no[pid]==*max)                
        {
                *s += a[pid];
                (*j)++;
        }

        join_process(&nproc, &pid);

        printf("\n");
        if(*max==1)
                printf("\nMode is not possible.");
        else

        {
                s1=*s;
                s1 = s1 / (*j);
                printf("The mode is %.2f",s1);
        }

        cleanup_memory(&shmid1);
        cleanup_memory(&shmid2);
        cleanup_memory(&sh3);
        cleanup_memory(&sh4);
        cleanup_memory(&sh5);
        cleanup_memory(&sh6);                 
        return 0;
}                      

/* OUTPUT */
Enter the size of the array :: 5 Enter the elements of the array :: 2 2 3 3 1 The mode is 2.50 Enter the size of the array :: 5 Enter the elements of the array :: 1 2 3 4 5 Mode is not possible.
  
Share: 


Didn't find what you were looking for? Find more on Program to find mode of the given array Or get search suggestion and latest updates.

Daisy Brown
Daisy Brown author of Program to find mode of the given array is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!