execution time of a sudoku program

Go4Expert Member
26Feb2009,21:15   #1
jackspa's Avatar
Hi,I am attaching a file containing a sudoku solving program in c.How can I get the execution time for this one?Please help.
Code:
#include<stdio.h>
#include<conio.h>
int a[9][9],b[9][9],rstr,rend,cstr,cend;
void rowcol(int,int);
void display();
int valid(int,int,int);
void solve();
void getdata();
void getdata()
{
	int i,j;
	for(i=0;i<9;i++)
		for(j=0;j<9;j++)
		{
			printf("\n Element[%d][%d](0 if blank)= ",i+1,j+1);
			scanf("%d",&a[i][j]);
		}
}
void rowcol(int i,int j)
{
	if(i>=0&&i<=2)
	{
		if(j>=0&&j<=2){rstr=0;rend=2;cstr=0;cend=2;}
		if(j>=3&&j<=5){rstr=0;rend=2;cstr=3;cend=5;}
		if(j>=6&&j<=8){rstr=0;rend=2;cstr=6;cend=8;}
	}
	if(i>=3&&i<=5)
	{
		if(j>=0&&j<=2){rstr=3;rend=5;cstr=0;cend=2;}
		if(j>=3&&j<=5){rstr=3;rend=5;cstr=3;cend=5;}
		if(j>=6&&j<=8){rstr=3;rend=5;cstr=6;cend=8;}
	}
	if(i>=6&&i<=8)
	{
		if(j>=0&&j<=2){rstr=6;rend=8;cstr=0;cend=2;}
		if(j>=3&&j<=5){rstr=6;rend=8;cstr=3;cend=5;}
		if(j>=6&&j<=8){rstr=6;rend=8;cstr=6;cend=8;}
	}
}
int valid(int k,int row,int col)
{
	int i,j,c=0;
	for(i=0;i<9;i++)
		if(b[row][i]==k)
			c++;
	for(i=0;i<9;i++)
		if(b[i][col]==k)
			c++;
	rowcol(row,col);
	for(i=rstr;i<=rend;i++)
		for(j=cstr;j<=cend;j++)
			if(b[i][j]==k)
				c++;
	return(c);
}
void display()
{
	int i,j;
	clrscr();
	for(i=0;i<9;i++)
	{
		printf("\n\n\n");
		for(j=0;j<9;j++)
			printf("%d   ",b[i][j]);
	}
}
void solve()
{
	int i=0,j=0,k=0,l,istr,jstr,iend,jend,e[9]={1,2,3,4,5,6,7,8,9};
	if(b[i][j]!=0&&b[i][j]==a[i][j])
		while(1)
		{
			j++;
			if(j==9){i++;j=0;}
			if(b[i][j]==0)
				break;
		}
	istr=i;jstr=j;
	iend=jend=8;
	while(1)
	{
		if(b[iend][jend]==0)
			break;
		jend--;
		if(jend<0){iend--;jend=8;}
	}
	while(1)
	{
		if(b[i][j]!=0&&b[i][j]==a[i][j])
			while(1)
			{
				j++;
				if(j==9){i++;j=0;}
				if(b[i][j]==0)
					break;
			}
		l=valid(e[k],i,j);
		if(!l)
		{
			b[i][j]=e[k];
			if(i==iend&&j==jend)
				break;
			k=0;j++;
			if(j==9){i++;j=0;}
			continue;
		}
		k++;
	    xyz:if(k==9)
		{
			while(1)
			{
				j--;
				if(j<0){i--;j=8;}
				if(i<=istr&&j<jstr)
				{
					i=istr;j=jstr;break;
				}
				if(b[i][j]!=a[i][j])
					break;
			}
			k=b[i][j];
			while(1)
			{
				if(k==9)
				{
					b[i][j]=0;
					goto xyz;
				}
				l=valid(e[k],i,j);
				if(!l)
				{
					b[i][j]=e[k];
					k=0;j++;
					if(j==9){i++;j=0;}
					break;
				}
				k++;
			}
		}
	}
}
void main()
{
	int i,j,k;
	clrscr();
      	getdata();
	for(i=0;i<9;i++)
		for(j=0;j<9;j++)
			b[i][j]=a[i][j];
	for(i=0;i<9;i++)
		for(j=0;j<9;j++)
			if(b[i][j]!=0)
			{
				k=valid(b[i][j],i,j);
				if(k>3)
				{
					printf("\n\nERROR:WRONG ENTRY!!!");
					getch();
					exit();
				}
			}
	solve();
	display();
	getch();
}
Go4Expert Founder
26Feb2009,21:45   #2
shabbir's Avatar
I have removed the attachment and moved the content of it into the post.

The execution time can be found but using the time difference at the start of the execution and when you are done with the calculation.
Go4Expert Member
26Feb2009,21:53   #3
jackspa's Avatar
how to get the starting time and end time?Can we get that in milliseconds?
The time calculated by this way will include other execution times too,if the system is multitasking.
Go4Expert Founder
26Feb2009,21:58   #4
shabbir's Avatar
You want to calculate the execution time of your code only then it would calculate the execution time of your code but the multitasking glitch is something you have to deal with but I am sure the effect on your personal computer would not be very high.

time_t can help you get the time
Go4Expert Member
26Feb2009,22:04   #5
jackspa's Avatar
i have tried time.h header file and clock( ) function.But i don't the unit of the output.it always comes 1 or 0.Can we get that in milliseconds?a syntax would help better.
Mentor
27Feb2009,18:01   #6
xpi0t0s's Avatar
The time() function is what you need.
Security Expert
27Feb2009,18:43   #7
indiansword's Avatar
Quote:
Originally Posted by xpi0t0s View Post
The time() function is what you need.
yea i guess he is right. i have been learning programming from what u guyz post