1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

execution time of a sudoku program

Discussion in 'C' started by jackspa, Feb 26, 2009.

  1. jackspa

    jackspa New Member

    Joined:
    Feb 20, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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();
    }
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    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.
     
  3. jackspa

    jackspa New Member

    Joined:
    Feb 20, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    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
     
  5. jackspa

    jackspa New Member

    Joined:
    Feb 20, 2009
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    The time() function is what you need.
     
  7. indiansword

    indiansword Security Expert

    Joined:
    Oct 19, 2008
    Messages:
    496
    Likes Received:
    36
    Trophy Points:
    0
    Occupation:
    Operation Planner for 3 Australia
    Home Page:
    yea i guess he is right. i have been learning programming from what u guyz post :D
     

Share This Page