Hey, I would some assitance, I wrote this code to find the average between the max and the min and assign that value to the nan, nut i have to chnage up my code, to assign the NaN to the previous non-zero value.
Example 112 113 114 116 NaN would be 112 113 114 116 116.
Text.txt
112 113 114 116 NaN 112 119 109 117 NaN
115 117 116 101 110 Nan 112 118 120 NaN
#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;
}