-7

I have project in banker algorithm implement in c++

But I have small mistake :') in resource request

I didn't know what the mistake. The first if statement doesn't work :(

#include<iostream>
#include<vector>

using namespace std;

int main() {
    int work[4];
    int allocation[5][4];
    int max[5][4];
    int need[5][4];
    int p, pr, r, a, aval[4], req[4];
    bool state[5], test;
    vector < int > avl;
    //----------------------------------------
    test = true;
    for (int i = 0; i < 4; i++)
        work[i] = aval[i];
    for (int i = 0; i < 5; i++)
        state[i] = false;
    //----------------------------enter p r---------------------------------
    cout << "Enter the number of processes in the system :";
    cin >> p;
    cout << "\nEnter the number of recourses :";
    cin >> r;
    //---------------------enter alloc---
    cout << "\nEnter the allocation " << endl;
    if (r = 1)
    {
        cout << "\t A \n \t ";
    }
    else if (r = 2)
    {
        cout << "\t A B \n \t ";
    }
    else if (r = 3)
    {
        cout << " A B C\n \t ";
    }
    else if (r = 4)
    {
        cout << " A B C D\n \t ";
    }
    for (int i = 0; i < p; i++)
    {
        cout << endl << "\t" << "P" << i << ":";
        for (int j = 0; j < 4; j++)
        {
            cin >> allocation[i][j];
            cout << " ";
        }
    }
    //-----------------------------entet max----------------
    cout << "\nEnter the MAX" << endl;
    if (r = 1)
        cout << " A \n \t ";
    else if (r = 2)
        cout << " A B \n \t ";
    else if (r = 3)
        cout << " A B C\n \t ";
    else if (r = 4)
        cout << " A B C D\n \t ";
    for (int i = 0; i < p; i++)
    {
        cout << endl << "P" << i << ":";
        for (int j = 0; j < 4; j++)
        {
            cin >> max[i][j];
            need[i][j] = max[i][j] - allocation[i][j];
        }
    }
    //-----------------enter ava--------------
    cout << "\nEnter the avaliable number : " << endl;
    for (int i = 0; i < 4; i++)
    {
        cin >> aval[i];
        cout << " ";
    }
    //-----------------enter request--------------
    cout << "\nEnter the number of process want be request  : ";
    cin >> pr;
    cout << "\nEnter the request number : " << endl;
    for (int i = 0; i < 4; i++)
    {
        cin >> req[i];
        cout << " ";
    }
    //-----------------------------------COUT---------------------
    cout << endl << "There are " << p << " processes in the system." << endl << endl;
    cout << "There are " << r << " resource types." << endl << endl;
    //----------------------------------cout allocation---------------  
    cout << " The allocation Matrix : " << endl << endl;
    cout << "\t  A B C D";
    for (int i = 0; i < p; i++)
    {
        cout << endl << "\tP" << i << ":";
        for (int j = 0; j < 4; j++)
        {
            cout << allocation[i][j] << " ";
        }
        cout << endl;
    }
    //----------------------------------cout max---------------  
    cout << endl << " The Max Matrix is...  " << endl << endl;
    cout << "\t  A B C D";
    for (int i = 0; i < p; i++)
    {
        cout << endl << "\tP" << i << ":";
        for (int j = 0; j < 4; j++)
        {
            cout << max[i][j] << " ";
        }
        cout << endl;
    }
    //-------------------------cout need-------------------------------------------
    cout << endl << " The Need Matrix is...  " << endl << endl;
    cout << "\t  A B C D";
    for (int i = 0; i < p; i++)
    {
        cout << endl << "\tP" << i << ":";
        for (int j = 0; j < 4; j++)
        {
            cout << need[i][j] << " ";
        }
        cout << endl;
    }
    //----------------------------- cout aval ---------------------
    cout << endl << "The Available Vector is..." << endl << endl;
    cout << "A B C D" << endl;
    for (int i = 0; i < 4; i++)
    {
        cout << aval[i] << " ";
    }
    //-----------------------------------SAFE STATE-----------------------
    int k = 0;
    for (k = 0; k < p; k++) {
        if (state[k] == false) {
            test = false;
            for (int j = 0; j<r; j++) {
                if (need[k][j] > work[j])
                    break;
                if (need[k][j] <= aval[j])
                    test = true;
            }
        }
    }
    if (test == true) {
        for (int j = 0; j < r; j++)
        {
            work[j] = work[j] + allocation[k][j];
        }
        state[k] = true;
        cout << endl << endl << "THE SYSTEM IS IN A SAFESTATE!" << endl;
    }
    if (test == false) {
        state[k] = false;
        cout << endl << endl << "THE SYSTEM IS NOT IN A SAFE STATE!";
    }
    //-----------------------------------request------------------------
    cout << "\nThe Request Vector is..." << endl;
    cout << "  A B C D" << endl;
    cout << pr << ":";
    for (int i = 0; i < 4; i++)
    {
        cout << req[i] << " ";
    }
    bool test2 = false;
    for (int i = 0; i < p; i++) {
        if (pr == p) {
            for (int j = 0; j < 4; j++)
            {
                if (req[j] <= avl[j] && req[j] <= need[i][j])
                {
                    test2 = true;
                }
                else
                {
                    break;
                }
            }
            if (test2 = true)
            {
                for (int n = 0; n < r; n++)
                {
                    aval[n] = aval[n] - req[n];
                    allocation[i][n] = allocation[i][n] + req[n];
                    need[i][n] = need[i][n] - req[n];
                }
                cout << "THE REQUEST CAN BE GRANTED!" << endl << endl;
                cout << "The Available Vector is...";
                cout << "A B C D" << endl;
                for (int x = 0; x < r; x++)
                {
                    cout << aval[x] << " ";
                }
            }
            else
            {
                cout << "THE REQUEST CANNOT BE GRANTED!" << endl << endl;
            }
        }
    }

    //------------------------------------------------------------------------------

    system("pause");
    return 0;
}
4
  • 1
    Possible can you format that code so that it is readable. Also seems a lot of it!
    – Ed Heal
    Dec 11, 2015 at 23:43
  • 2
    Indent the code correctly otherwise it is hard to understand Dec 11, 2015 at 23:43
  • 3
    plzzzzzz help me :( - Help yourself by writing a good question. Dec 11, 2015 at 23:45
  • Always include the error message you get when asking for help. "doesn't work" is not specific enough.
    – David
    Dec 12, 2015 at 0:15

3 Answers 3

5

When checking if two primitive types are equal, you need to use "==" instead of "=" e.g, change your if statements from

if ( r = 1 )

to

if (r == 1)
1
  1. Apart from above

    work[i] = aval[i]; - aval[i] - has not been initialised

  2. Read up on switch instead of r == 1 do this etc. ( I took into account the above statement and the chained if statements

  3. Ditto with = true. Learn the difference between comparison and assignment

  4. Perhaps learn how to use the debugger

  5. You need to use cout << flush so that the output is sent.

.... I could add others - this is enough to get on with

0

Here is the whole working program. I made some changes in it and made the code more easy to understand.

int p, r;
bool test;
vector < int > avl;

//----------------------------enter p r---------------------------------
system("clear");

cout << "Enter the number of processes in the system: ";
cin >> p;
cout << "Enter the number of recourses: ";
cin >> r;


//test
int allocation[p][r];
int max[p][r];
int need[p][r];
int aval[r];
int state[p];
test = true;
//test
//---------------------enter alloc---
system("clear");

cout << "\nEnter the allocation " << endl;
if (r == 1)
{
  cout << "\t    A \n \t ";
}
else if (r == 2)
{
    cout << "\t    A B \n \t ";
}
else if (r == 3)
{
    cout << "\t    A B C \n \t ";
}
else if (r == 4)
{
    cout << "\t    A B C D \n \t ";
}
for (int i = 0; i < p; i++)
{
    cout << endl << "\t" << "P" << i << ": ";
    for (int j = 0; j < r; j++)
    {
        cin >> allocation[i][j];
        cout << " ";
    }
}
system("clear");
//-----------------------------entet max----------------
cout << "\nEnter the MAX" << endl;
if (r == 1)
    cout << "\t    A \n \t ";
else if (r == 2)
    cout << "\t    A B \n \t ";
else if (r == 3)
    cout << "\t    A B C\n \t ";
else if (r == 4)
    cout << "\t    A B C D\n \t ";
for (int i = 0; i < p; i++)
{
    cout << endl << "\t" << "P" << i << ": ";
    for (int j = 0; j < r; j++)
    {
        cin >> max[i][j];
        need[i][j] = max[i][j] - allocation[i][j];
    }
}
system("clear");
//-----------------enter ava--------------
cout << "\nEnter the avaliable number : " << endl;
cout<<"\tAvail: ";
for (int i = 0; i < r; i++)
{
    cin >> aval[i];
    cout << " ";
}
//-----------------------------------COUT---------------------
system("clear");
system("clear");
cout << "There are " << p << " processes in the system." << endl;
cout << "There are " << r << " resource types." << endl << endl;
//----------------------------------cout allocation---------------
cout << " The allocation Matrix : " << endl << endl;
if (r == 1)
    cout << "\t    A \n \t ";
else if (r == 2)
    cout << "\t    A B \n \t ";
else if (r == 3)
    cout << "\t    A B C\n \t ";
else if (r == 4)
    cout << "\t    A B C D\n \t ";
for (int i = 0; i < p; i++)
{
    cout << endl << "\tP" << i << ": ";
    for (int j = 0; j < r; j++)
    {
        cout << allocation[i][j] << " ";
    }
    cout << endl;
}
//----------------------------------cout max---------------
cout << endl << " The Max Matrix is...  " << endl << endl;
if (r == 1)
    cout << "\t    A \n \t ";
else if (r == 2)
    cout << "\t    A B \n \t ";
else if (r == 3)
    cout << "\t    A B C\n \t ";
else if (r == 4)
    cout << "\t    A B C D\n \t ";
for (int i = 0; i < p; i++)
{
    cout << endl << "\tP" << i << ": ";
    for (int j = 0; j < r; j++)
    {
        cout << max[i][j] << " ";
    }
    cout << endl;
}
//-------------------------cout need-------------------------------------------
cout << endl << " The Need Matrix is...  " << endl << endl;
for(int i = 0; i < p; i++)
{
  for(int j = 0; j < r; j++)
  {
    need[i][j] = max[i][j] - allocation[i][j];
  }
}
if (r == 1)
    cout << "\t    A \n \t ";
else if (r == 2)
    cout << "\t    A B \n \t ";
else if (r == 3)
    cout << "\t    A B C\n \t ";
else if (r == 4)
    cout << "\t    A B C D\n \t ";
for (int i = 0; i < p; i++)
{
    cout << endl << "\tP" << i << ": ";
    for (int j = 0; j < r; j++)
    {
        cout << need[i][j] << " ";
    }
    cout << endl;
}
//----------------------------- cout aval ---------------------
cout << endl << "The Available Vector is..." << endl << endl;
cout<<"\tAvail: ";

for (int i = 0; i < r; i++)
{
    cout << aval[i] << " ";
}
cout<<endl;
//--------SAFESTATE----------SAFESTATE----------SAFESTATE-------------------
  int count = p;
  cout<<endl;
  cout<<"Safe sequence: ";
  do{
    for(int loop_var = 0; loop_var < p; loop_var++)
    {
      test = false;
        for (int j = 0; j < r; j++) {
          if(state[loop_var] == true)
          {
            break;
          }
          if(need[loop_var][j] > aval[j])
          {
            test = false;
            state[loop_var] = false;
            break;
          }
          else
          {
            test = true;
          }
      }

      if((test))
      {
        count--;
        state[loop_var] = true;
        for(int sb = 0; sb < r; sb++)
        {
          aval[sb] = aval[sb] + allocation[loop_var][sb];
        }
        if(count == 0)
        {
          cout<<"P"<<loop_var<<" ";
        }
        else if(count > 0)
        {
          cout<<"P"<<loop_var<<"-->";
        }
      }
    }
  }while(count != 0);
  cout<<endl;

//--------SAFESTATE----------SAFESTATE----------SAFESTATE-------------------
cout<<endl;
cout<<"The new available vector is: ";
for(int i = 0; i < r; i++)
{
  cout<<aval[i]<<" ";
}
cout << endl << endl;
return 0;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.