分配,大约2d阵列

时间:2017-03-29 15:51:38

标签: c

我想帮助解决一个问题。

我有这个文本文件:

我将文本内容输入到字符串2d数组中并将其转换为整数2d数组。当我转换为int数组时,nan被替换为零。我现在继续找到2d数组中每行的最大值和最小值,并找到两者的平均值,这是我成功实现的。我的问题是我想将每行的平均值分配给零。我尝试使用两个for循环,但它用第一行的平均值替换所有零。任何人都可以帮助我。

我的代码:

int min = 200;

int main() {

    fstream imagefile;
    imagefile.open("Text.txt");

    if (imagefile.fail()) { // checking for errors in file
        cerr << "Error opening file" << endl;
        exit(1);

    std::string data[3][11];
    int integer[3][11];
    for (int k = 0; k < 3; k++) {

        for (int i = 0; i < 11; i++) {
            imagefile >> data[k][i]; // read from file

        }
    }

    cin.get();

    for (int k = 0; k < 3; k++) {

        for (int i = 0; i < 11; i++)
        {
            integer[k][i] = atoi(data[k][i].c_str()); // converts string to int 
        }
    }
    cin.get();

    for (int k = 0; k < 3; k++) { //k is the row  and i is the column

        for (int i = 0; i < 11; i++) {
            cout << integer[k][i] << "  ";

        }
        cout << endl;
    }

    int row, col;
    for (row = 0; row < 3; row++) { //k is the row  and i is the column

        max = integer[row][0];
        min = integer[row][0];

        for (col = 0; col < 11; col++) {
            if (integer[row][col] > max) { max = integer[row][col]; }
            if ((integer[row][col] < min) && (integer[row][col] != 0)) { min = integer[row][col]; }
            //if (integer[row][col] == 0) { avg == integer[row][col]; }

            sum = max + min;
            avg = sum / 2;
        }
            for (int k = 0; k < integer[k][i]; k++) {  //k is the row  and i is the column
                for (int i = 0; i < 11; i++) {
                    if (integer[k][i] == 0) {

                        integer[k][i] = avg;

                    }
                    cout << integer[k][i] << "  ";

                }
                cout << endl;
            }

        cout << max << " ";
        cout << min << " ";
        cout << avg << " " << endl;
    }

    imagefile.close();
    system("pause");
    return 0;
}

2 个答案:

答案 0 :(得分:0)

您提供的代码存在多个问题。这一行:

    imagefile >> data[k][i];

错误C2679:二进制'&gt;&gt;' :没有找到哪个运算符采用'std :: string'类型的右手操作数(或者没有可接受的转换)

    for (int k = 0; k < integer[k][i]; k++) 

错误C2065:'i':未声明的标识符(78)

尝试发布实际代码。

答案 1 :(得分:0)

如果您有此文件,那么您的代码将完美运行。

  
    

注意:您的Text.txt应该是这种格式。没有空行。

  
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    ifstream in("Text.txt");
    char ch = '\0'; int count = 1; //Ac counter to count number of rows
    while (!in.eof())
    {
        in.get(ch);
        if (ch == '\n') //counting lines
            count++;
    }
    in.close();
    double *avgArray = new double[count]; //array to store avg of min/max of every row
    int arraySize = count;
    in.open("Text.txt");
    count = 0;
    string strTemp = "";
    int temp = 0; //temp to get int values
    int sum = 0; //calculate sum of avery row
    int i = 0; //index number
    int max = 0;
    int min = 99999;
    while (!in.eof())
    {
        in >> strTemp;
        if (strTemp == "NaN")
            temp = 0;
        else
            temp = atoi(strTemp.c_str());
        if (temp > max)
            max = temp;
        if (temp < min && temp!=0)
            min = temp;
        in.get(ch);
        if (ch == '\n' || in.eof()) //if new line occured 
        {
            avgArray[i] = double(max + min) / 2.0; //store avg in abg array
            max = 0;
            min = 99999;
            i++; //counter of avg array
        }
    }
    in.close();
    //for (int i = 0; i < arraySize; i++) //printing
        //cout << avgArray[i]<<endl; //avg of all rows
    in.open("Text.txt");
    i = 0;
    int col = 0; //counter for cols
    int SizeCol = 0;
    temp = 0;
    strTemp = "";
    double **DataArray = new double*[arraySize]; //2D array to store file data
    while (!in.eof())
    {
        in >> strTemp;
        if (strTemp == "NaN")
            temp = 0;
        else
            temp = atoi(strTemp.c_str());
        in.get(ch);
        col++; //counting columns
        if (ch == '\n')
        {
            DataArray[i] = new double[col]; //making dynmically cols
            i++;
            SizeCol = col;
            col = 0;
        }
        if (in.eof())
            DataArray[i] = new double[col]; //for last line of file
    }
    in.close();
    in.open("Text.txt");

    for (int i = 0; i < arraySize; i++)
    {
        for (int j = 0; j < SizeCol; j++)
        {
            in >> strTemp;
            if (strTemp == "NaN")
                temp = 0;
            else
                temp = atoi(strTemp.c_str());
            DataArray[i][j]=temp;
            if (DataArray[i][j] == 0)
                DataArray[i][j] = avgArray[i]; //Assigning avg of row at row's 0 place
        }
    }
    in.close();

    //Printing the final array
    for (int i = 0; i < arraySize; i++)
    {
        for (int j = 0; j < SizeCol; j++)
        {
            cout<< DataArray[i][j]<<" "; //printing
        }
        cout << endl;
    }
    cout << endl << endl;
    ofstream out("Text.txt");
    //Wrting the final array into Text.txt
    for (int i = 0; i < arraySize; i++)
    {
        for (int j = 0; j < SizeCol; j++)
        {
            out << DataArray[i][j] << " "; //Writing
        }
        out << endl;
    }
    delete[] avgArray;
    for (int i = 0; i < arraySize; i++)
        delete DataArray[i];
    delete[] DataArray;
    system("pause");
    return 0;
}