• 0

Problem with code Can anyone tell me the error


Question

#include<stdio.h>

#include<conio.h>

int main()

{

int a[10][10],b[10][10],n[10][10],i,j,x,y,succ,done;

int z[10],flag[10]={0},r;

printf("enter the number of resources you want ti have ");

scanf("%d",&r);

printf("\n enter the reqest matrix \n ");

for(x=1;x<=r;x++)

{

for(y=1;y<=r;y++)

{

scanf("%d",&b[x][y]);

}}

printf("\n enter the allocation matrix");

for(x=1;x<=r;x++)

{

for(y=1;y<=r;y++)

{

scanf("%d",&a[x][y]);

}}

for(x=1;x<=r;x++)

{

for(y=1;y<=r;y++)

{

n[x][y]=b[x][y]-a[x][y];

printf(" %d",n[x][y]);

}

printf("\n");

}

printf("\n Enter the resource vector\n");

for(x=1;x<=r;x++)

{

scanf("%d",z[x]);

}

done=r;

for(x=1;x<=r && done!=0;x++)

{

for(y=1;y<=r;y++)

{

if(z[y]>=n[x][y])

{

flag[x]=1;

if(y==r)

{

for(i=1;i<=r;i++)

{

z=z+a[x];

}

flag[x]=1;

done--;

x=1;

}

}

else

{

flag[x]=0;

if(y==r)

{

flag[x]=0;

done--;

break;

}

else

break;

}

}}

for(x=1;x<=r;x++)

{

if(flag[x]==1)

{

printf("\nprocess [%d] in safe state",x);

}

else if(flag[x]==0)

{

printf("\nprocess [%d] unsafe",x);

}

}

getch();

}

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Thats all a bit garbled, can you format it better, people will be more inclined to help you if you format you code so its easily readable. What error is it giving you?

Link to comment
Share on other sites

  • 0

Reposted for readability :) :

#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;

int main()
{
	int a[10][10],b[10][10],n[10][10],i,j,x,y,succ,done;
	int z[10],flag[10]={0},r;
	printf("enter the number of resources you want ti have ");
	scanf("%d",&amp;r);
	printf("\n enter the reqest matrix \n ");

	for(x=1;x&lt;=r;x++)
	{
		for(y=1;y&lt;=r;y++)
		{
			scanf("%d",&amp;b[x][y]);
		}
	}

	printf("\n enter the allocation matrix");

	for(x=1;x&lt;=r;x++)
	{
		for(y=1;y&lt;=r;y++)
		{
			scanf("%d",&amp;a[x][y]);
		}
	}

	for(x=1;x&lt;=r;x++)
	{
		for(y=1;y&lt;=r;y++)
		{
			n[x][y]=b[x][y]-a[x][y];
			printf(" %d",n[x][y]);
		}
		printf("\n");
	}

	printf("\n Enter the resource vector\n");

	for(x=1;x&lt;=r;x++)
	{
		scanf("%d",z[x]);
	}
	done=r;

	for(x=1;x&lt;=r &amp;&amp; done!=0;x++)
	{
		for(y=1;y&lt;=r;y++)
		{
			if(z[y]&gt;=n[x][y])
			{
				flag[x]=1;
				if(y==r)
				{
					for(i=1;i&lt;=r;i++)
					{
						z[i]=z[i]+a[x][i];
					}
					flag[x]=1;
					done--;

					x=1;
				}
			}
			else
			{
				flag[x]=0;
				if(y==r)
				{
					flag[x]=0;
					done--;
					break;
				}
				else
				break;
			}
		}
	}

	for(x=1;x&lt;=r;x++)
	{
		if(flag[x]==1)
		{
			printf("\nprocess [%d] in safe state",x);
		}
		else if(flag[x]==0)
		{
			printf("\nprocess [%d] unsafe",x);
		}
	}
	getch();
}

Link to comment
Share on other sites

  • 0

Quick run through of the code with the -Wall compiler flag gives these warnings:

[Warning] format argument is not a pointer (arg 2) (44)

[Warning] unused variable `j' (6)

[Warning] unused variable `succ' (6)

[Warning] control reaches end of non-void function (94)

While the unused variables effectively do nothing I'm betting that line 44 causes a crash.

You're missing an & on your variable. The correct line should be:

scanf("%d", &amp;z[x]);

Try to compile your code with the -Wall compiler flag in future. This will warn on all common errors and I will pick the above mistakes up.

Link to comment
Share on other sites

  • 0

Did you know you can give meaningful names to variables ? This isn't mathematics where every constant and variable is a single letter, in programming you want your code to be somewhat readable. Variable names such as x, r, n, tell me absolutely nothing about what they represent. "succ" and "done" are already much better. For instance, "a" should be "allocationMatrix".

ViZioN picked up the most probable cause of crash. There's at least another one, on that same line. When you do scanf("%d", &z[x]), you are storing a number at the address of z[x]. x is defined in the enclosing for loop, as ranging from 1 to r. r is something the user inputs at the beginning and can be any number. z, however, can store a maximum of 10 numbers, as defined in the first few lines. So if the user enters r greater than 10, funny things will happen.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.