Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h> // for printf, scanf and perror
- #include <stdlib.h> // for atoi
- #include <unistd.h> // for pipe
- #include <sys/types.h> // for pid_t
- #include <string.h> // for strcmp
- // function to calculate sum of array
- int summation(int n, int* arr){
- int sum = 0;
- for(int i = 0; i < n; i++)
- sum += arr[i];
- return sum;
- }
- // function to sort array using bubble sort
- void Bubble_Sort(int n, int* arr){
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n - i - 1; j++){
- if(arr[j] > arr[j + 1]){
- int temp = arr[j];
- arr[j] = arr[j + 1];
- arr[j + 1] = temp;
- }
- }
- }
- }
- // function to check for error in pipe
- void checkerror(int err, char* msg){
- /*
- Print a message describing the meaning of the value of errno.
- This function is a possible cancellation point and therefore not marked with __THROW.
- */
- if(err == -1){
- perror(msg);
- exit(-1);
- }
- }
- // function to get max element from array
- int get_max(int n, int* arr){
- int max = arr[0];
- for(int i = 1; i < n; i++){
- if(arr[i] > max)
- max = arr[i];
- }
- return max;
- }
- // function to get min element from array
- int get_min(int n, int* arr){
- int min = arr[0];
- for(int i = 1; i < n; i++){
- if(arr[i] < min)
- min = arr[i];
- }
- return min;
- }
- // function to get average of array
- float get_avg(int n, int* arr){
- int sum = 0;
- for(int i = 0; i < n; i++)
- sum += arr[i];
- return (float)sum / n;
- }
- // function to get median of array
- float get_median(int n, int* arr){
- if(n % 2 == 0){
- int mid = n / 2;
- return (float)(arr[mid] + arr[mid - 1]) / 2;
- }
- else{
- int mid = n / 2;
- return arr[mid];
- }
- }
- // function to get number of prime numbers in array
- int get_prime(int n, int* arr){
- int count = 0;
- for(int i = 0; i < n; i++){
- int flag = (arr[i] == 1) ? 1 : 0;
- for(int j = 2; j < arr[i]; j++){
- if(arr[i] % j == 0){
- flag = 1;
- break;
- }
- }
- if(flag == 0)
- count++;
- }
- return count;
- }
- int main(int argc, char* argv[]){
- // declare pipe
- int pipefds[2];
- // The pid_t data type is a signed integer type which is capable of representing a process ID. In the GNU library, this is an int.
- pid_t childpid;
- // create pipe
- childpid = pipe(pipefds);
- // check for error
- checkerror(childpid, "Unable to create pipe");
- // getting n from command line
- int n = atoi(argv[1]);
- // declare input and output array of size n
- int input[n], output[n];
- // get input array
- printf("Enter %d numbers seperated by spaces: ", n);
- for(int i = 0; i < n; i++)
- scanf("%d", &input[i]);
- // 0 for read (child process will read from pipe)
- // 1 for write (parent process will write to pipe)
- // parent process will write to pipe
- write(pipefds[1], input, sizeof(input));
- // child process will read from pipe
- read(pipefds[0], output, sizeof(output));
- // print output array
- printf("\nArray: ");
- for(int i = 0; i < n; i++)
- printf("%d ", output[i]);
- for(int i = 1; i < argc; i++){
- if(!strcmp(argv[i], "max")){
- printf("\nMax Element: %d", get_max(n, output));
- }else if(!strcmp(argv[i], "min")){
- printf("\nMin Element: %d", get_min(n, output));
- }else if(!strcmp(argv[i], "avg")){
- printf("\nAverage: %0.2f", get_avg(n, output));
- }else if(!strcmp(argv[i], "median")){
- printf("\nMedian: %0.1f", get_median(n, output));
- }else if(!strcmp(argv[i], "primes")){
- printf("\nNumber of Prime Numbers: %d", get_prime(n, output));
- }else if(!strcmp(argv[i], "sum")){
- printf("\nSum: %d", summation(n, output));
- }else if(!strcmp(argv[i], "sort")){
- Bubble_Sort(n, output);
- printf("\nSorted Array: ");
- for(int i = 0; i < n; i++)
- printf("%d ", output[i]);
- printf("\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement