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