10.31.2017

3주차 과제

#include <iostream>
#include <fstream>
using namespace std;

struct course {
        char subject[64];
        int studentNum;
        int score[256];
};

void process(ifstream &fin, course &a)
{
        int sum = 0;
        double ave = 0;
        fin >> a.subject;
        if (!fin.eof()) {
                cout << a.subject;
                fin >> a.studentNum;
                a.score = new int[a.studentNum];
                for (int j = 0; j < a.studentNum; j++) {
                        fin >> a.score[j];
                        sum += a.score[j];
                        ave = sum / (double)a.studentNum;
                }
                cout << "average : " << ave << endl;
        }
}

int main()
{
        course a[5] = {
                { "과목명1", 0, 0 },
                { "과목명2", 0, 0 },
                { "과목명3", 0, 0 },
                { "과목명4", 0, 0 },
                { "과목명5", 0, 0 },
        };

        ifstream fin("data.txt");

        if (fin.fail()) {
                cerr << "input file error"; return -1;
        }
        for (int i = 0; i < 5; i++)
                process(fin, a[i]);

        system("pause");
        return 0;
}

댓글 없음:

댓글 쓰기