我需要计算每个学生的平均成绩

时间:2017-05-02 18:00:33

标签: c++

/*This code creates two text files : Mokiniai.txt and Vidurkiai.txt
In Mokiniai.txt there are stored each students grades, in Vidurkiai there
should be calculated each students average grade*/

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    int n, k, isvisopazymiu, i, pazymiai;
    double vidurkis; //average grade
    ofstream myfile;
    myfile.open("Mokiniai.txt");
    cout << "parasykite kiek mokiniu yra" << endl; cin >> n; //how many students
    myfile << n << endl;
    cout << "parasykite kiek yra mokiniu pazymiu"; cin >> isvisopazymiu; // how many grades one student has
    for (k = 1; k <= n; k++) {
        for (i = 1; i <= isvisopazymiu; i++) {
            cout << "Parasykite kokie yra mokiniu pazymiai "; cin >> pazymiai; // what are students grades
            myfile << " " << pazymiai;
        }
        myfile << endl;
    }
    myfile.close();
    myfile.open("Vidurkiai.txt"); //trying to calculate average students grades on different file
    for (k = 1; k <= n; k++) {
        vidurkis = pazymiai / isvisopazymiu; //calculating students grades
        myfile << k << " " << vidurkis << endl;
    }
    myfile.close();
    return 0;
}

我的问题是:vidurkiai.txt文件有问题,但我不知道出了什么问题。

例如:第一个学生的成绩是:7 8 9 7 8,平均成绩应该是7,8,但编码后,在vidurkiai.txt文件中显示,平均成绩为1。

1 个答案:

答案 0 :(得分:1)

它没有按预期显示成绩的原因是因为你没有将这些值加起来。而是取所有等级变量的平均值 &#39; pazymiai&#39;只存储最后输入的变量。为了完成这项工作,你可以使用一个&#39;数组&#39;存储每个数组元素的等级总和。

cin>>pazymiai;
a[k]+=pazymiai;

在计算成绩时使用

 vidurkis = a[k] / isvisopazymiu;