C Program - Working with Sub-arrays

Language
C program with the following specifications : • An array of a variable size (n) • A variable (y) of type integer • The algorithm should divide the array into n/y parts • Calculate the average for each part • Finally find the maximum value among the averages
  1.  
  2.  
  3.  #include<stdio.h> /* C Standard Input and Output Library*/
  4.  
  5.      /*Variable declarations*/
  6.      int y;                                                 /*  Declaration of a variable called y of type integer. */
  7.      double sum=0;                                          /*  Declaration of a variable called sum of type double and initialize it to zero. */
  8.      double average=0;                                      /*  Declaration of a variable called average of type double and initialize it to zero. */
  9.      double maximum_average=0;                              /*  Declaration of a variable called maximum_average of type double and initialize it to zero. */
  10.      double sub_arrays;                                     /*  Declaration of a variable called sub_arrays to hold the result of n/y which is the number of sub-arrays. */
  11.      int array_input_counter, results_array_input_counter;  /*  Declaration of two counters */
  12.      int counter_finding_max_in_loop=0;                     /*  Declaration of a counter and initialization*/
  13.      int n;                                                 /*  Declaration of a global variable called n of type integer - the size of the array */
  14.  
  15.  int main(void)/* The main method*/
  16.  {
  17.     /* Brief detail on the screen about the program*/
  18.     printf(" /*************************************************************************\n *                                                                        *\n *       Mureithi David Wachira                                             *\n *       P15/2204/2011                                                       *\n *                                                                        *\n *       University of Nairobi                                                  *\n *       School of Computing & Informatics                                   *\n *                                                                        *\n *       Course: DATA STRUCTURES AND ALGORITHMS   (CSC 211)                     *\n *       Date:   Tuesday 01st October 2013                                   *\n *                                                                        *\n *       Develop an algorithm with the following characteristics:       *\n *         An array of a variable size (n)                           *\n *         A variable (y) of type integer                            *\n *         The algorithm should divide the array into n/y parts      *\n *         Calculate the average for each part                       *\n *         Finally find the maximum value among the averages         *\n *         Discuss about the number of operations for the algorithm  *\n *                                                                        *\n **************************************************************************/\n            ");
  19.  
  20.     printf("\nPlease enter the size of the array (n): \n");/* Prompt to instruct user to enter size n */
  21.     scanf("%d",&n);                                        /* Capture of the integer to be assigned to n*/
  22.  
  23.     /*Variable declaration*/
  24.     int array_of_numbers[n];                               /*  Declaration of an array of n size, n being an integer that is known. */
  25.  
  26.     printf("\nPlease enter y: \n");                        /* Prompt to instruct use to enter y */
  27.     scanf("%d",&y);                                        /* Capture of the integer to be assigned to variable y*/
  28.  
  29.     sub_arrays=((double)n/(double)y);                      /* Divide n by y and round off the value before assigning it to the variable called sub_arrays*/
  30.  
  31.           if ( sub_arrays - (int)sub_arrays > 0.0)         /* Determining whether the subarrays value has decimals*/
  32.           {
  33.            printf("\nThe value \"%.2lf\" has decimal points after it. It will be rounded off to %d.\nTherefore the array will be divided into %d sub arrays \n \n",sub_arrays, ((int)sub_arrays)+1, ((int)sub_arrays)+1);
  34.           }
  35.           else                                            /* In the case that the sub arrays value has no decimals*/
  36.           {
  37.            printf("\nThe array will be divided into %d sub arrays \n",(int)sub_arrays);
  38.           }
  39.  
  40.     printf("\nPlease enter the numbers in the array: \n");     /* Prompt to instruct use to enter array elements */
  41.  
  42.     double array_of_results[(int)sub_arrays][2];               /* Declaration of a 2-dimensional array to hold the sums and averages*/
  43.  
  44.     results_array_input_counter = 0;                           /* Initialization of the counter for looping through the array of results*/
  45.  
  46.     for ( array_input_counter = 1 ; array_input_counter <= n; array_input_counter++ )          /* Loop to capture the numbers and also calculate the sum of sub arrays*/
  47.      {       scanf( "%d", &array_of_numbers[array_input_counter] );                            /* Capturing of the numbers through scanf()*/
  48.              sum= sum + array_of_numbers[array_input_counter] ;                                /* Incrementation of the value held by sum as more numbers are captured*/
  49.  
  50.              if (array_input_counter%y==0 && array_input_counter!=0)                           /* Checking if one sub array is complete*/
  51.                     {   array_of_results [results_array_input_counter][0] = sum;               /* Assignment of the value of sum of a particular subarray to the array of results*/
  52.                         //printf("\nThe value \"%.2lf\" entered in the array as sum for sub-array (%d)", array_of_results [results_array_input_counter][0], (results_array_input_counter+1));
  53.                         average = sum/ y;                                                      /* Calculation of the average*/
  54.                         array_of_results [results_array_input_counter][1] = average;           /* Assignment of the value of the average of a particular sub array to the array of results*/
  55.                         //printf("\nThe value \"%.2lf\" entered in the array as mean for sub-array(%d)\n\n", array_of_results [results_array_input_counter][1], (results_array_input_counter+1) );
  56.                         sum=0;                                                                 /* Initialization of the value of sum after completing one sub array*/
  57.                         average=0;                                                             /* Initialization of the value of average after completing one sub array*/
  58.                         results_array_input_counter=results_array_input_counter+1;             /* Incrementation of the counter by 1*/
  59.                     }
  60.     }
  61.  
  62.  
  63.     if (((int)sub_arrays * y)!=n)
  64.            {
  65.               array_of_results [results_array_input_counter][0] = sum;
  66.               //printf("\nThe value \"%.2lf\" entered in the array as sum for sub-array (%d)", array_of_results [results_array_input_counter][0], (results_array_input_counter+1));
  67.               average = sum/ (n-((int)sub_arrays * y));
  68.               array_of_results [results_array_input_counter][1] = average;
  69.               //printf("\nThe value \"%.2lf\" entered in the array as mean for sub-array(%d)\n\n", array_of_results [results_array_input_counter][1], (results_array_input_counter+1) );
  70.  
  71.             }
  72.  
  73.        /* Loop to determine the maximum value among the averages of the sub arrays*/
  74.        for ( counter_finding_max_in_loop = 0 ; counter_finding_max_in_loop <= ((int)((double)n/(double)y)); counter_finding_max_in_loop++ )
  75.         { if (array_of_results [counter_finding_max_in_loop][1]>maximum_average )
  76.               {maximum_average= array_of_results [counter_finding_max_in_loop][1];}
  77.         }
  78.  
  79.  
  80.             printf("\n The maximum average is %.2lf\n--------------------------------------------------------------------------------\n ",maximum_average);
  81.             printf("\n\n Let us visualize the array\n");
  82.             printf( " [ ");
  83.             for ( array_input_counter = 1 ; array_input_counter <= n; array_input_counter++ )
  84.                      {    printf( "%d ", array_of_numbers[array_input_counter] );
  85.                           if (array_input_counter%y==0 && array_input_counter!=n)
  86.                              {printf( "] - [ ");}
  87.                      }
  88.             printf( "]\n");
  89.             printf("\n\n The averages are as follows :\n");
  90.  
  91.             if (sub_arrays - ((int)((double)n/(double)y)) > 0.0)
  92.                  {
  93.  
  94.                     for ( array_input_counter = 0 ; array_input_counter <= (int)sub_arrays; array_input_counter++ )
  95.                              {    printf( "  [ %.2lf ]   ", array_of_results [array_input_counter][1] );
  96.  
  97.                              }
  98.                     printf( "\n\n--------------------------------------------------------------------------------\n");
  99.                  }
  100.             else if (sub_arrays - ((int)((double)n/(double)y)) <= 0.0)
  101.                  {
  102.  
  103.                     for ( array_input_counter = 0 ; array_input_counter < (int)sub_arrays; array_input_counter++ )
  104.                              {    printf( "  [ %.2lf ]   ", array_of_results [array_input_counter][1] );
  105.  
  106.                              }
  107.                     printf( "\n\n--------------------------------------------------------------------------------\n");
  108.                  }
  109.  
  110.  
  111.  
  112.  
  113. }

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment