Advertisement
7oSkaaa

Operating_System

Dec 24th, 2022 (edited)
1,346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | Source Code | 0 0
  1. #include <stdio.h>  // for printf, scanf and perror
  2. #include <stdlib.h>  // for atoi
  3. #include <unistd.h> // for pipe
  4. #include <sys/types.h> // for pid_t
  5. #include <string.h> // for strcmp
  6.  
  7. // function to calculate sum of array
  8. int summation(int n, int* arr){
  9.     int sum = 0;
  10.     for(int i = 0; i < n; i++)
  11.         sum += arr[i];
  12.     return sum;
  13. }
  14.  
  15. // function to sort array using bubble sort
  16. void Bubble_Sort(int n, int* arr){
  17.     for(int i = 0; i < n; i++){
  18.         for(int j = 0; j < n - i - 1; j++){
  19.             if(arr[j] > arr[j + 1]){
  20.                 int temp = arr[j];
  21.                 arr[j] = arr[j + 1];
  22.                 arr[j + 1] = temp;
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28.  
  29. // function to check for error in pipe
  30. void checkerror(int err, char* msg){
  31.     /*
  32.         Print a message describing the meaning of the value of errno.
  33.         This function is a possible cancellation point and therefore not marked with __THROW.
  34.     */
  35.     if(err == -1){
  36.         perror(msg);
  37.         exit(-1);
  38.     }
  39. }
  40.  
  41. // function to get max element from array
  42. int get_max(int n, int* arr){
  43.     int max = arr[0];
  44.     for(int i = 1; i < n; i++){
  45.         if(arr[i] > max)
  46.             max = arr[i];
  47.     }
  48.     return max;
  49. }
  50.  
  51.  
  52. // function to get min element from array
  53. int get_min(int n, int* arr){
  54.     int min = arr[0];
  55.     for(int i = 1; i < n; i++){
  56.         if(arr[i] < min)
  57.             min = arr[i];
  58.     }
  59.     return min;
  60. }
  61.  
  62. // function to get average of array
  63. float get_avg(int n, int* arr){
  64.     int sum = 0;
  65.     for(int i = 0; i < n; i++)
  66.         sum += arr[i];
  67.     return (float)sum / n;
  68. }
  69.  
  70. // function to get median of array
  71. float get_median(int n, int* arr){
  72.     if(n % 2 == 0){
  73.         int mid = n / 2;
  74.         return (float)(arr[mid] + arr[mid - 1]) / 2;
  75.     }
  76.     else{
  77.         int mid = n / 2;
  78.         return arr[mid];
  79.     }
  80. }
  81.  
  82. // function to get number of prime numbers in array
  83. int get_prime(int n, int* arr){
  84.     int count = 0;
  85.     for(int i = 0; i < n; i++){
  86.         int flag = (arr[i] == 1) ? 1 : 0;
  87.         for(int j = 2; j < arr[i]; j++){
  88.             if(arr[i] % j == 0){
  89.                 flag = 1;
  90.                 break;
  91.             }
  92.         }
  93.         if(flag == 0)
  94.             count++;
  95.     }
  96.     return count;
  97. }
  98.  
  99. int main(int argc, char* argv[]){
  100.    
  101.     // declare pipe
  102.     int pipefds[2];
  103.  
  104.     // 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.
  105.     pid_t childpid;
  106.  
  107.     // create pipe
  108.     childpid = pipe(pipefds);
  109.  
  110.     // check for error
  111.     checkerror(childpid, "Unable to create pipe");
  112.  
  113.     // getting n from command line
  114.     int n = atoi(argv[1]);
  115.  
  116.     // declare input and output array of size n
  117.     int input[n], output[n];
  118.  
  119.     // get input array
  120.     printf("Enter %d numbers seperated by spaces: ", n);
  121.     for(int i = 0; i < n; i++)
  122.         scanf("%d", &input[i]);
  123.  
  124.     // 0 for read  (child process will read from pipe)
  125.     // 1 for write (parent process will write to pipe)
  126.  
  127.     // parent process will write to pipe
  128.     write(pipefds[1], input, sizeof(input));
  129.    
  130.     // child process will read from pipe
  131.     read(pipefds[0], output, sizeof(output));
  132.  
  133.     // print output array
  134.     printf("\nArray: ");
  135.     for(int i = 0; i < n; i++)
  136.         printf("%d ", output[i]);
  137.  
  138.     for(int i = 1; i < argc; i++){
  139.         if(!strcmp(argv[i], "max")){
  140.             printf("\nMax Element: %d", get_max(n, output));
  141.         }else if(!strcmp(argv[i], "min")){
  142.             printf("\nMin Element: %d", get_min(n, output));
  143.         }else if(!strcmp(argv[i], "avg")){
  144.             printf("\nAverage: %0.2f", get_avg(n, output));
  145.         }else if(!strcmp(argv[i], "median")){
  146.             printf("\nMedian: %0.1f", get_median(n, output));
  147.         }else if(!strcmp(argv[i], "primes")){
  148.             printf("\nNumber of Prime Numbers: %d", get_prime(n, output));
  149.         }else if(!strcmp(argv[i], "sum")){
  150.             printf("\nSum: %d", summation(n, output));
  151.         }else if(!strcmp(argv[i], "sort")){
  152.             Bubble_Sort(n, output);
  153.             printf("\nSorted Array: ");
  154.             for(int i = 0; i < n; i++)
  155.                 printf("%d ", output[i]);
  156.             printf("\n");
  157.         }
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement