Advertisement
horselurrver

Averages

Aug 5th, 2016
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #define ROWS 3
  3. #define COLS 5
  4. void storage(int rows, int cols, double array[rows][cols]);
  5. void row_average(int rows, int cols, double array[rows][cols]);
  6. double average(int rows, int cols, double array[rows][cols]);
  7. double largest(int rows, int cols, double array[rows][cols]);
  8.  
  9. int main(void){
  10.     double array1[ROWS][COLS];
  11.     printf("Enter 3 sets of 5 double numbers each. Press enter after every 5 numbers.\n");
  12.     storage(ROWS, COLS, array1);
  13.     row_average(ROWS, COLS, array1);
  14.     printf("The average of all the values is %.2f.\n", average(ROWS, COLS, array1));
  15.     printf("The biggest value of the 15 values is %.2lf.\n", largest(ROWS, COLS, array1));
  16.    
  17.     return 0;
  18.    
  19. }
  20.  
  21. void storage(int rows, int cols, double array[rows][cols]){
  22.     int i, j;
  23.     for(i=0; i<ROWS; i++){
  24.         for(j=0; j<COLS; j++){
  25.             scanf("%lf", &array[i][j]);
  26.         }
  27.     }
  28. }
  29.  
  30. void row_average(int rows, int cols, double array[rows][cols]){
  31.     int i, j;
  32.     double subtot=0;
  33.     for(i=0; i<ROWS; i++){
  34.         for(j=0; j<COLS; j++){
  35.             subtot+=array[i][j];
  36.         }
  37.         printf("For row %d, the average is %.2lf.\n", i+1, subtot/COLS);
  38.         subtot=0;
  39.     }
  40. }
  41.  
  42. double average(int rows, int cols, double array[rows][cols]){
  43.     int i, j;
  44.     double total=0, average;
  45.     for(i=0; i<ROWS; i++){
  46.         for(j=0; j<COLS; j++){
  47.             total+=array[i][j];
  48.         }
  49.     }
  50.    
  51.     average=total/(rows*cols);
  52.    
  53.     return average;
  54. }
  55.  
  56. double largest(int rows, int cols, double array[rows][cols]){
  57.     int i, j;
  58.     double biggest=-100000;
  59.     for(i=0; i<ROWS; i++){
  60.         for(j=0; j<COLS; j++){
  61.             if(array[i][j]>biggest)
  62.                 biggest=array[i][j];
  63.         }
  64.     }
  65.    
  66.     return biggest;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement