Logo 
Search:

C Programming Articles

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

Program to find minimum and maximum from an array using structure

Posted By: Viveka Fischer     Category: C Programming     Views: 5353

Program to find minimum and maximum from an array using structure.

Code for Program to find minimum and maximum from an array using structure in C Programming

#include<stdio.h>
#include<pthread.h>

struct minmax
{
    int min;
    int max;
};
typedef struct minmax s;

int a[10],n;

void *min_max();
s *smain,*sthread;

main()
{
    int finalmin,finalmax,i;
    pthread_t tid;
    
    smain=(s*)malloc(sizeof(s));
    
    printf("Enter the limit : ");
    scanf("%d",&n);

    printf("\nEnter the elemenats :");
    for(i=0;i<n;i++)
    {
        printf("\na[%d] : ",i);
        scanf("%d",&a[i]);
    }

    smain->min=smain->max=a[0];
    
    pthread_create(&tid,NULL,min_max,NULL);
    
    for(i=0;i<n/2;i++)
    {
        if(a[i] < smain->min)
            smain->min=a[i];
        if(a[i] > smain->max)
            smain->max=a[i];
    }

    pthread_join(tid,NULL);

    if(smain->min <= sthread->min)
        finalmin=smain->min;
    else
        finalmin=sthread->min;

    if(smain->max >= sthread->max)
        finalmax=smain->max;
    else
        finalmax=sthread->max;

    printf("\nMin : %d \n Max : %d",finalmin,finalmax);
}

void *min_max()
{
    int i;
    
    sthread=(s*)malloc(sizeof(s));
    
    sthread->min=sthread->max=a[n/2];
     
    for(i=a[n/2];i<n;i++)
    {
        if(a[i] < sthread->min)
            sthread->min=a[i];
        if(a[i] > sthread->max)
            sthread->max=a[i];
    }
}
  
Share: 



Viveka Fischer
Viveka Fischer author of Program to find minimum and maximum from an array using structure is from Frankfurt, Germany.
 
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!