Experiment 8-1-9 Output student scores (20 points)

This question requires writing a program to count and output the average, highest and lowest grades of the students based on the grades of the input students. It is recommended to use dynamic memory allocation.

Input format: The
first line of input first gives a positive integer N, which represents the number of students. The next line gives the scores of N students, separated by spaces.

Output format: output
in the following format:

average = average score
max = highest score
min = lowest score All
results are rounded to two decimal places.

Sample input:
. 3
85 90 95
Output Sample:
Average = 90.00
max = 95.00
min = 85.00
Title Set Collection Portal

#include <stdio.h>
#include <stdlib.h>    //为malloc()、free()提供原型
int main()
{
    
    
    int n;
    double* p, max = -1, min = 101, ave, sum = 0;
    scanf("%d", &n);
    p = (double*)malloc(n * sizeof(double));
    if (p == NULL)
        exit(EXIT_FAILURE);
    for (int i = 0; i < n; i++)
    {
    
    
        scanf("%lf", &p[i]);
        sum += p[i];
        if (p[i] > max)
            max = p[i];
        if (p[i] < min)
            min = p[i];
    }
    printf("average = %.2f\n", sum / n);
    printf("max = %.2f\n", max);
    printf("min = %.2f\n", min);
    free(p);

    return 0;
}

Guess you like

Origin blog.csdn.net/fjdep/article/details/112993989