Advertisement
Adnan_Sarker

Replacement Algo OS - 02 05 2017

May 2nd, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int a[5][5], b[50];
  5. void sett()
  6. {
  7.     for(int i = 1;i<6;i++)
  8.     {
  9.         b[i-1] = i;
  10.     }
  11. }
  12.  
  13. bool cq(int n)
  14. {
  15.     for(int i = 0;i<50;i++)
  16.     {
  17.         if(b[i]==n)
  18.             return true;
  19.     }
  20.     return false;
  21.  
  22. }
  23. int Shift(int row, int value)
  24. {
  25.     int temp;
  26.     for(int i = row;i<row+1;i++)
  27.     {
  28.         for(int j = 0;j<5;j++)
  29.         {
  30.             temp = a[i][j];
  31.             a[i][j] = value;
  32.             value = temp;
  33.         }
  34.     }
  35. }
  36.  
  37. int Min_Cq()
  38. {
  39.     int Min = 9999999, ans = 0, R;
  40.     for(int i = 0;i<5;i++)
  41.     {
  42.         ans = 0;
  43.         for(int j = 0;j<5;j++)
  44.         {
  45.             int num = a[i][j];
  46.             if(num==1)
  47.             {
  48.                 if(j==0){ ans = ans + 16;}
  49.                 else if(j==1) { ans = ans + 8;}
  50.                 else if(j==2) { ans = ans + 4;}
  51.                 else if(j==3) { ans = ans + 2;}
  52.                 else if(j==4) { ans = ans + 1;}
  53.             }
  54.         }
  55.         if(Min>ans)
  56.         {
  57.             Min = ans;
  58.             R = i;
  59.         }
  60.     }
  61.     return R;
  62. }
  63.  
  64. int main()
  65. {
  66.     sett();
  67.     for(int i = 0;i<5;i++)
  68.     {
  69.         for(int j = 0;j<5;j++)
  70.         {
  71.             a[i][j] = 0;
  72.         }
  73.     }
  74.  
  75.     cout<<"What Operation u want to prefer ? for read bit type 1, for inserting enter 2 : "<<endl;
  76.     int N;
  77.  
  78.     while(cin>>N)
  79.     {
  80.         cout<<endl;
  81.         if(N==1)
  82.         {
  83.             for(int i = 0; i<5; i++)
  84.             {
  85.                 cin>>N;
  86.                 Shift(i,N);
  87.             }
  88.             cout<<endl;
  89.             for(int i = 0; i<5; i++)
  90.             {
  91.                 for(int j = 0; j<5; j++)
  92.                 {
  93.                     cout<<a[i][j]<<" ";
  94.                 }
  95.                 cout<<endl;
  96.             }
  97.         }
  98.  
  99.         else if(N==2)
  100.         {
  101.  
  102.             int res = Min_Cq();
  103.             cout<<"Enter shifting value :"<<endl<<endl;
  104.             cin>>N;
  105.             int T = 1;
  106.             for(int i =0;i<50;i++)
  107.             {
  108.                 if(b[i]==N)
  109.                 {
  110.                     T = 0;
  111.                 }
  112.             }
  113.  
  114.             if(T){
  115.             for(int i = 0;i<5;i++)
  116.             {
  117.                 if(i==0){ a[res][i] = 1;}
  118.                 else{
  119.                     a[res][i] = 0;
  120.                 }
  121.             }
  122.             }
  123.             else{
  124.                 Shift(N,1);
  125.             }
  126.  
  127.             for(int i = 0; i<5; i++)
  128.             {
  129.                 for(int j = 0; j<5; j++)
  130.                 {
  131.                     cout<<a[i][j]<<" ";
  132.                 }
  133.                 cout<<endl;
  134.             }
  135.  
  136.         }
  137.         cout<<"What Operation u want to prefer ? for read bit type 1, for inserting enter 2 : "<<endl;
  138.     }
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement