• We just launched and are currently in beta. Join us as we build and grow the community.

C Program - Working with Sub-arrays

JustTrynaHelp

Freelance Platform Wizard
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
169
Likes
187
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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. #include<stdio.h> /* C Standard Input and Output Library*/

  2. /*Variable declarations*/
  3. int

    y;

    /* Declaration of a variable called y of type integer. */
  4. double

    sum=

    0

    ;

    /* Declaration of a variable called sum of type double and initialize it to zero. */
  5. double

    average=

    0

    ;

    /* Declaration of a variable called average of type double and initialize it to zero. */
  6. double

    maximum_average=

    0

    ;

    /* Declaration of a variable called maximum_average of type double and initialize it to zero. */
  7. double

    sub_arrays;

    /* Declaration of a variable called sub_arrays to hold the result of n/y which is the number of sub-arrays. */
  8. int

    array_input_counter,

    results_array_input_counter;

    /* Declaration of two counters */
  9. int

    counter_finding_max_in_loop=

    0

    ;

    /* Declaration of a counter and initialization*/
  10. int

    n;

    /* Declaration of a global variable called n of type integer - the size of the array */

  11. int

    main(

    void

    )

    /* The main method*/
  12. {
  13. /* Brief detail on the screen about the program*/
  14. 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

    "

    )

    ;

  15. printf

    (

    "\n

    Please enter the size of the array (n): \n

    "

    )

    ;

    /* Prompt to instruct user to enter size n */
  16. scanf

    (

    "%d"

    ,&

    n)

    ;

    /* Capture of the integer to be assigned to n*/

  17. /*Variable declaration*/
  18. int

    array_of_numbers[

    n]

    ;

    /* Declaration of an array of n size, n being an integer that is known. */

  19. printf

    (

    "\n

    Please enter y: \n

    "

    )

    ;

    /* Prompt to instruct use to enter y */
  20. scanf

    (

    "%d"

    ,&

    y)

    ;

    /* Capture of the integer to be assigned to variable y*/

  21. sub_arrays=

    (

    (

    double

    )

    n/

    (

    double

    )

    y)

    ;

    /* Divide n by y and round off the value before assigning it to the variable called sub_arrays*/

  22. if

    (

    sub_arrays -

    (

    int

    )

    sub_arrays >

    0.0

    )

    /* Determining whether the subarrays value has decimals*/
  23. {
  24. printf

    (

    "\n

    The value \"

    %.2lf\"

    has decimal points after it. It will be rounded off to %d.\n

    Therefore the array will be divided into %d sub arrays \n

    \n

    "

    ,

    sub_arrays,

    (

    (

    int

    )

    sub_arrays)

    +

    1

    ,

    (

    (

    int

    )

    sub_arrays)

    +

    1

    )

    ;
  25. }
  26. else

    /* In the case that the sub arrays value has no decimals*/
  27. {
  28. printf

    (

    "\n

    The array will be divided into %d sub arrays \n

    "

    ,

    (

    int

    )

    sub_arrays)

    ;
  29. }

  30. printf

    (

    "\n

    Please enter the numbers in the array: \n

    "

    )

    ;

    /* Prompt to instruct use to enter array elements */

  31. double

    array_of_results[

    (

    int

    )

    sub_arrays]

    [

    2

    ]

    ;

    /* Declaration of a 2-dimensional array to hold the sums and averages*/

  32. results_array_input_counter =

    0

    ;

    /* Initialization of the counter for looping through the array of results*/

  33. 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*/
  34. {

    scanf

    (

    "%d"

    ,

    &

    array_of_numbers[

    array_input_counter]

    )

    ;

    /* Capturing of the numbers through scanf()*/
  35. sum=

    sum +

    array_of_numbers[

    array_input_counter]

    ;

    /* Incrementation of the value held by sum as more numbers are captured*/

  36. if

    (

    array_input_counter%

    y==

    0

    &&

    array_input_counter!=

    0

    )

    /* Checking if one sub array is complete*/
  37. {

    array_of_results [

    results_array_input_counter]

    [

    0

    ]

    =

    sum;

    /* Assignment of the value of sum of a particular subarray to the array of results*/
  38. //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));
  39. average =

    sum/

    y;

    /* Calculation of the average*/
  40. 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*/
  41. //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) );
  42. sum=

    0

    ;

    /* Initialization of the value of sum after completing one sub array*/
  43. average=

    0

    ;

    /* Initialization of the value of average after completing one sub array*/
  44. results_array_input_counter=

    results_array_input_counter+

    1

    ;

    /* Incrementation of the counter by 1*/
  45. }
  46. }


  47. if

    (

    (

    (

    int

    )

    sub_arrays *

    y)

    !=

    n)
  48. {
  49. array_of_results [

    results_array_input_counter]

    [

    0

    ]

    =

    sum;
  50. //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));
  51. average =

    sum/

    (

    n-

    (

    (

    int

    )

    sub_arrays *

    y)

    )

    ;
  52. array_of_results [

    results_array_input_counter]

    [

    1

    ]

    =

    average;
  53. //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) );

  54. }

  55. /* Loop to determine the maximum value among the averages of the sub arrays*/
  56. for

    (

    counter_finding_max_in_loop =

    0

    ;

    counter_finding_max_in_loop <=

    (

    (

    int

    )

    (

    (

    double

    )

    n/

    (

    double

    )

    y)

    )

    ;

    counter_finding_max_in_loop++

    )
  57. {

    if

    (

    array_of_results [

    counter_finding_max_in_loop]

    [

    1

    ]

    >

    maximum_average )
  58. {

    maximum_average=

    array_of_results [

    counter_finding_max_in_loop]

    [

    1

    ]

    ;

    }
  59. }


  60. printf

    (

    "\n

    The maximum average is %.2lf\n

    --------------------------------------------------------------------------------\n

    "

    ,

    maximum_average)

    ;
  61. printf

    (

    "\n

    \n

    Let us visualize the array\n

    "

    )

    ;
  62. printf

    (

    " [ "

    )

    ;
  63. for

    (

    array_input_counter =

    1

    ;

    array_input_counter <=

    n;

    array_input_counter++

    )
  64. {

    printf

    (

    "%d "

    ,

    array_of_numbers[

    array_input_counter]

    )

    ;
  65. if

    (

    array_input_counter%

    y==

    0

    &&

    array_input_counter!=

    n)
  66. {

    printf

    (

    "] - [ "

    )

    ;

    }
  67. }
  68. printf

    (

    "]\n

    "

    )

    ;
  69. printf

    (

    "\n

    \n

    The averages are as follows :\n

    "

    )

    ;

  70. if

    (

    sub_arrays -

    (

    (

    int

    )

    (

    (

    double

    )

    n/

    (

    double

    )

    y)

    )

    >

    0.0

    )
  71. {

  72. for

    (

    array_input_counter =

    0

    ;

    array_input_counter <=

    (

    int

    )

    sub_arrays;

    array_input_counter++

    )
  73. {

    printf

    (

    " [ %.2lf ] "

    ,

    array_of_results [

    array_input_counter]

    [

    1

    ]

    )

    ;

  74. }
  75. printf

    (

    "\n

    \n

    --------------------------------------------------------------------------------\n

    "

    )

    ;
  76. }
  77. else

    if

    (

    sub_arrays -

    (

    (

    int

    )

    (

    (

    double

    )

    n/

    (

    double

    )

    y)

    )

    <=

    0.0

    )
  78. {

  79. for

    (

    array_input_counter =

    0

    ;

    array_input_counter <

    (

    int

    )

    sub_arrays;

    array_input_counter++

    )
  80. {

    printf

    (

    " [ %.2lf ] "

    ,

    array_of_results [

    array_input_counter]

    [

    1

    ]

    )

    ;

  81. }
  82. printf

    (

    "\n

    \n

    --------------------------------------------------------------------------------\n

    "

    )

    ;
  83. }




  84. }

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.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

440,010

316,559

316,568

Top