C + + homework – score statistics

Time:2021-6-28

Achievement statistics

1) Write a function randscore to generate an integer of [0100] interval( Tip: use rand function)

int randScore(){
    int num = rand() % 101;
    return num;
}

2) Write a function, using the above randscore function, randomly generate the results of a course for the whole class.

void stuRandomScore(int stuScore[]){
    for (int i = 0; i < stuNum; i++)
        stuScore[i] = randScore();
    for (int i = 0; i < stuNum; i++)
        Cout < < I + 1 < < students' scores are: < stuscore [i] < endl;
}

3) Write a function score2grade to realize the conversion from the hundred point system to the five point system.

void Score2Grade(int stuScore[]){
    for (int i = 0; i < stuNum; i++)
        if (stuScore[i] >= 50)
            score2Grade[i] = float(stuScore[i]) / 10 - 5;
        else
            score2Grade[i] = 0;
    for (int i = 0; i < stuNum; i++)
        Cout < "No." < I + 1 < "grade points of students: < score2grade [i] < endl;
}

4) Write a function to use score2grade to count the proportion of different grades of the whole class.

void PersentGrade(float score2Grade[5]){
    int per[5] = {0};
    for (int i = 0; i < stuNum; i++){
        if (score2Grade[i] >= 0 && score2Grade[i] <= 1)
            per[0]++;
        if (score2Grade[i] > 1 && score2Grade[i] <= 2)
            per[1]++;
        if (score2Grade[i] > 2 && score2Grade[i] <= 3)
            per[2]++;
        if (score2Grade[i] > 3 && score2Grade[i] <= 4)
            per[3]++;
        if (score2Grade[i] > 4 && score2Grade[i] <= 5)
            per[4]++;
    }
    for (int i = 0; i < 5; i++){
        persentGrade[i] = float(per[i]) / stuNum;
        Cout < "the proportion is: < persentgrade [i] < endl;
    }
}

5) Write a function to calculate the average score and standard deviation of the whole class.

float Average(int *stuScore){
    int sum = 0;
    for (int i = 0;  i < stuNum;  I + +) // sum
        sum += stuScore[i];
    return float(sum) / stuNum; //  Get the average
}
float StandardDeviation(int *stuScore){
    float sum = 0;
    float average = Average(stuScore);
    for (int i = 0;  i < stuNum;  I + +) // sum
        sum += pow(stuScore[i] - average, 2);
    return sqrt(sum / stuNum);
}

The procedures are summarized below

#include <iostream>
#include <stdlib.h> 
#include <math.h>
using namespace std;
class MyScores{
public:
    int stuNum;
    int *stuScore;
    float *score2Grade;
    float *persentGrade;
    int randScore(){
        int num = rand() % 101;
        return num;
    }
    void stuRandomScore(int stuScore[]){
        for (int i = 0; i < stuNum; i++)
            stuScore[i] = randScore();
        for (int i = 0; i < stuNum; i++)
            Cout < < I + 1 < < students' scores are: < stuscore [i] < endl;

    }
    void Score2Grade(int stuScore[]){
        for (int i = 0; i < stuNum; i++)
            if (stuScore[i] >= 50)
                score2Grade[i] = float(stuScore[i]) / 10 - 5;
            else
                score2Grade[i] = 0;
        for (int i = 0; i < stuNum; i++)
            Cout < "No." < I + 1 < "grade points of students: < score2grade [i] < endl;
    }
    void PersentGrade(float score2Grade[5]){
        int per[5] = {0};
        for (int i = 0; i < stuNum; i++){
            if (score2Grade[i] >= 0 && score2Grade[i] <= 1)
                per[0]++;
            if (score2Grade[i] > 1 && score2Grade[i] <= 2)
                per[1]++;
            if (score2Grade[i] > 2 && score2Grade[i] <= 3)
                per[2]++;
            if (score2Grade[i] > 3 && score2Grade[i] <= 4)
                per[3]++;
            if (score2Grade[i] > 4 && score2Grade[i] <= 5)
                per[4]++;
        }
        for (int i = 0; i < 5; i++){
            persentGrade[i] = float(per[i])
                / stuNum;
            Cout < < I < "~" < I + 1 < "the proportion is: < persentgrade [i] < endl;
        }
    }
    float Average(int *stuScore){
        int sum = 0;
        for (int i = 0;  i < stuNum;  I + +) // sum
            sum += stuScore[i];
        return float(sum) / stuNum; //  Get the average
    }
    float StandardDeviation(int *stuScore)
    {
        float sum = 0;
        float average = Average(stuScore);
        for (int i = 0;  i < stuNum;  I + +) // sum
            sum += pow(stuScore[i] - average, 2);
        return sqrt(sum / stuNum);
    }
};

void main(){
    MyScores t;
    cin >> t.stuNum;
    t.stuScore = new int[t.stuNum];
    t.score2Grade = new float[t.stuNum];
    t.persentGrade = new float[5];
    t.stuRandomScore(t.stuScore);
    cout << endl;
    t.Score2Grade(t.stuScore);
    cout << endl;
    t.PersentGrade(t.score2Grade);
    cout << endl;
    float average = t.Average(t.stuScore);
    Cout < average < endl;
    float sd = t.StandardDeviation(t.stuScore);
    Cout < SD < endl;
    system("pause");
}

C + + homework - score statistics

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]