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

insert and calculate Array elements in C * Example Run: * $ ./array * Number of

ID: 3882308 • Letter: I

Question

insert and calculate Array elements in C

* Example Run:
* $ ./array
* Number of values (up to 100): 3
* Reading 3 values...
* Enter value 0: 98.6
* Enter value 1: 14.2
* Enter value 2: -3
* Array 1 Contents: 225.00 19.00 8.00 0.00 42.00 -20.00 1.00 -99.00 6.00 200.00
* Array 1 Statistics: avg=38.20, min=-99.00, max=225.00
* Array 2 Contents: 98.60 14.20 -3.00
* Array 2 Statistics: avg=36.60, min=-3.00, max=98.60
*/

#include <stdio.h>
#include <float.h>

#define MAX_ARRAY 100

float array1[] = {
225.0,
19.0,
8.0,
0.0,
42.0,
-20.0,
1.0,
-99.0,
6.0,
200.0
};

float array2[MAX_ARRAY] = { 0 };

void read_array(float numbers[], int count);
void print_array(float numbers[], int count);
void calc_stats(float numbers[], int count,
float *avg_p, float *min_p, float *max_p);

int main(void) {
/* Find out how many numbers the user wants to enter */
int num;
printf("Number of values (up to %d): ", MAX_ARRAY);
scanf("%d", &num);
read_array(array2, num);

float avg, min, max;

/* Print out our pre-defined sample array */
printf("Array 1 Contents: ");
print_array(array1, 10);

/* Calculate its statistics */
calc_stats(array1, 10, &avg, &min, &max);
printf("Array 1 Statistics: avg=%.2f, min=%.2f, max=%.2f ",
avg, min, max);

/* Now, let's do the same with the user-defined array: */
printf("Array 2 Contents: ");
print_array(array2, num);
calc_stats(array2, num, &avg, &min, &max);
printf("Array 2 Statistics: avg=%.2f, min=%.2f, max=%.2f ",
avg, min, max);

return 0;
}


void print_array(float numbers[], int count) {
int i;
for (i = 0; i < count; ++i) {
printf("%.2f ", numbers[i]);
}
printf(" ");
}

void read_array(float numbers[], int count) {


}

/* TODO: Read in values and store them in an array. */
  
}


void calc_stats(float numbers[], int count,
float *avg_p, float *min_p, float *max_p) {
/* TODO: Loop through the array of numbers to calculate their average as
* well as the minimum and maximum values. Once you have calculated these,
* update the output parameters (avg_p, min_p, and max_p). */
}

Explanation / Answer

#include <stdio.h>
#include <float.h>
#define MAX_ARRAY 100
float array1[] = {
    225.0,
    19.0,
    8.0,
    0.0,
    42.0,
    -20.0,
    1.0,
    -99.0,
    6.0,
    200.0
};
float array2[MAX_ARRAY] = { 0 };
void read_array(float numbers[], int count);
void print_array(float numbers[], int count);
void calc_stats(float numbers[], int count,
        float *avg_p, float *min_p, float *max_p);
int main(void) {
    /* Find out how many numbers the user wants to enter */
    int num;
    printf("Number of values (up to %d): ", MAX_ARRAY);
    scanf("%d", &num);
    read_array(array2, num);
    float avg, min, max;
    /* Print out our pre-defined sample array */
    printf("Array 1 Contents: ");
    print_array(array1, 10);
    /* Calculate its statistics */
    calc_stats(array1, 10, &avg, &min, &max);
    printf("Array 1 Statistics: avg=%.2f, min=%.2f, max=%.2f ",
            avg, min, max);
    /* Now, let's do the same with the user-defined array: */
    printf("Array 2 Contents: ");
    print_array(array2, num);
    calc_stats(array2, num, &avg, &min, &max);
    printf("Array 2 Statistics: avg=%.2f, min=%.2f, max=%.2f ",
            avg, min, max);
    return 0;
}

void print_array(float numbers[], int count) {
    int i;
    for (i = 0; i < count; ++i) {
        printf("%.2f ", numbers[i]);
    }
    printf(" ");
}
void read_array(float numbers[], int count) {

   
    /* TODO: Read in values and store them in an array. */
    for (int i = 0; i < count; ++i)
    {
    scanf("%f",&numbers[i]); // read count number of values from user
    }
   
}

void calc_stats(float numbers[], int count,float *avg_p, float *min_p, float *max_p) {
    /* TODO: Loop through the array of numbers to calculate their average as
     * well as the minimum and maximum values. Once you have calculated these,
     * update the output parameters (avg_p, min_p, and max_p). */
    
     //initialize avg,min and max to first element in the array
     *avg_p = numbers[0];
     *min_p = numbers[0];
     *max_p = numbers[0];
     for (int i = 1; i < count; ++i)
     {
    
     *avg_p = *avg_p + numbers[i];
    
     if(*min_p > numbers[i])
     *min_p = numbers[i];
    
     if(*max_p < numbers[i])
     *max_p = numbers[i];
     }
     *avg_p = *avg_p/count;   // compute avg = sum/count
}

Output:

Number of values (up to 100):10

10.2
20.3
30.4
40.5
50.6
-10.9
-20.6
-2
100.7
2.3

Array 1 Contents: 225.00 19.00 8.00 0.00 42.00 -20.00 1.00 -99.00 6.00 200.00
Array 1 Statistics: avg=38.20, min=-99.00, max=225.00
Array 2 Contents: 10.20 20.30 30.40 40.50 50.60 -10.90 -20.60 -2.00 100.70 2.30
Array 2 Statistics: avg=22.15, min=-20.60, max=100.70