0

Hi!

I have to do 2 different codes for my task. I finally did the 1st and now I need to do the 2nd one too. But I don't know the way to do it differently.

Do anyone know how to make this code more simple? It needs to have the same function. Maybe do while loop instead of while loop or something? Please help that I get 2 different codes.

```
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
float averageOfNNumbers(int n)
{
float avg = 0;
int i;
for(i=1;i<n;i++)
{
avg=avg+i;
}
return (avg/n);
}
int main(int argc, char* argv[])
{
if(argc!=4)
{
printf("Error! Wrong number of arguments!\n");
return 1;
}
int n=atoi(argv[1]);
int m=atoi(argv[2]);
int k=atoi(argv[3]);
int i=1,j=1;
float avg;
pid_t pid[n];
while(i<=n)
{
pid[i-1]=fork();
if(pid[i-1]==0)
{
while(j<=m)
{
printf("PID: %d\n",getpid());
j++;
}
if(k-i>0)
{
avg=averageOfNNumbers(k-i);
}
else
{
avg=0;
}
printf("Average: %2.f\n\n",avg);
exit(EXIT_SUCCESS);
}
i++;
}
int i2=0;
while(i2<n)
{
wait(&pid[i2]);
i2++;
}
return 0;
}
```

Any help is the most welcome! Thank you!

Regards,

trume