将2d阵列的实际值复制到另一个2d阵列c++

copying the actual values of a 2d array to another 2d array c++

本文关键字:2d 阵列 另一个 c++ 复制      更新时间:2023-10-16

我是c++的新手,一整天都在努力思考这个问题。我正在尝试读取一个包含未知行数的数据文件(老师说不会超过100行(。每行包含4个整数,其值范围为0-100。这4列代表了学生在一个学期中的考试成绩。每一行代表一个学生的分数。而每列代表1个测试。我要建立一个二维数组来读取分数。数据文件中的分数进入前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}">

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

初始化包含大小为0的100个CCD_ 1s的向量。为了避免上一个例子中的分段错误,您应该将其初始化为

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

或者用类似的东西替换myfile >> grades[row][columns];

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