problems in loop

I couldn't run this program as i had not expected.this is basically an unfinished sudoku generating program.the codes showed below is for generating the #s but i hadn't taken 3X3 squares into consideration.Can someone help me ,thanks!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<time.h>
using namespace std;

int main()
{
	srand(time(0));
	int a[9][9];
	for (int i=1;i<=9;i++)
		for (int j=1;j<=9;j++)
			a[i][j]=0;
	int flagx,flagy,store=0;                //flagx :to check if the number in a row has existed before 
	//flagy :numbers in column
	for (int i=1;i<=9;i++){                 //i:row
		for (int j=1;j<=9;j++)                  //j:column
		{
			do
			{
				flagx=0,flagy=0;
				store=rand()%9+1;
				for (int x=1;x<j;x++)                //this loop is used to check if the number is valid in a row
				{
					if (a[i][x]!=store) 
						flagx++;
				}
				for (int y=1;y<i;y++)             //this is for column (the loops for row and column both work out seperately but i couldn't combine them )
				{
					if (a[y][j]!=store)
						flagy++;
				}                                      //i need another loop for 3X3 square,could use modulous to do that,but i need to work both row and column out first

			}
			while(flagx!=j-1||flagy!=i-1);
			a[i][j]=store;

		}
		cout<<"outside"<<i<<endl;
	}

	for (int i=1;i<=9;i++)
	{
		cout<<"sdkjkl";                    //just to see if this loop work.and it turns out the program was stuck in the while loop()
		for (int j=1;j<=9;j++)
			cout<<a[i][j]<<" ";
		cout<<endl;
	}

	system("pause");
	return 0;
}
Last edited on
That's not a complete program or function.
It's unrealistic to expect anyone to compile and test it.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.

sorry,but how to do that,i couldn't make it as what everybody else did.
Topic archived. No new replies allowed.