将2d数组的实际值复制到另一个2d数组c ++

时间:2014-03-14 00:35:54

标签: c++ vector multidimensional-array fstream filereader

我是c ++的新手,而且我一整天都试图绕过这一切。我正在尝试读取包含未知行数的数据文件(老师说不会超过100行)。每行包含4个整数,其值范围为0-100。 4列代表学期内的学生考试成绩。每行/每行代表一个学生的分数。而每列代表1次测试。我要设置一个2D数组来读取分数。来自数据文件的分数进入前4列,并且对于每个学生/行,在第5列中计算所有4个测试的平均值。由于我不知道文件中有多少学生/行,因此我将有0到n-1行。在第n行,我计算每个位置行[0到4]作为其上方整个列的平均值。所有学生的平均成绩在每个栏目行(第n)的底部计算,每个学生在第5栏中计算的所有四个考试的平均成绩。每个学生的平均考试平均值在第5栏底部计算(成绩[第n行] [5] = {第5列中所有行的平均值}

我确信一个更广泛的知识库将是非常有益的,但这是一个家庭作业,所以我不得不尝试。我认为指针对于这项任务来说是最有益的理解;但是,我还没到那里。

这是我的第一次尝试:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <vector>

using namespace std;
double grades [100][5] = {0};
int row = 0;
int column = 0;

int main() {
char line[100];
ifstream myfile("sturec.dat");

if(myfile){
    while (getline(myfile, line)) {
        if (line == char) {
            //grades[row][column] = 
            cout << line << endl;
            for (int i = 0; i <= line.length(); i++) {
                if (line[i] != line[0]) && (i == " ") && (line[i+1] == char) {
                    column += 1;
                }
                else if (line[i] && line[i+1] && line[i+2] !== " ") {
                    grades[row][column] = {line[i] + line[i+1] + line[i+2]};
                else if (line[i] && line[i+1] !== " " ) {
                    grades [row][1] = {line[i] + line[i+1]};
                }


            }
            row += 1;
        }
    }
}

} 我放弃了,并试图创建一个向量的向量来填充文件。我花了很长时间才弄清楚如何从文件中实际输入数据。最后我诉诸:

#include //all the necessary libraries
using namespace std;
double grades[100][5] = {0}//the 2d array i had hoped to populate with the data from file

int main(){
ifstream myfile("filename");
rowCount = 0;
int t1, t2, t3, t4;
while(myfile >> t1 >> t2 >> t3 >> t4){
    cout << t1 << " " << t2 <<  " " << t3 << " " << t4 << endl;
            cout << "this is row 1 + : " << rowCount << endl;
//at this point i was just happy to have successfully read the file and printed the values.

            rowCount ++;
}
for(int i = 0; i < 4; i++){
    grades[rowCount][i]// this is where i got lost i tried multiple different things in attempt to populate "grades" by trying to create temp arrays to hold the values of t1,2,3,4 in order to parse them and place them in "grades", but to no avail. Any direction would be appreciated.
}

只是为了展示我的一些不同的方法,我会发布我所拥有的类似代码的略有不同的版本。

``

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;
double grades[100][5] = {0};

int main() {
    ifstream myfile("sturec.dat");
    int rowCount = 0;
    int tempArray[100][4] = {0};
    char test [4] = {0};
    int i = 0;      
        while (myfile >> tempArray[rowCount][i]) {
            cout << rowCount << endl << " " << i << endl;
            cout << "temp array: " << tempArray<< endl;
            while(i < 4){
                i++;
                rowCount++;
            }
        }
        /*for (int c = 0; c <= rowCount; c++) {
            for (int r = 0; r <= i; r++) {
                grades[rowCount][i] = (tempArray[r][c]);
            }
        }
    cout<< tempArray << endl << grades << endl; 
    */
}
    /*double final;
    while (myfile >> grades[rowCount][test]) {
        //cout << t1 << " " << t2 <<  " " << t3 << " " << t4 << endl;
        cout << grades << endl;
        cout << rowCount << endl;
                //cout << grades[rowCount][]
        rowCount ++;
    }


}
   */

下一个

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;
double grades[100][5] = {0};

int main() {
ifstream myfile("sturec.dat");
int rowCount = 0;
int tempArray[100] = {0};
int t1, t2, t3, t4;
while (myfile >> t1 >> t2 >> t3 >> t4) {
    cout << t1 << " " << t2 <<  " " << t3 << " " << t4 << endl;
    int test [4] = {0};
    for (int i = 0; (i < sizeof(test) - 1); i++) {
            grades[rowCount][i] = {tempArray};
    }
}
double final;
while (myfile >> grades[rowCount][i]) {

    cout << grades << endl;
    cout << rowCount << endl;
            //cout << grades[rowCount][]
    rowCount ++;
}



vector < vector <int> > grades(100);
//vector <int> rows(4/*,0*/); // assigns 4 columns to rows vector with value of zero
//rows.assign(5,0);
int row = 0;

myfile.open("sturec.dat", ios::in); //opens file
if (myfile.is_open()) {

    cout << "file opened" << endl;

    string line;
    vector<string> myLines;
    while (getline(myfile, line)) { //gets lines using myfile and puts them in line
        myLines.push_back(line);

        cout << "string line contains: " << line << endl;
        for (int columns = 0; columns <= 4 /*sizeof(rows)*/; columns ++) {
            myfile >> grades[row][columns];         cout << "2" << endl;

        }
        row += 1;
    }
}

else cout << "cannot open file" << endl;
myfile.close(); cout << "closed file" << endl;
return 0;

//cout << grades;

}

最后一个:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

int main() {
ifstream myfile;
vector < vector <int> > grades(100);
//vector <int> rows(4/*,0*/); // assigns 4 columns to rows vector with value of zero
//rows.assign(5,0);
int row = 0;

myfile.open("sturec.dat", ios::in); //opens file
if (myfile.is_open()) {
    cout << "1" << endl;
    cout << "file opened" << endl;

    string line;
    vector<string> myLines;
    while (getline(myfile, line)) { //gets lines using myfile and puts them in line
        myLines.push_back(line);

        cout << "string line contains: " << line << endl;
        for (int columns = 0; columns <= 4 /*sizeof(rows)*/; columns ++) {
            myfile >> grades[row][columns];         cout << "2" << endl;

        }
        row += 1;
    }
}

else cout << "cannot open file" << endl;
myfile.close(); cout << "closed file" << endl;
return 0;

//cout << grades;

} 这个实际上让我得到了文件的第一行,但我不能让这个错误消失: 运行命令:第1行:13531分段错误:11 ./“$ 2”“$ {@:3}”

1 个答案:

答案 0 :(得分:0)

vector < vector <int> > grades(100);

初始化一个包含100个vector<int>大小为0的向量。要避免上一个示例中的分段错误,您应该将其初始化为

vector < vector <int> > grades(100,vector<int>(5,0));

或用{/ p>之类的内容替换myfile >> grades[row][columns];

int tmp;
myfile >> tmp;
grades[row].push_back(tmp);