Attempting to use the banker's algorithm and trying to calculate need using C++. I was trying to get the Max / Allocation vector to subtract from each other and can't seem to get the right method of using math on vector of vectors.
The whole program is supposed to output the Allocation / Max / Need and I haven't been able to find resources online on manipulating 2D Vectors, which are required for this code. I will continue my online search and return if I find anything to either edit this or take this down, but thanks in advance for any help that anyone can provide.
#include <iostream>
#include <fstream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main(int argc, char** argv)
{
int m, n, q;
//vectors we're storing to
vector<vector<int> > alloc;
vector<vector<int> > max;
vector<vector<int> > need;
ifstream fin(argv[1]);
fin >> n;
fin >> m;
cout << "There are " << m << " resource types.\n\n";
cout << "There are " << n << " processes in the system.\n\n";
//inserting the data into the Allocation Array
for(int i=0; i<n; i++)
{
vector<int>temp;
for(int j=0; j<m; j++)
{
fin >> q;
temp.push_back(q);
}
alloc.push_back(temp);
}
//ALLOCATION
//Printing headings
cout << "The Allocation Matrix is...";
cout << "\n ";
for(int i=0; i<m; i++)
cout << (char)('A' + i) <<" ";
cout << endl;
//printing the allocation vector
for (int i = 0; i<alloc.size(); i++)
{
cout << i << ": ";
for(int j = 0; j<alloc[i].size();j++)
{
cout << alloc[i][j] << " ";
}
cout << endl;
}
cout << endl;
//MAX
//inserting data into the max vector
for(int i=0; i<n; i++)
{
vector<int>temp;
for(int j=0; j<m; j++)
{
fin >> q;
temp.push_back(q);
}
max.push_back(temp);
}
//printing resource titles
cout << "The Max Matrix is...";
cout << "\n ";
for(int i=0; i<m; i++)
cout << (char) ('A' + i) << " ";
cout << endl;
//printing the max vector
for (int i = 0; i<max.size(); i++)
{
cout << i << ": ";
for(int j = 0; j<max[i].size();j++)
{
cout << max[i][j] << " ";
}
cout << endl;
}
cout << endl;
cout << "The Need Matrix is...";
cout << "\n ";
for(int i=0; i<m; i++)
cout << (char)('A' + i) << " ";
cout << endl;
for (int i = 0; i < n; i++)
{
cout << i << ": ";
for (int j = 0; j < m; j++)
{
//
//HERE IS THE AREA OF ISSUE
//Neither of these work
// need[i][j] = max[i][j] - alloc[i][j];
// need.push_back(max[i][j] - alloc[i][j]);
}
cout << endl;
}
cout << endl;
return 0;
}