Logo 
Search:

C Programming Articles

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

Program to find the maximum and minimum element in an array using threads

Posted By: Easy Tutor     Category: C Programming     Views: 9309

Write a program to find the maximum and minimum element in an array using threads.

Code for Program to find the maximum and minimum element in an array using threads in C Programming

# include <stdio.h>
# include <pthread.h>
# define arrSize 10

struct StructMaxMin
{
    int iMax;
    int iMin;
};

int arr[arrSize];

void *thread_search_min_max(void *);

int main()
{
    pthread_t tid;
    struct StructMaxMin *st_main,*st_th;
    int FinalMax,FinalMin;
    
    st_main=(struct StructMaxMin*)malloc(sizeof(struct StructMaxMin));
    
    int iCount;
    for(iCount=0;iCount<arrSize;iCount++)
    {
        printf("Enter Value of arr[%d] :",iCount);
        scanf("%d",&arr[iCount]);
    }        
    pthread_create(&tid,NULL,thread_search_min_max,NULL);
    
    st_main->iMax=arr[0];
    st_main->iMin=arr[0];
    
    for(iCount=1;iCount<arrSize/2;iCount++)
    {
        if(arr[iCount] > st_main->iMax)
        {
            st_main->iMax=arr[iCount];
        }
        
        if(arr[iCount] < st_main->iMin)
        {
            st_main->iMin=arr[iCount];
        }
    }    
    
    pthread_join(tid,(void**)&st_th);    
    
    if(st_main->iMax >= st_th->iMax)
    {
        FinalMax=st_main->iMax;
    }    
    else
    {
        FinalMax=st_th->iMax;
    }
        
    if(st_main->iMin <=st_th->iMin)
    {
        FinalMin=st_main->iMin;
    }
    else
    {
        FinalMin=st_th->iMin;
    }
    
    printf("Final Max : %d \n",FinalMax);
    printf("Final Min : %d \n",FinalMin);
    return 0;
}


void *thread_search_min_max(void *para)
{
    struct StructMaxMin *st;
    st=(struct StructMaxMin*)malloc(sizeof(struct StructMaxMin));
        
    int iCount;
    st->iMax=arr[arrSize/2];
    st->iMin=arr[arrSize/2];
        
    
    for(iCount=arrSize/2 + 1;iCount<arrSize;iCount++)
    {
        if(arr[iCount] > st->iMax)
        {
            st->iMax=arr[iCount];
        }
        if(arr[iCount] < st->iMin)
        {
            st->iMin=arr[iCount];
        }
    }    
    
    pthread_exit((void*)st);        
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to find the maximum and minimum element in an array using threads is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
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!