0

I am trying to use a function call from main to update the values of a vector, then use another helper function to get two particular values from the vector. When I run this code, however, the values in gibbsInfo (the vector in question) are the same every time (the cout statements printing every value of gibbsInfo is the same every iteration of the for loop over i in main). I believe I am misunderstanding something fundamental about the scope of the vector<vector> I am declaring in main and how it relates to the vector<vector> being returned by GibbsInformation. Here is the code. Does anyone have any suggestions?

#include<iostream>
#include<cmath>
#include<vector>

using namespace std;

vector<vector<double>> gibbsInformation(double, double);
vector<double> commonTangent(vector<vector<double>>, double);

int main(){

    for(int j = 1000; j <= 1500; j += 25){
        vector<vector<double>> gibbsInfo = gibbsInformation(0.5, j);
        vector<double> coords = commonTangent(gibbsInfo, j);

        cout << "(" << coords[0] << ", " << coords[2] << ")," << endl;
        cout << "(" << coords[1] << ", " << coords[2] << ")," << endl;

        for(int i = 0; i < gibbsInfo[0].size(); i++){
            cout << "Xa Value: " << gibbsInfo[0][i] << " Gibbs Value: " << gibbsInfo[1][i] << endl;
        }
    }

    return 0;
}

vector<vector<double>> gibbsInformation(double omega, double temp){

    vector<vector<double>> gibbs(2, vector<double>(200,0.0));

    int index = 0;

    for(double x = -0.5; x <= 1.5; x += 0.01){
        gibbs[0][index] = x;
        gibbs[1][index] = x + (0.9 * (1 - x)) + (omega * x * (1 - x)) + (((1000 - (int) temp) / 1000) * x * (1 - x)) + pow(0.5 - x,4);
        index++;
    }

    return gibbs;
}

vector<double> commonTangent(vector<vector<double>> gibbs, double temp){

    vector<double> coordinates(3, 0.0);

    coordinates[2] = temp;//set the third entry of coordinates to the temperature
    double minDistance = 1000;

    for(int i = 1; i < gibbs[1].size(); i++){

        //find the equation for the line
        double slope = (gibbs[1][i] - gibbs[1][i - 1]) / 0.01;

        if(i + 25 < gibbs[1].size()){//translates down by a distance of 0.25 away from the current point in the x direction
            for(int l = i + 25; l < gibbs[1].size(); l++){
                if(abs(gibbs[1][l] - (gibbs[1][i] + (slope * 0.01 * (l - i)))) < minDistance){
                    minDistance = abs(gibbs[1][l] - (gibbs[1][i] + (slope * 0.01 * (l - i))));
                    coordinates[0] = gibbs[0][i];
                    coordinates[1] = gibbs[0][l];
                }
            }
        }
    
    }

    return coordinates;
}
  • You have `vector> gibbsInfo = gibbsInformation(0.5, j);` at the start of the loop. That means every iteration `gibbsInfo` is getting initialized with `gibbsInformation(0.5, j)`, so it will always be the same. – NathanOliver Sep 22 '22 at 12:57
  • 1
    `(1000 - (int) temp) / 1000` in `gibbsInformation` will always result in 0 due to integer division. – TrebledJ Sep 22 '22 at 12:57
  • Why the cast in `(int) temp`? – molbdnilo Sep 22 '22 at 13:00
  • I had casted temp as an integer because I wasn't sure if the division was being done properly to end up as a double in the expression (I didn't know if subtracting (int - double) / int would result in an int or a double). – Nathaniel Hess Sep 22 '22 at 13:05
  • You were correct, however, removing that type cast fixed the problem – Nathaniel Hess Sep 22 '22 at 13:06

0 Answers0