Advertisement
Kyojin96

[WARNING - not working 100%] Task 4

Jan 5th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define N 15
  5.  
  6. //Return count and pass by reference the sum
  7. int CountSum(float arr[], int n, float* sum);
  8.  
  9. //function for array input
  10. void read_arr(float arr[], int *n);
  11.  
  12. //function to read the array values
  13. void write_arr(float a[], int *n);
  14.  
  15. int main()
  16. {
  17.     float arr[N];
  18.     float totalSum = 0;
  19.     int n;
  20.  
  21.     read_arr(arr, &n);
  22.  
  23.     int count = CountSum(arr, n, &totalSum);
  24.  
  25.     //write_arr(arr, &n);
  26.  
  27.     printf("Count: %d || Sum: %f \n", count, totalSum);
  28.  
  29.     return 0;
  30. }
  31.  
  32. void read_arr(float a[], int *n)
  33. {
  34.     int j;
  35.     do
  36.     {
  37.         printf("\nVavedi razmernostta na masiva: ");
  38.         scanf("%d", n);
  39.     }
  40.     while( *n < 1 || *n > N);
  41.         for( j = 0; j < *n; j++ )
  42.         {
  43.             printf("array[%d]: ", j);
  44.             scanf("%d", &a[j]);
  45.         }
  46. }
  47.  
  48. int CountSum(float arr[], int n, float* sum)
  49. {
  50.     //Find all positive numbers and count of all 0 elements
  51.     int i;
  52.     int count = 0;
  53.     for(i = 0; i < n; i++)
  54.     {
  55.         //If element is equal to 0, then increment the count
  56.         if(arr[i] == 0)
  57.             count++;
  58.  
  59.         //If the element is positive, add it to the sum
  60.         if(arr[i] > 0)
  61.             *sum += arr[i];
  62.     }
  63.  
  64.     //Return the count
  65.     return n;int j;
  66. printf("\n(");
  67. for( j = 0; j < n; j++ )
  68. {
  69. printf(" %d", a[j]);
  70. }
  71. printf(")\n");
  72.  
  73. }
  74.  
  75. void write_arr(float a[], int* n)
  76. {
  77.     int j;
  78.     printf("\n(");
  79.     for( j = 0; j < n; j++ )
  80.     {
  81.         printf(" %d", a[j]);
  82.     }
  83.     printf(")\n");
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement