Write a program in C/C++/Linux to find the mean and then use that mean for
further calculations. Make three threads to perform different task as follows:
a) The 1st Thread will find the mean.
b) The 2nd Thread will take the mean from the 1st Thread and display the numbers
that are greater than the mean.
c) The 3rd Thread will also take the mean from the 1st Thread and display the
factorial of the two numbers that are the closest to the mean.
d) The Thread#2 and Thread#3 will wait until the thread#1 complete its task, and
then terminate by giving an appropriate message on the screen.
This program should take user input from command line as follows to calculate the mean:
Sample output is shown below:
[prompt]# ./file 12 7 11 14 5 7 9 4 33 65
Output starts here
Thread 1 starts
Mean = 16.7
Thread 1 ends
Thread 2 starts
33 65
Thread 2 ends
Thread 3 starts
12! =
14! =
Thread 3 ends
End of output

sergent commented: Guess why I downvoted! -1

Homework
End of output....

my confusion is as syntax of thread creation is

pthread_create( 
		pthread_t *thread, 
		const pthread_attr_t *attr, 
		void *(*func)(void *),
		void *arg);

how i use two arguments
int argc,char * argv[]) in it

my confusion is as syntax of thread creation is
pthread_create(
pthread_t *thread,
const pthread_attr_t *attr,
void *(*func)(void *),
void *arg);
how i use two arguments
int argc,char * argv[]) in it

CODE TAGS!!!! Use them!

Which parameter in the pthread_create() call is the *argv[] parameter? Same with argc. Start there.

Well first you can write a program containing 3 functions executed sequentially.

int main ( int argc, char *argv[] ) {
  calculateMean();
  printGreaterThanMean();
  printNearestFactorials();
}

When you can do those things, then worry about the thread issues.

//nidasyed.cpp
//#include<stdio.h>
#include<stdlib.h>
#include<iostream.h>
//#include<conio>
#include<pthread.h>
#include<string.h>
#include<fstream.h>
//using namespace std;
long factorial (int);
float mean1=0.0;
int count=0;
int array2[1000];
int array[1000];
void *thread1(void *);
void *thread2(void *);
void *thread3(void *);

//int count=0;

int main(int argc, char*argv[])
{
//count=0;

int temp,temparray[argc],i;
pthread_t tid1;
pthread_t tid2;
pthread_t tid3;
int l,j,k;
for(i=1;i<argc;i++)
{
temparray[i]=atoi(argv[i]);
array[i]=atoi(argv[i]);
count++;
}
l=pthread_create(&tid1,NULL,thread1,NULL);
pthread_join(tid1,NULL);
j=pthread_create(&tid2,NULL,thread2,NULL);
pthread_join(tid2,NULL);
k=pthread_create(&tid3,NULL,thread3,NULL);
return 0;
}
void *thread1(void *)
{
cout<<"out put start here"<<endl;
cout<<"start of thread 1"<<endl;
int n;
//n=0;
float mean;
mean=0.0;
int i;
cout<<"number you entred"<<endl;
for(i=1;i<=count;i++)
{
cout<<array[i]<<endl;
mean+=array[i];

}
 mean=mean/count;

cout<<"mean is:   "<<mean;
cout<<"\n ist thread sucessfully executed"<<endl;
mean1=mean; 

}

void *thread2(void *)
{
cout<<"start of thread 2"<<endl;
cout<<"mean in ist thread was"<<mean1<<endl;
cout<<"number greater than mean are/is"<<endl;
for(int i=0;i<=count;i++)
{
if(array[i]>mean1)
{cout<<array[i]<<endl;
}
}
cout<<"2nd thread sucessfully executed"<<endl;


}

void *thread3(void *)
{
//array2[1000]=array[1000];
int first;
int second;
int t;
int temparr[1000];
cout<<"start of third thread"<<endl;
for(int u=count-1;u>=1;u--)
for(int i=0;i<u;i++)
if(array[i]<array[i+1])
{
t=array[i];
array[i]=array[i+1];
array[i+1]=t;
}
//cout<<"number closest to mean are"<<endl;
//cout<<"sorted array is"<<endl;
for(int i=0;i<=count;i++)
{
if(array[i]>mean1)
{

// in ko input lelo array mein aur array ki o nd ist value ka factrIAL NIKAL LO
//cout<<array[i]<<endl;;
}
}
first=array[1];
second=array[2];
cout<<"factorial of ist closest no "<<second<<"is  :"<<factorial(second)<<endl;
cout<<"factorial of 2nd closest no "<<first<<"is  :"<<factorial(first)<<endl;
cout<<"end of thread 3"<<endl;
cout<<"output ends here"<<endl;                                                                                    
}
long factorial (int n)
{
if (n==0)
{return(0);}
else if (n==1)
{return(1);} 
else
{
n=factorial(n-1)*n;
}
return n;
}

above is the solution to this thread

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.