Below is the code for resource request. My safety algorithm is running fine but when I ask for additional resources it is giving error situation(request>need). But when I practically do it than I am unable to find any error.
here is my code
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main() {
int i, j;
printf("********** Banker's Algo ************\n");
input();
show();
cal();
request();
getch();
return 0;
}
void input() {
int i, j;
printf("Enter the no of Processes\t");
scanf("%d", &n);
printf("Enter the no of resources instances\t");
scanf("%d", &r);
printf("Enter the Max Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}
void show() {
int i, j;
printf("Process\t Allocation\t Max\t Available\t");
for (i = 0; i < n; i++) {
printf("\nP%d\t ", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++)
printf("%d ", avail[j]);
}
}
}
void cal() {
int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;
int safe[100];
int i, j;
for (i = 0; i < n; i++) {
finish[i] = 0;
}
//find need matrix
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while (flag) {
flag = 0;
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
}
printf("P%d->", i);
if (finish[i] == 1) {
i = n;
}
}
}
}
}
}
for (i = 0; i < n; i++) {
if (finish[i] == 1) {
c1++;
} else {
printf("P%d->", i);
}
}
if (c1 == n) {
printf("\n The system is in safe state");
} else {
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
void request() {
int c, pid, request[100][100], B[100][100], i;
printf("\n Do you want make an additional request for any of the process ? (1=Yes|0=No)");
scanf("%d", &c);
if (c == 1) {
printf("\n Enter process number : ");
scanf("%d", &pid);
printf("\n Enter additional request : \n");
for (i = 0; i < r; i++) {
printf(" Request for resource %d : ", i + 1);
scanf("%d", &request[0][i]);
}
for (i = 0; i < r; i++) {
if (request[0][i] > need[pid][i]) {
printf("\n ******Error encountered******\n");
exit(0);
}
}
for (i = 0; i < r; i++) {
avail[i] -= request[0][i];
alloc[pid][i] += request[0][i];
need[pid][i] -= request[0][i];
}
cal();
getch();
} else {
exit(0);
}
}
OUTPUT for the above code:
Enter the no of Processes 5
Enter the no of resources instances 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the available Resources
3 3 2
Process Allocation Max Available
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3
P1->P3->P4->P2->P0->
The system is in safe state
Do you want make an additional request for any of the process ? (1=Yes|0=No)1
Enter process number : 2
Enter additional request :
Request for resource 1 : 1
Request for resource 2 : 2
Request for resource 3 : 1
******Error encountered******
OUTPUT that is needed:
*********DEADLOCK AVOIDANCE USINGBANKER'S ALGORITHM***********
Enter total number of processes : 5
Enter total number of resources : 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix
0 1 0
2 0 0
3 0
2
2 1 1
0 0 2
Available resources :
Resource 1 : 3
Resource 2 : 3
Resource 3 : 2
********Maximum Requirement Matrix*********
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
********Allocation Matrix**********
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A safety sequence has been detected.
P1 P3 P4 P0 P2
Do you want make an additionalrequest for any of the process ? (1=Yes|0=No)1
Enter process number : 2
Enter additional request :
Request for resource 1 : 1
Request for resource 2 : 2
Request for resource 3 : 1
A safety sequence has been detected.
P1 P3 P4 P0 P2
Where can I make changes to get the above output.
Having done some cleanup on your code it appears that your problem is that in if (request[0][i] > need[pid][i]) {, you are looking at the values,
request[0][0] = 1
request[0][1] = 2
request[0][2] = 1
However when you compare this against need you are comparing against need[2][i], the values of i at this time are,
need[2][0] = 6
need[2][1] = 0
need[2][2] = 0
As such when i=1 and when i=2 the values 1 and 2 are more than 0 and 0, as such the error occurs.
I've modified the output to show you the values in need,
********** Banker's Algo ************
Enter the no of Processes 5
Enter the no of resources instances 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the available Resources
3 3 2
P1->P3->P4->P2->P0->
The system is in safe state
Process Allocation Need Max Available
P1 0 1 0 7 5 3 7 4 3 8 8 7
P2 2 0 0 3 2 2 1 2 2
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 2 2 2 0 1 1
P5 0 0 2 4 3 3 4 3 1
Do you want make an additional request for any of the process ? (1=Yes|0=No)1
Enter process number : 2
Enter additional request :
Request for resource 1 : 1
Request for resource 2 : 2
Request for resource 3 : 1
******Error encountered******
Code,
#include<stdio.h>
#include<stdlib.h>
//include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
void request();
int main() {
int i, j;
printf("********** Banker's Algo ************\n");
input();
cal();
show();
request();
//getch();
return 0;
}
void input() {
int i, j;
printf("Enter the no of Processes\t");
scanf("%d", &n);
printf("Enter the no of resources instances\t");
scanf("%d", &r);
printf("Enter the Max Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}
void show() {
int i, j;
printf("Process\t Allocation\t Need\t Max\t Available");
for (i = 0; i < n; i++) {
printf("\nP%d\t ", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", need[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++)
printf("%d ", avail[j]);
}
}
}
void cal() {
int finish[100], flag = 1, k, c1 = 0;
int i, j;
for (i = 0; i < n; i++) {
finish[i] = 0;
}
//find need matrix
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while (flag) {
flag = 0;
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
}
printf("P%d->", i);
if (finish[i] == 1) {
i = n;
}
}
}
}
}
}
for (i = 0; i < n; i++) {
if (finish[i] == 1) {
c1++;
} else {
printf("P%d->", i);
}
}
if (c1 == n) {
printf("\n The system is in safe state\n");
} else {
printf("\n Process are in dead lock\n");
printf("\n System is in unsafe state\n");
}
}
void request() {
int c, pid, request[100][100], i;
printf("\n Do you want make an additional request for any of the process ? (1=Yes|0=No)");
scanf("%d", &c);
if (c == 1) {
printf("\n Enter process number : ");
scanf("%d", &pid);
printf("\n Enter additional request : \n");
for (i = 0; i < r; i++) {
printf(" Request for resource %d : ", i + 1);
scanf("%d", &request[0][i]);
}
for (i = 0; i < r; i++) {
if (request[0][i] > need[pid][i]) {
printf("\n ******Error encountered******\n");
exit(0);
}
}
for (i = 0; i < r; i++) {
avail[i] -= request[0][i];
alloc[pid][i] += request[0][i];
need[pid][i] -= request[0][i];
}
cal();
//getch();
} else {
exit(0);
}
}
Related
This below is my ds hw code.
#include <stdio.h>
typedef struct
{
int row;
int col;
int val;
}term;
void CreateTriplet(term t[],int a[][10],int m, int n)
{
int i, j, k = 0;
t[0].row = m;
t[0].col = n;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j ++)
{
if(a[i][j] == 0)
continue;
k++;
t[k].row = i;
t[k].col = j;
t[k].val = a[i][j];
}
}
t[0].val = k;
}
void printTriplet(term t[], int k)
{
int i;
for(i = 0 ; i < k ; i++)
{
printf("%d\t", t[i].row);
printf("%d\t", t[i].col);
printf("%d\n", t[i].val);
}
}
void findTranspose(term t1[], term t2[])
{
int i, j, k;
t2[0].row = t1[0].col;
t2[0].col = t1[0].row;
t2[0].val = t1[0].val;
k = 1;
for (i = 0; i < t1[0].col; i++)
{
for (j = 1; j <=t1[0].val; j++)
{
if (t1[j].col == i)
{
t2[k].row = t1[j].col;
t2[k].col = t1[j].row;
t2[k].val = t1[j].val;
k++;
}
}
}
}
int main()
{
int a[10][10], i, j, m, n, zero=0, nonzero;
term t1[101], t2[102];
scanf("%d %d", &m, &n);
for(i = 0; i < m ;i++)
{
for(j= 0; j < n ;j++)
{
scanf("%d]\n", &a[i][j]);
if(a[i][j]==0)
zero++;
}
}
if (i*j != i*i && j*j) /* I have no idea how to modify this section for the output*/
{
printf("Input matrix has wrong size. Please input again. \n");
}
else
{
nonzero = m * n - zero;
printf("Sparse matrix by triplet form:\n");
CreateTriplet(t1, a, m, n);
printTriplet(t1, nonzero+1);
findTranspose(t1, t2);
printf("Transpose of the sparse matrix:\n");
printTriplet(t2, nonzero+1);
}
return 0;
}
The main problem is in the section (if statement.)
When I was writing the if conditional, the input size was 6 * 6. Also, the elements in the matrix are not 6 * 6. My idea is to let the condition determine row * col != input elements. I don't know how to modify the if statement. Can someone help me?
**Input**
55
15 0 0 22 0
0 0 3 0 0
0 0 0 -6 0
0 0 0 0 7
91 0 0 0 0
**Output**
Sparse matrix by triplet form:
5 5 6
0 0 15
0 3 22
1 2 3
2 3 -6
3 4 7
4 0 91
Transpose of the sparse matrix:
5 5 6
0 0 15
0 4 91
2 1 3
3 0 22
3 2 -6
4 3 7
**Input**
66
15 0 0 22 0 -15 100
0 11 3 0 0 0
0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0
1 0 0 0 0 0
**Output**
Input matrix has wrong size. Please input again.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row;
int col;
int val;
} term;
void CreateTriplet (term t[],int a[][10],int m, int n) {
int i, j, k = 0;
t[0].row = m;
t[0].col = n;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j ++) {
if(a[i][j] == 0)
continue;
k++;
t[k].row = i;
t[k].col = j;
t[k].val = a[i][j];
}
}
t[0].val = k;
}
void printTriplet(term t[], int k) {
int i;
for(i = 0 ; i < k ; i++) {
printf("%d\t", t[i].row);
printf("%d\t", t[i].col);
printf("%d\n", t[i].val);
}
}
void findTranspose(term t1[], term t2[]) {
int i, j, k;
t2[0].row = t1[0].col;
t2[0].col = t1[0].row;
t2[0].val = t1[0].val;
k = 1;
for (i = 0; i < t1[0].col; i++) {
for (j = 1; j <=t1[0].val; j++) {
if (t1[j].col == i) {
t2[k].row = t1[j].col;
t2[k].col = t1[j].row;
t2[k].val = t1[j].val;
k++;
}
}
}
}
int main (int argc, char * *argv, char * *envp) {
int a[10][10], i, j, m, n, zero = 0, nonzero;
term t1[101], t2[102];
int ok_or_not = 1;
while (ok_or_not == 1) {
scanf ("%d %d", &m, &n);
if (m * n != m * m) {
printf ("Input matrix has wrong size. Please input again. \n");
}
else {
ok_or_not = 0;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d]\n", &a[i][j]);
if (a[i][j] == 0)
zero++;
}
}
if (i * j != i * i && j * j) { /* I have no idea how to modify this section for the output*/
} else {
nonzero = m * n - zero;
printf("Sparse matrix by triplet form:\n");
CreateTriplet(t1, a, m, n);
printTriplet(t1, nonzero + 1);
findTranspose(t1, t2);
printf("Transpose of the sparse matrix:\n");
printTriplet(t2, nonzero + 1);
}
return (EXIT_SUCCESS);
}
the program should this:
the matrix use 1,2,...,n on first line and 2,3,...n,n-1 on second etc, for instance :
input :
5
expected output:
1 2 3 4 5
2 3 4 5 4
3 4 5 4 3
4 5 4 3 2
5 4 3 2 1
i get try maybe anyone can help me to solve this program.
this my program before:
#include <stdio.h>
void makeSymmetricMatrix(int n) {
int i,j;
int matrix[n][n];
for( i = 0; i<n; i++){
int count = 1;
for( j = 0; j <n; j++){
if(i == j){
matrix[i][j] = 0;
}else{
matrix[i][j] = count++;
}
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int n = 5;
makeSymmetricMatrix(n);
}
I really need your correction about my program
Try this function.
void makeSymmetricMatrix(int n)
{
int i, j, count, decrease;
int matrix[n][n];
for(i = 0; i < n; i++)
{
decrease = 0;
count = i+1;
for(j = 0; j < n; j++)
{
matrix[i][j] = count;
if(count == n)
decrease = 1;
if(decrease == 1)
count--;
else
count++;
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d", matrix[i][j]);
}
printf("\n");
}
}
Basically it stores a variable decrease which gets triggered when count reaches the maximum value (n here). Once that happens, numbers are stored in a reverse order.
Result for n = 6:
123456
234565
345654
456543
565432
654321
can be done using :
void makeSymmetricMatrix(int n) {
int i,j;
int matrix[n][n];
for (i = 0; i<n; i++) {
int v = i+1, offset = 1;
for (j = 0; j <n; j++){
matrix[i][j] = v;
if (v == n)
offset = -1;
v += offset;
}
}
for( i = 0; i<n; i++){
for( j = 0; j <n; j++){
printf("%d ", matrix[i][j]);
}
putchar('\n');
}
}
in each line the first value equals the rank of the line plus 1 and by default the next value is the current plus 1
each time the value reaches n the rest of the values of the same line are the previous minus 1
After the change compilation and execution :
pi#raspberrypi:/tmp $ gcc -Wall m.c
pi#raspberrypi:/tmp $ ./a.out
1 2 3 4 5
2 3 4 5 4
3 4 5 4 3
4 5 4 3 2
5 4 3 2 1
pi#raspberrypi:/tmp $
In this code I took two 1D arrays and kept multiplicand and multipliers in those.
Then I kept the multiplication results in a 2d array to add them.
{
int n, m, i, j;
printf("Enter multiplicand(n) size: ");
scanf("%d", &n);
printf("Enter multiplier(m) size: ");
scanf("%d", &m);
int a[n], b[m];
printf("Enter multiplicands: ");
for(i = 0; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter multipliers: ");
for(i = 0; i<m; i++)
{
scanf("%d", &b[i]);
}
int c[m][n+m] , k = 0 , l = m+n-1 , p = 2;
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
c[i][j] = -1;
}
}
for(i = m-1; i>=0; i--)
{
for(j = n-1; j>=0; j--)
{
c[k][l] = a[j]*b[i];
l--;
}
l = m+n-p;
p++;
k++;
}
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
I was able to get -1's on the top left but I want 0's in the right bottom of the numbers
So, if I input a[5] = {1 , 1 , 1 , 1 , 1} , b[5] = {1 , 1 , 1 , 1 , 1}
I should get:
-1 -1 -1 -1 -1 1 1 1 1 1
-1 -1 -1 -1 1 1 1 1 1 0
-1 -1 -1 1 1 1 1 1 0 0
-1 -1 1 1 1 1 1 0 0 0
-1 1 1 1 1 1 0 0 0 0
Here is some cleaner code with print outs. I really don't understand what you are trying to do. The whole -1 loop is confusing and the output example is not clear.
I think you are trying to just add the multiplication result to a new array? But what does the -1 mean?
#include <stdio.h>
int main()
{
int n, m, i,l, j,k,p;
n =5;
m = 5;
int a[5] = {1}, b[5] = {1};
int c[m][n+m];
k = 0;
l = m+n-1;
p = 2;
for(i = 0; i<m; i++)
{
printf("\n");
for(j = 0; j<m+n; j++)
{
c[i][j] = -1;
printf("[%i][%i]:%i ",i,j,c[i][j]);
}
}
printf("\n");
for(i = m-1; i>=0; i--)
{
printf("\n");
for(j = n-1; j>=0; j--)
{
c[k][l] = a[j]*b[i];
printf("[%i][%i]: %i x %i = %i ",k,l,a[j],b[i],c[k][l]);
l--;
}
l = m+n-1;
k++;
}
printf("\n\n");
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
If you explain more about what you are trying to do then I can edit this to help.
For example, if your 'a' array is {2,4} and your 'b' array is {3,6}. What is the expected output of the 'c' array.
c = { ? }
The size of the square matrix is entered as standard input (the range of input values is 2 to 10). Create a program that prints the unit matrix of the input size.
The unit matrix is a matrix whose diagonal from the top left to the bottom right (main diagonal) is 1 and all others are zero.
ex) input: 5
result :
1 0 0 0 0\n
0 1 0 0 0\n
0 0 1 0 0\n
0 0 0 1 0\n
0 0 0 0 1\n
my code:
int main(){
int n;
scanf("%d", &n);
int **matrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++)
{
matrix[i] = malloc(sizeof(int) * n);
memset(matrix[i], 0, sizeof(int) * n);
}
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1;
printf("%d ", matrix[i][i]);
}
printf("\n");
for (int i = 0; i < n; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
.. what 's the error?
You need a double loop to print the output:
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1; // set identity
}
for (int j=0; j<n; j++)
{
for (int i = 0; i < n; i++)
{
printf("%d ", matrix[j][i]);
}
printf("\n");
}
One error is to think yo need a data strucure to print the output requested.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
if (scanf("%d", &n) != 1 || n < 2 || 10 < n) {
perror("Input error!\n\n");
return EXIT_FAILURE;
}
for (int i = 0; i < n; ++i) {
for (int k = 0; k < n; ++k)
printf(i == k ? "1 " : "0 ");
putchar('\n');
}
}
I need to input five numbers and then a positive or negative. The order will be changed like the number.
For example, if three (3) is the number then:
1 2 3 4 5 6 7 8 9 10
will become:
4 5 6 7 8 9 10 1 2 3
Please do not use pointers. I have made the following code and would like to be able to improve on it. Can I use a mathematical formula using the modulus operator % ? If so, how would I be able to do it.
Thanks, this is my code.
#include<stdio.h>
#define n 10
int main(void)
{
int num[n] = { 0 }, assist[n] = { 0 }, i = 0, j = 0, variable = 0, k = 0;
for (i = 0; i < n; i++)
{
printf("please enter index. %d\n", i );
scanf("%d", &num[i]);
}
printf("please enter the circle number\n");
scanf("%d", &variable);
printf("\n\n");
if (variable >= 0)
{
for (i = variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < variable, j < n; i++, j++)//to assist//
{
assist[j] = num[i];
}
}
if(variable < 0)
{
for (i = n + variable, j = 0; i < n; i++, j++)
{
assist[j] = num[i];
k++;
}
for (i = 0, j = k; i < n + variable, j < n; i++, j++)
{
assist[j] = num[i];
}
}
for (i = 0; i < n; i++)//output//
{
printf("%d\n", assist[i]);
}
return 0;
}
Something like this. I did not compile; any small mistakes are for you.
#include<stdio.h>
#define N 10
int main(void)
{
int numbers[N];
int offset;
for (int i = 0; i < N; i++)
{
printf("please enter number %d: ", i);
scanf("%d", &num[i]);
}
printf("\nplease enter the circle number: ");
scanf("%d", &offset);
printf("\n\n");
for (int i = 0; i < N; i++)
{
printf("%d ", num[(i + offset) % N]);
}
printf("\n");
}