C++ 更改c+中的数组值+;
我的cpp程序中有一个二维数组,它存储八列三行的双值。我有一个函数来确定每行的最小值。现在我想更改最小变量的值。我通过指针传递数组,这对我来说是一个挑战。下面是getMaxMin()它得到的值最小。任何帮助都将不胜感激C++ 更改c+中的数组值+;,c++,arrays,pointers,C++,Arrays,Pointers,我的cpp程序中有一个二维数组,它存储八列三行的双值。我有一个函数来确定每行的最小值。现在我想更改最小变量的值。我通过指针传递数组,这对我来说是一个挑战。下面是getMaxMin()它得到的值最小。任何帮助都将不胜感激 double **getMaxMin(double **scores){ for(int i=0;i<3;i++){ double small=scores[i][0]; for(int j=1;j<8;j++){
double **getMaxMin(double **scores){
for(int i=0;i<3;i++){
double small=scores[i][0];
for(int j=1;j<8;j++){
if(small>scores[i][j]){
small=scores[i][j];
}
}
cout<<small<<endl;
}
return scores;
}
double**getMaxMin(双倍**分数){
对于(inti=0;i这是否回答了您的问题
double **getMaxMin(double **scores){
for(int i=0;i<3;i++){
double small=scores[i][0];
int best_j = 0; // NEW
for(int j=1;j<8;j++){
if(small>scores[i][j]){
small=scores[i][j];
best_j = j; // NEW
}
}
cout<<small<<endl;
scores[i][best_j] = 42.0f; // NEW
}
return scores;
}
double**getMaxMin(双倍**分数){
对于(int i=0;i当您保存small
时,也保存索引:
// ...
if( small > scores[i][j] )
{
small = scores[i][j];
small_i = i;
small_j = j;
}
// later
scores[small_i][small_j] = //...
我想在这种情况下,您只需要存储列索引,因为您是逐行进行的。这是一个更通用的版本。我可能遗漏了一些内容,但是为什么要使用最小的地址并使用它来分配新值呢
int smalli,smallj;
....
if(small>scores[i][j]){
small=scores[i][j];
smalli = i;
smallj = j;
}
...
scores[smalli][smallj] = newval;
(NB:我可能会丢失一些东西,没有在愤怒中编码C++……哦,垃圾,<强>年!< /强>)
double**getMaxMin(双倍**分数)
{
对于(inti=0;i有很多方法可以做到这一点,但是一个简单的方法就是保存索引
跟踪每行的最小值:
double **getMaxMin(double **scores){
for(int i=0;i<3;i++){
double small=scores[i][0];
int small_j = 0;
for(int j=1;j<8;j++){
if(small>scores[i][j]){
small=scores[i][j];
small_j = j;
}
}
cout<<small<<endl;
// Now you can change the value of the smallest variable for that row
//small[i][small_j] = yourvalue
}
return scores;
}
double **getMaxMin(double **scores){
for(int i=0;i<3;i++){
double small=scores[i][0];
int small_j = 0;
int small_i = 0;
for(int j=1;j<8;j++){
if(small>scores[i][j]){
small=scores[i][j];
small_j = j;
small_i = i;
}
}
cout<<small<<endl;
}
// Now you can do something with the smallest value in the entire array
// small[small_i][small_j] = yourvalue
return scores;
}
double**getMaxMin(双倍**分数){
对于(int i=0;i)您何时要更改最小变量的值?您正在传入一个double**
,以便直接修改scores
中的值。您不需要从此函数返回任何内容。为什么函数名为getMaxMin?完全不清楚该函数必须执行什么操作。
double **getMaxMin(double **scores){
for(int i=0;i<3;i++){
double small=scores[i][0];
int small_j = 0;
int small_i = 0;
for(int j=1;j<8;j++){
if(small>scores[i][j]){
small=scores[i][j];
small_j = j;
small_i = i;
}
}
cout<<small<<endl;
}
// Now you can do something with the smallest value in the entire array
// small[small_i][small_j] = yourvalue
return scores;
}