0

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

  • 1
    If you need matrices, make matrices, not vectors of vectors. Unfortunately there is no standard matrix class, so create your own or use one of the many available on the internet. Also a "2D vector" is usually a vector in 2D space, not a C++ vector of vectors. – Nelfeal May 03 '20 at 18:00
  • @Nelfeal thank you for your feedback, but use of vectors is required and I thought a vector of vectors would be easiest to get this all laid out, but apparently I was wrong. – Taylor Converse May 03 '20 at 18:04
  • Why are you not preferring 2d array over vector>? – am10 May 03 '20 at 18:04
  • @AyushMishra I would love to use a 2d array but I was told, and I quote: "Yes, being extensible, vectors are utilized to solve the problem. So it would be appropriate to use vectors instead of arrays." sucks, but it is what's been asked of me so I have to try to get it written properly in this manner. – Taylor Converse May 03 '20 at 18:11

2 Answers2

0

By the way for your solution: you need to replace this:

need.push_back(max[i][j] - alloc[i][j]);

with

need[i].push_back(max[i][j] - alloc[i][j]);

As you can think need[i] is itself an vector and push_back is a vector method.

am10
  • 449
  • 1
  • 6
  • 17
0
need[i][j] = max[i][j] - alloc[i][j];

This doesn't work because you haven't allocated any memory for need.

need.push_back(max[i][j] - alloc[i][j]);

This doesn't work either because need is a vector of vector, so push_back expects a vector, and you are giving it a single value.

I'm guessing what you want is something like that:

need.resize(n);
for (int i = 0; i < n; i++)
{
    cout << i << ": ";
    for (int j = 0; j < m; j++)
    {
        need[i].push_back(max[i][j] - alloc[i][j]);    
    }
    cout << endl;
}
cout << endl;

However, you would be better off not using vectors of vectors, and instead using vectors of n*m values. That way, you could do this:

for (int i = 0; i < n; i++)
{
    cout << i << ": ";
    for (int j = 0; j < m; j++)
    {
        need.push_back(max[i*m+j] - alloc[i*m+j]);    
    }
    cout << endl;
}
cout << endl;

Or this:

need.resize(n*m);
for (int i = 0; i < n; i++)
{
    cout << i << ": ";
    for (int j = 0; j < m; j++)
    {
        need[i*m+j] = max[i*m+j] - alloc[i*m+j];    
    }
    cout << endl;
}
cout << endl;
Nelfeal
  • 12,593
  • 1
  • 20
  • 39
  • [Here is a simple class that wraps the one `vector` solution](https://stackoverflow.com/a/2076668/4581301), hiding the indexing math from sight. – user4581301 May 03 '20 at 18:14
  • That makes a lot of sense, thanks! I appreciate the help, and will likely utilize the n*m approach. – Taylor Converse May 03 '20 at 18:22