0

I have a 2 dimension array in my cpp program which stores double values across eight columns and three rows.I have a function to determine the smallest value per row.Now i want to change the value of that smallest variable.I am passing the array via pointers and its challenging to me.Below is the getMaxMin() which gets the smallest values.Any help will be appreciated.

 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;
}
3
  • When do you want to change the value of the smallest variable? Commented Mar 19, 2014 at 16:32
  • You are passing a double ** in so the values in scores will be directly modified. You don't need to return anything from this function. Commented Mar 19, 2014 at 16:37
  • Why is the function called getMaxMin? it is totally unclear what the function has to do. Commented Mar 19, 2014 at 16:40

5 Answers 5

1

When you save small also save the indexes:

// ...
if( small > scores[i][j] )
{
    small = scores[i][j];
    small_i = i;
    small_j = j;
}


// later
scores[small_i][small_j] = //...

I guess for this scenario you only need to store the column index since you are doing it row by row. This is a more generic version.

Sign up to request clarification or add additional context in comments.

Comments

1
    int smalli,smallj;
....
      if(small>scores[i][j]){
                small=scores[i][j];
                smalli = i;
                smallj = j;
           }

    ...

    scores[smalli][smallj] = newval;

Comments

0

Does this answer your question?

 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;
}

Comments

0

I may be missing something, but why take the address of the smallest and use that to assign a new value?

(NB: I may well be missing something, haven't coded c++ in anger in . . . Oh crap, years this year!)

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;
    }
    *small = 100.0; // Set new value here
    return scores;
}

Comments

0

There are many ways to do this, but a simple way would be to just save the indices as well.

To keep track of the smallest value per row:

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;
}

To keep track of the smallest value in the entire array:

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.