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: Paige Hughes     Category: C Programming     Views: 3565

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);        
}

/*
Output

[divyen@localhost pp-tw4]$ cc -o Prog06 -lpthread Prog06.c
[divyen@localhost pp-tw4]$ ./Prog06
Enter Value of arr[0] :1
Enter Value of arr[1] :2
Enter Value of arr[2] :-3
Enter Value of arr[3] :45
Enter Value of arr[4] :23
Enter Value of arr[5] :45
Enter Value of arr[6] :67
Enter Value of arr[7] :-9
Enter Value of arr[8] :12
Enter Value of arr[9] :45
Final Max : 67
Final Min : -9

*/
  
Share: 



Paige Hughes
Paige Hughes author of Program to find the maximum and minimum element in an array using threads 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!