Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program #4: Statistical Analysis of an Array The contents of an int array will b

ID: 3854305 • Letter: P

Question

Program #4: Statistical Analysis of an Array The contents of an int array will be provided to you on eLearning. You can create and initialize an array with those contents by using an array initialization list, as follows: intra inArr [ ] = { // copy in array contents here Every element of this array is an int between 0 and 3000, except for the last element, which is 1. Since we did not provide a size declarator in the array declaration, nor do we know how many elements are in the array until we process it, that last-1 can be used as a sentinel value for our array processing. It is not considered part of the data set in the array and should not be included in the following calculations. Write a C program that scans through the array contents (everything except the -1) and determines the following statistics on it: 1)the number of elements 2) the average of the elements 3) the minimum and maximum values 4) the second to last minimum and second to last maximum values (if the min or max values are duplicated, use the duplicated value not the next unique value) Your program should analyze the array, determine the above statistics, and print out the results in the following format. Total number: Average: Min element: Min2 element: Max element: Max2 element: 700 2462.9 15 21 2984 2976 The average should be printed as a floating point value with one decimal position. All numbers should be right justified as indicated. "Min2" refers to the second-to-smallest minimum element, and “Max2” refers to the second-to-largest maximum element. Note that it is not necessary to create an output file for this project. Printing the results to the console will be sufficient.

Explanation / Answer

NOTE: I have initialized the array elements you have provided and tested the array operations mentioned. Please let me know if you face any issues.

Code:

#include <stdio.h>

int main()
{
   /* initializing array */
   int mainArr[] = {120,80,72,143,123,124,125,3000,2999,3000,82,876,986,345,1990,2367,98,2,444,993,635,283,544,923,18,543,777,234,549,864,39,97,986,986,1,2999,473,776,9,23,397,15,822,1927,1438,1937,1956,7,29,-1};
   /* declaring and initializing variables */
   int i = 0, total = 0, min, max, min2, max2, sum = 0;
  
   /* assigning max and min variables with first array location */
   max = min = max2 = min2 = mainArr[0];
  
   printf(" Given Array is: ");
   for(i = 0; mainArr[i] != -1; i++){
       printf(" %d", mainArr[i]);
       total++;
       sum += mainArr[i];

       /* getting max from array */
       if(mainArr[i] > max){
           max2 = max;
           max = mainArr[i];
       }
       /* getting second max */
       else if(mainArr[i] > max2 && mainArr[i] != max){
           max2 = mainArr[i];
       }

       /* getting min from array */
       if(mainArr[i] < min){
           min2 = min;
           min = mainArr[i];
       }
       /* getting second min */
       else if(mainArr[i] < min2 && mainArr[i] != min){
           min2 = mainArr[i];
       }
   }
   printf(" Total number:   %20d", total);
   printf(" Average:    %20.1f", (float)(sum/total));
   printf(" Min element: %20d", min);
   printf(" Min2 element: %20d", min2);
   printf(" Max element: %20d", max);
   printf(" Max2 element: %20d", max2);
   printf(" ");

   return 0;
}

Execution and output:

Unix Terminal> cc array_oper.c
Unix Terminal> ./a.out

Given Array is:
120 80 72 143 123 124 125 3000 2999 3000 82 876 986 345 1990 2367 98 2 444 993 635 283 544 923 18 543 777 234 549 864 39 97 986 986 1 2999 473 776 9 23 397 15 822 1927 1438 1937 1956 7 29

Total number:                      49
Average:                    780.0
Min element:                     1
Min2 element:                    2
Max element:                  3000
Max2 element:                 2999
Unix Terminal>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote