C 赋值,关于二维数组
我想在一个问题上得到一些帮助 我有这个文本文件: 我将文本的内容输入到字符串2d数组中,并将其转换为整数2d数组。当我转换为int数组时,nan被替换为零。现在,我开始查找2d数组中每条线的最大值和最小值,并找到这两条线的平均值,我成功地实现了这一点。我的问题是我想把每一行的平均值赋给零。我尝试使用两个for循环,但它用第一行的平均值替换了所有的零。谁能帮我一下吗 我的代码:C 赋值,关于二维数组,c,C,我想在一个问题上得到一些帮助 我有这个文本文件: 我将文本的内容输入到字符串2d数组中,并将其转换为整数2d数组。当我转换为int数组时,nan被替换为零。现在,我开始查找2d数组中每条线的最大值和最小值,并找到这两条线的平均值,我成功地实现了这一点。我的问题是我想把每一行的平均值赋给零。我尝试使用两个for循环,但它用第一行的平均值替换了所有的零。谁能帮我一下吗 我的代码: int min = 200; int main() { fstream imagefile; ima
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;
}
int最小值=200;
int main(){
fstream图像文件;
打开(“Text.txt”);
if(imagefile.fail()){//检查文件中的错误
cerr数据[k][i];//从文件中读取
}
}
cin.get();
对于(int k=0;k<3;k++){
对于(int i=0;i<11;i++)
{
整数[k][i]=atoi(数据[k][i].c_str());//将字符串转换为int
}
}
cin.get();
对于(int k=0;k<3;k++){//k是行,i是列
对于(int i=0;i<11;i++){
cout您提供的代码存在多个问题。这一行:
imagefile >> data[k][i];
错误C2679:二进制“>>”:未找到接受类型为“std::string”的右操作数的运算符(或没有可接受的转换)
以及:
for(int k=0;k
错误C2065:“i”:未声明的标识符(78)
请尝试发布实际代码。如果您拥有此文件,您的代码将非常有效
注意:您的Text.txt格式应与此格式完全相同。不带空行
#包括
#包括
#包括
使用名称空间std;
int main()
{
ifstream in(“Text.txt”);
char ch='\0';int count=1;//用于计算行数的Ac计数器
而(!in.eof())
{
in.get(ch);
if(ch=='\n')//计算行数
计数++;
}
in.close();
double*avgArray=new double[count];//存储每行最小/最大平均值的数组
int arraySize=计数;
in.open(“Text.txt”);
计数=0;
字符串strTemp=“”;
int temp=0;//获取int值的temp
int sum=0;//计算平均行的和
int i=0;//索引号
int max=0;
int-min=99999;
而(!in.eof())
{
在>>strTemp中;
如果(strTemp==“NaN”)
温度=0;
其他的
温度=原子(strTemp.c_str());
如果(温度>最大值)
最大值=温度;
如果(温度strTemp;
如果(strTemp==“NaN”)
温度=0;
其他的
温度=原子(strTemp.c_str());
数据阵列[i][j]=温度;
if(DataArray[i][j]==0)
DataArray[i][j]=avgaray[i];//在行的0处分配行的平均值
}
}
in.close();
//打印最终数组
for(int i=0;i coutsems两个0在你的最后一行,你想给它们分配平均值还是只给其中一个?最小值除了0对吗?好的!我对代码进行了注释,这可能会帮助你理解。我不懂matlab,请在matlab选项卡中询问。还有什么事?@guestuserasign nan number在它前面?然后分配什么?@guestuser
for (int k = 0; k < integer[k][i]; k++)
#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;
}