Hi,
I'm working in a 4x4 sudoku solver and i'm a beginner in c++.
I use Microsoft Visual Studio 6.0 and windows xp professional
Variables are:
a1 b1 c1 d1
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4
First i wrote that program

``````//sudoku.cpp:

#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>

void main(){

srand(time(0));

int a1;
int b1;
int c1;
int d1;
int a2;
int b2;
int c2;
int d2;
int a3;
int b3;
int c3;
int d3;
int a4;
int b4;
int c4;
int d4;

srand(time(0));

// 1st row

a1=rand()*5/32768;
printf("a1 = %d\n",a1);

while((b1!=0)||(b1=a1)){
b1=rand()*5/32768;
if ((b1!=0)&&(b1!=a1)){
break;
printf("b1 %d\n",b1);
}}

printf("b1 = %d\n",b1);

while((c1!=0)||(c1=a1)||(c1=b1)){
c1=rand()*5/32768;
if ((c1!=0)&&(c1!=a1)&&(c1!=b1)){
break;
}}
printf("c1 = %d\n",c1);

while((d1!=0)||(d1=a1)||(d1=b1)||(d1=c1)){
d1=rand()*5/32768;
if ((d1!=0)&&(d1!=a1)&&(d1!=b1)&&(d1!=c1)){
break;
}}
printf("d1 = %d\n",d1);

// Column A

while((a2!=0)||(a2=a1)||(a2=b1)){
a2=rand()*5/32768;
if ((a2!=0)&&(a2!=a1)&&(a2!=b1)){
break;
}}

printf("a2 = %d\n",a2);

while((a3!=0)||(a3=a1)||(a3=a2)){
a3=rand()*5/32768;
if ((a3!=0)&&(a3!=a1)&&(a3!=a2)){
break;
}}

printf("a3 = %d\n",a3);

while((a4!=0)||(a4=a1)||(a4=a2)||(a4=a3)){
a4=rand()*5/32768;
if ((a4!=0)&&(a4!=a1)&&(a4!=a2)&&(a4!=a3)){
break;
}}

printf("a4 = %d\n",a4);

// Column D

while((d2!=0)||(d2=c1)||(d2=d1)||(d2=a2)){
d2=rand()*5/32768;
if ((d2!=0)&&(d2!=c1)&&(d2!=d1)&&(d2!=a2)){
break;
}}

printf("d2 = %d\n",d2);

while((d3!=0)||(d3=d1)||(d3=d2)||(d3=a3)){
d3=rand()*5/32768;
if ((d3!=0)&&(d3!=d1)&&(d3!=d2)&&(d3!=a3)){
break;
}}

printf("d3 = %d\n",d3);

while((d4!=0)||(d4=d1)||(d4=d2)||(d4=d3)||(d4=a4)){
d4=rand()*5/32768;
if ((d4!=0)&&(d4!=d1)&&(d4!=d2)&&(d4!=d3)&&(d4!=a4)){
break;
}}

printf("d4 = %d\n",d4);

// 4th row

while((b4!=0)||(b4=a3)||(b4=a4)||(b4=b1)||(b4=d4)){
b4=rand()*5/32768;
if ((b4!=0)&&(b4!=a3)&&(b4!=a4)&&(b4!=b1)&&(b4!=d4)){
break;
}}

printf("b4 = %d\n",b4);

while((c4!=0)||(c4=d3)||(c4=d4)||(c4=c1)||(c4=a4)||(c4=b4)){
c4=rand()*5/32768;
if ((c4!=0)&&(c4!=d3)&&(c4!=d4)&&(c4!=c1)&&(c4!=a4)){
break;
}}

printf("c4 = %d\n",c4);

// Central Square

while((b2!=0)||(b2=a1)||(b2=b1)||(b2=a2)||(b2=d2)||(b2=b4)){
b2=rand()*5/32768;
if ((b2!=0)&&(b2!=a1)&&(b2!=b1)&&(b2!=a2)&&(b2!=d2)&&(b2!=b4)){
break;
}}

printf("b2 = %d\n",b2);

while((c2!=0)||(c2=c1)||(c2=d1)||(c2=a2)||(c2=b2)||(c2=d2)||(c2=c4)){
c2=rand()*5/32768;
if ((c2!=0)&&(c2!=c1)&&(c2!=d1)&&(c2!=a2)&&(c2!=b2)&&(c2!=d2)&&(c2!=c4)){
break;
}}

printf("c2 = %d\n",c2);

while((b3!=0)||(b3=a3)||(b3=a4)||(b3=b4)||(b3=b1)||(b3=b2||(b3=d3))){
b3=rand()*5/32768;
if ((b3!=0)&&(b3!=a3)&&(b3!=a4)&&(b3!=b4)&&(b3!=b1)&&(b3!=b2)&&(b3!=d3)){
break;
}}

printf("b3 = %d\n",b3);

while((c3!=0)||(c3=c4)||(c3=d4)||(c3=d3)||(c3=c1)||(c3=c2)||(c3=a3)||(c3=b3)){
c3=rand()*5/32768;
if ((c3!=0)&&(c3!=c4)&&(c3!=d4)&&(c3!=d3)&&(c3!=c1)&&(c3!=c2)&&(c3!=a3)&&(c3!=b3)){
break;
}}
printf("c3 = %d\n",c3);

}``````

It works but sometimes it reach absurd situations like this:
1 4 2 3
3 4
4 1
2 !

When that happens program stops and don't show the solced variables in that case a1=1 b1=4 c1=2 d1=3 a2=3 a3=4 a4=2 d2=4 d3=1

In order to solve that i've writen another code with a loop that doesn't stop the program until the last variable to be evaluated (c3) is not 0.

I've post it here:

``````//sudoku.cpp:

#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>
#include<iostream>
using std::cout;
using std::endl;
using std::cin;

void main(){

int a1;
int b1;
int c1;
int d1;
int a2;
int b2;
int c2;
int d2;
int a3;
int b3;
int c3;
int d3;
int a4;
int b4;
int c4;
int d4;
int xval;

srand(time(0));
c3=0;

while(c3==0)
{
// 1st row

a1=(rand()%4)+1;

cout <<"a1 = "<<a1<<endl;

do {
b1=(rand()%4)+1;
}while(b1==a1);
{
cout <<"b1 = "<<b1<<endl;
}

do {
c1=(rand()%4)+1;
}while((c1==a1)||(c1==b1));
{
cout <<"c1 = "<<c1<<endl;
}

do {
d1=(rand()%4)+1;
}while((d1==a1)||(d1==b1)||(d1==c1));
{
cout <<"d1 = "<<d1<<endl;
}

// Column A
do {
a2=(rand()%4)+1;
}while((a2==a1)||(a2==b1));
{
cout <<"a2 = "<<a2<<endl;
}

do {
a3=(rand()%4)+1;
}while((a3==a1)||(a3==a2));
{
cout <<"a3 = "<<a3<<endl;
}

do {
a4=(rand()%4)+1;
}while((a4==a1)||(a4==a2)||(a4==a3));
{
cout <<"a4 = "<<a4<<endl;
}

// Column D
do {
d2=(rand()%4)+1;
}while((d2==c1)||(d2==d1)||(d2==a2));
{
cout <<"d2 = "<<d2<<endl;
}
do {
d3=(rand()%4)+1;
}while((d3==d1)||(d3==d2)||(d3==a3));
{
cout <<"d3 = "<<d3<<endl;
}
do {
d4=(rand()%4)+1;
}while((d4==d1)||(d4==d2)||(d4==d3)||(d4==a4));
{
cout <<"d4 = "<<d4<<endl;
}

// 4th row

do {
b4=(rand()%4)+1;
}while((b4==a3)||(b4==a4)||(b4==b1)||(b4==d4));
{
cout <<"b4 = "<<b4<<endl;
}

do {
c4=(rand()%4)+1;
}while((c4==d3)||(c4==d4)||(c4==c1)||(c4==a4)||(c4==b4));
{
cout <<"c4 = "<<c4<<endl;
}

// Central square

do {
b2=(rand()%4)+1;
}while((b2==a1)||(b2==b1)||(b2==a2)||(b2==d2)||(b2==b4));
{
cout <<"b2 = "<<b2<<endl;
}

do {
c2=(rand()%4)+1;
}while((c2==c1)||(c2==d1)||(c2==a2)||(c2==b2)||(c2==d2)||(c2==c4));
{
cout <<"c2 = "<<c2<<endl;
}

do {
b3=(rand()%4)+1;
}while((b3==a3)||(b3==a4)||(b3==b4)||(b3==b1)||(b3==b2)||(b3==d3));
{
cout <<"b3 = "<<b3<<endl;
}

do {
c3=(rand()%4)+1;
}while((c3==c4)||(c3==d4)||(c3==d3)||(c3==c1)||(c3==c2)||(c3==a3)||(c3==b3));
{
cout <<"c3 = "<<c3<<endl;
}
if (c3!=0)
{
break;
}
}

// It shows all sudoku

cout <<""<<endl;
cout <<"Sudoku"<<endl;

cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
cout <<a2<<" "<<b2<<" "<<c2<<" "<<d2<<endl;
cout <<a3<<" "<<b3<<" "<<c3<<" "<<d3<<endl;
cout <<a4<<" "<<b4<<" "<<c4<<" "<<d4<<endl;

cout <<""<<endl;

cout <<a1<<" "<<b1<<" "<<c1<<" "<<d1<<endl;
cout <<a2<<" "<<"x"<<" "<<c2<<" "<<d2<<endl;
cout <<a3<<" "<<b3<<" "<<"y"<<" "<<d3<<endl;
cout <<"z"<<" "<<b4<<" "<<c4<<" "<<d4<<endl;

cout <<""<<endl;
while (xval!=b2){cout << "What's x value?\n";
cin >> xval;
if (xval==b2) {
cout << "OK\n";
break;
}

}
cout <<""<<endl;
while (xval!=c3){cout << "What's y value?\n";
cin >> xval;
if (xval==c3) {
cout << "OK\n";
break;
}

}
cout <<""<<endl;
while (xval!=a4){cout << "What's x value??\n";
cin >> xval;
if (xval==a4) {
cout << "OK\n";
break;
}

}
cout <<""<<endl;``````

First the loop seemed work but later i noticed it reach these absurd situacions too.
Anyone know why? i've no idea what's wrong
thank you

5
Contributors
11
Replies
12
Views
10 Years
Discussion Span
Last Post by Srynx

>>Microsoft Visual Studio 6.0
That's your first mistake. Unless you are required to use that compiler by your school you should scrap that compiler because of its age and known NON-compilance with c++ standards. You can get a pretty good free VC++ 2005 Express compiler that will let you code most everything you will learn in school and text books.

Why in the world are you using all those variables ? I thought you are supposed to be learning matrices, such as one declared like this ? That will greatly simplify your code because you only have to deal with ONE variable, not 16.

``int matrix[4][4];``

Hi,
I've wrote all code using using matrices and the problem happens even more often.
I've tried it in another compiler (DEV-C++) and with this one program just doesn't run.

can you post the corrected code??
i.e. the code after using matrices??

Hi,
I've wrote all code using using matrices and the problem happens even more often.

So rather than fix it, you took out what you were supposed to learn?

I've tried it in another compiler (DEV-C++) and with this one program just doesn't run.

That should tell you something. The code is very wrong. What happens with Dev? Crashes? Doesn't compile?

Start over and slow. Write a small piece of the code, compile, test. Then add the next step, compile, test. Repeat until the program is finished...

Hi,

can you post the corrected code??
i.e. the code after using matrices??

here the corrected code:

``````//sudoku.cpp:

#include<stdlib.h>
#include<math.h>
#include<stdio.h>
#include<time.h>
#include<algorithm>
#include<iostream>
using std::cout;
using std::endl;
using std::cin;

void main(){

int ninc=0;
int xval;
int puzzle[4][4];

srand(time(0));
puzzle[2][2]=0;

while(puzzle[2][2]==0)
{
// Fila 1

puzzle[0][0]=(rand()%4)+1;

do {
puzzle[0][1]=(rand()%4)+1;
}while(puzzle[0][1]==puzzle[0][0]);
{

}

do {
puzzle[0][2]=(rand()%4)+1;
}while((puzzle[0][2]==puzzle[0][0])||(puzzle[0][2]==puzzle[0][1]));
{

}

do {
puzzle[0][3]=(rand()%4)+1;
}while((puzzle[0][3]==puzzle[0][0])||(puzzle[0][3]==puzzle[0][1])||(puzzle[0][3]==puzzle[0][2]));
{

}

// Columna A
do {
puzzle[1][0]=(rand()%4)+1;
}while((puzzle[1][0]==puzzle[0][0])||(puzzle[1][0]==puzzle[0][1]));
{

}

do {
puzzle[2][0]=(rand()%4)+1;
}while((puzzle[2][0]==puzzle[0][0])||(puzzle[2][0]==puzzle[1][0]));
{

}

do {
puzzle[3][0]=(rand()%4)+1;
}while((puzzle[3][0]==puzzle[0][0])||(puzzle[3][0]==puzzle[0][1])||(puzzle[3][0]==puzzle[2][0]));
{

}

// Columna D
do {
puzzle[1][3]=(rand()%4)+1;
}while((puzzle[1][3]==puzzle[0][2])||(puzzle[1][3]==puzzle[0][3])||(puzzle[1][3]==puzzle[1][0]));
{

}
do {
puzzle[2][3]=(rand()%4)+1;
}while((puzzle[2][3]==puzzle[0][3])||(puzzle[2][3]==puzzle[1][3])||(puzzle[2][3]==puzzle[2][0]));
{

}
do {
puzzle[3][3]=(rand()%4)+1;
}while((puzzle[3][3]==puzzle[0][3])||(puzzle[3][3]==puzzle[1][3])||(puzzle[3][3]==puzzle[2][3])||(puzzle[3][3]==puzzle[3][0]));
{

}

// Fila 4

do {
puzzle[3][1]=(rand()%4)+1;
}while((puzzle[3][1]==puzzle[2][0])||(puzzle[3][1]==puzzle[3][0])||(puzzle[3][1]==puzzle[0][1])||(puzzle[3][1]==puzzle[3][3]));
{

}

do {
puzzle[3][2]=(rand()%4)+1;
}while((puzzle[3][2]==puzzle[2][3])||(puzzle[3][2]==puzzle[3][3])||(puzzle[3][2]==puzzle[0][2])||(puzzle[3][2]==puzzle[3][0])||(puzzle[3][2]==puzzle[3][1]));
{

}

do {
puzzle[1][1]=(rand()%4)+1;
}while((puzzle[1][1]==puzzle[0][0])||(puzzle[1][1]==puzzle[0][1])||(puzzle[1][1]==puzzle[1][0])||(puzzle[1][1]==puzzle[1][3])||(puzzle[1][1]==puzzle[3][1]));
{

}

do {
puzzle[1][2]=(rand()%4)+1;
}while((puzzle[1][2]==puzzle[0][2])||(puzzle[1][2]==puzzle[0][3])||(puzzle[1][2]==puzzle[1][0])||(puzzle[1][2]==puzzle[1][1])||(puzzle[1][2]==puzzle[1][3])||(puzzle[1][2]==puzzle[3][2]));
{

}

do {
puzzle[2][1]=(rand()%4)+1;
}while((puzzle[2][1]==puzzle[2][0])||(puzzle[2][1]==puzzle[3][0])||(puzzle[2][1]==puzzle[3][0])||(puzzle[2][1]==puzzle[0][1])||(puzzle[2][1]==puzzle[1][1])||(puzzle[2][1]==puzzle[2][3]));
{

}

do {
puzzle[2][2]=(rand()%4)+1;
}while((puzzle[2][2]==puzzle[3][2])||(puzzle[2][2]==puzzle[3][3])||(puzzle[2][2]==puzzle[2][3])||(puzzle[2][2]==puzzle[0][2])||(puzzle[2][2]==puzzle[1][2])||(puzzle[2][2]==puzzle[2][0])||(puzzle[2][2]==puzzle[2][1]));
{

}
if (puzzle[2][2]!=0)
{
break;
}
}

// Es mostra el sudoku complert

cout <<""<<endl;
cout <<"Sudoku"<<endl;

cout <<puzzle[0][0]<<" "<<puzzle[0][1]<<" "<<puzzle[0][2]<<" "<<puzzle[0][3]<<endl;
cout <<puzzle[1][0]<<" "<<puzzle[1][1]<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<endl;
cout <<puzzle[2][0]<<" "<<puzzle[2][1]<<" "<<puzzle[2][2]<<" "<<puzzle[2][3]<<endl;
cout <<puzzle[3][0]<<" "<<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<endl;

cout <<""<<endl;

cout <<puzzle[0][0]<<" "<<puzzle[0][1]<<" "<<puzzle[0][2]<<" "<<puzzle[0][3]<<endl;
cout <<puzzle[1][0]<<" "<<"x"<<" "<<puzzle[1][2]<<" "<<puzzle[1][3]<<endl;
cout <<puzzle[2][0]<<" "<<puzzle[2][1]<<" "<<"y"<<" "<<puzzle[2][3]<<endl;
cout <<"z"<<" "<<puzzle[3][1]<<" "<<puzzle[3][2]<<" "<<puzzle[3][3]<<endl;

cout <<""<<endl;
while (xval!=puzzle[1][1]){cout << "Quin \x082s el valor de x?\n";
cin >> xval;
if (xval==puzzle[1][1]) {
cout << "Felicitats, has encertat\n";
break;
}

}

cout <<""<<endl;
while (xval!=puzzle[2][2]){cout << "Quin \x082s el valor de y?\n";
cin >> xval;
if (xval==puzzle[2][2]) {
cout << "Felicitats, has encertat\n";
break;
}

}
cout <<""<<endl;
while (xval!=puzzle[3][0]){cout << "Quin \x082s el valor de z?\n";
cin >> xval;
if (xval==puzzle[3][0]) {
cout << "Felicitats, has encertat\n";
break;
}

}
cout <<""<<endl;
// Es procedeix a esborrar una quantitat de nombres definida per l'usuari

cout << "Introdueix quants n\x0A3meros vols esborrar\n";
cin >> ninc;

}``````

What happens with Dev? Crashes? Doesn't compile?

In Dev the code compiles but when i run it nothing appears in MS-DOS

Thank u 4 help

Hi,

``````// Sudoku

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream.h>
#define MAX_FILES 10
#define MAX_COLUMNES 10

//Factorial of dimensions
int suma(int dimensions){

if (dimensions<2) return dimensions;
return dimensions+suma(dimensions-1);

}

int main(){

double a[MAX_ROWS][MAX_COLUMNS];
int dimensions=0;
int i,j;
int q;
int suma(int);
int k;

cout <<"How many columns or rows there are in the sudoku?\n";
cin >> dimensions;

q=dimensions/2;
k=0;

//introducciÃ³ dels elements del sudoku

while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){

//first box
do{
for (i=0;i<q;i++){
for(j=0;j<q;j++){
a[i][j]=(rand()%dimensions)+1;
}
}

}while ((a[i][j])=!suma(dimensions));

//second box
do{
for (i=(q-1);i<dimensions;i++){
for(j=0;j<q;j++){
a[i][j]=(rand()%dimensions)+1;
}
}
}while ((a[i][j])=!suma(dimensions));

//third box
do{
for (i=0;i<q;i++){
for(j=(q-1);j<dimensions;j++){
a[i][j]=(rand()%dimensions)+1;
}
}
}while ((a[i][j])=!suma(dimensions));

//fourth box
do{
for (i=(q-1);i<dimensions;i++){
for(j=(q-1);j<dimensions;j++){
a[i][j]=(rand()%dimensions)+1;
}
}
}while ((a[i][j])=!suma(dimensions));

if ((a[i][k]==suma(dimensions))&&(a[k][j]==suma(dimensions))) {
break;
}

}

// All sudoku is showed
for (i=(q-1);i<dimensions;i++){
for(j=(q-1);j<dimensions;j++){
cout <<""<< a[i,j] <<endl;
}
}

}``````

I've used sum of all members of a box, row or column should be sudoku's dimensions factorial.
However I get a runtime error when I run the code.
How to solve it?
Thank you

Please post the error that you get.

I just get an error at runtime when I input how many columns or rows are there in the sudoku. Windows XP warn me to send an errors report and I only know it's a dwin.exe error.

It doesn't run because it won't compiler -- you have some syntax errors in the code you posted. For example, line 21 is wrong because MAX_ROWS and MAX_COLUMNS have not been defined.

>>"How many columns or rows there are in the sudoku?\n";
Well, which are you asking us to input -- the number of columns or the number of rows? If you want both then you have to have an integer for each one because they can not be both in the same integer, like this:

``````cout <<"Enter the number of columns and rows  in the sudoku?\n";
cin >> columns >> rows;``````

Hi,

``````1.  // Sudoku
2.
3.
4.  #include<stdio.h>
5.  #include<stdlib.h>
6.  #include<time.h>
7.  #include<iostream>
8.  #define MAX_FILES 10
9.  #define MAX_COLUMNES 10
10.
11. using namespace std;
12.
13.
14. int suma(int);
15.
16. //factorial sum
17. int suma(int dimensions){
18.
19.   if (dimensions<2) return dimensions;
20.      return dimensions+suma(dimensions-1);
21.
22.  }
23.
24.
25. void main(){
26.
27.      double a[MAX_ROWS][MAX_COLUMNS];
28.  	int dimensions=0;
29.      int i,j;
30.      int rows, columns;
31.    	int q;
32.   	int k;
33.
34.  rows=0;
35.  columns=0;
36.  dimensions=0;
37.  i=0;j=0;k=0;
38.
39.  srand(time(0));
40.
41.
42.
43.  cout <<"How many cloumns and rows are there in the sudoku? (put the value 3 times, 44.  please):\n";
45.    cin >> rows >> columns >> dimensions;
46.
47.
48. q=rows/2;
49.
50.
51.
52.   while ((a[i][k]=!suma(dimensions))||(a[k][j]=!suma(dimensions))){
53.
54.
55.
56.    //first box
57.   do{
58.       for (i=0;i<q;i++){
59.           for(j=0;j<q;j++){
60.              a[i][j]=(rand()%dimensions)+1;
61.              }
62.	   }
63.
64.   }while ((a[i][j])=!suma(dimensions));
65.
66.
67.    //second box
68.    do{
69.       for (i=(q-1);i<rows;i++){
70.           for(j=0;j<q;j++){
71.             a[i][j]=(rand()%dimensions)+1;
72.             }
73.	   }
74.   }while ((a[i][j])=!suma(dimensions));
75.
76.
77.    //third box
78.   do{
79.       for (i=0;i<q;i++){
80.           for(j=(q-1);j<columns;j++){
81.             a[i][j]=(rand()%dimensions)+1;
82.             }
83.	   }
84.   }while ((a[i][j])=!suma(dimensions));
85.
86.
87.    //fourth box
88.   do{
89.       for (i=(q-1);i<rows;i++){
90.           for(j=(q-1);j<columns;j++){
91.             a[i][j]=(rand()%dimensions)+1;
92.             }
93.	   }
94.   }while ((a[i][j])=!suma(dimensions));
95.
96.
97.  if ((a[i][k]==suma(dimensions))&&(a[k][j]==suma(dimensions))) {
98.       break;
99.   }
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110. }
111.
112. // All sudoku is showed
113.  for (i=0;i<rows;i++){
114.       for(j=0;j<columns;j++){
115.			cout <<""<< a[i][j] <<endl;
116.		}
117.  }
118.
119.
120.}``````

With defining columns and rows separately and some more changes (incializin 0 to i and j) the code works but when it prits the sudoku's solutions it print one value for all members of array -9.25596e+061
How to solve it?
Thanks

I've wrote a new code. I think it should work but it doesn't give any results when I run the program.
Can you help me telling me what's wrong, please?

``````// Sudoku

#include<stdio.h>
#include<fstream>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#define MAX_FILES 10
#define MAX_COLUMNES 10

using namespace std;

void main(){
int a[MAX_FILES][MAX_COLUMNES];
int i=0;
int j=0;
int dimensions=4;
int rows=4;
int columns=4;

// Initialization
for (i=0;i<rows;i++){
for(j=0;j<columns;j++){
a[i][j]=0;
}
}

time_t seconds;
srand((unsigned)time(&seconds));

while(((a[0][0]+a[0][1]+a[0][2]+a[0][3])!=10)||((a[1][0]+a[1][1]+a[1][2]+a[1][3])!=10)||((a[2][0]+a[2][1]+a[2][2]+a[2][3])!=10)||((a[3][0]+a[3][1]+a[3][2]+a[3][3])!=10)||((a[0][0]+a[1][0]+a[2][0]+a[3][0])!=10)||((a[0][1]+a[1][1]+a[2][1]+a[3][1])!=10)||((a[0][2]+a[1][2]+a[2][2]+a[3][2])!=10)||((a[0][3]+a[1][3]+a[2][3]+a[3][3])!=10)||((a[0][0]+a[0][1]+a[1][0]+a[1][1])!=10)||((a[2][0]+a[2][1]+a[3][0]+a[3][1])!=10)||((a[0][2]+a[0][3]+a[1][2]+a[1][3])!=10)||((a[2][2]+a[2][3]+a[3][2]+a[3][3])!=10)){

for (i=0;i<rows;i++){
for(j=0;j<columns;j++){
a[i][j]=(rand()%dimensions)+1;
}
}

if(((a[0][0]+a[0][1]+a[0][2]+a[0][3])==10)&&((a[1][0]+a[1][1]+a[1][2]+a[1][3])==10)&&((a[2][0]+a[2][1]+a[2][2]+a[2][3])==10)&&((a[3][0]+a[3][1]+a[3][2]+a[3][3])==10)&&((a[0][0]+a[1][0]+a[2][0]+a[3][0])==10)&&((a[0][1]+a[1][1]+a[2][1]+a[3][1])==10)&&((a[0][2]+a[1][2]+a[2][2]+a[3][2])==10)&&((a[0][3]+a[1][3]+a[2][3]+a[3][3])==10)&&((a[0][0]+a[0][1]+a[1][0]+a[1][1])==10)&&((a[2][0]+a[2][1]+a[3][0]+a[3][1])==10)&&((a[0][2]+a[0][3]+a[1][2]+a[1][3])==10)&&((a[2][2]+a[2][3]+a[3][2]+a[3][3])==10)){

cout <<"First box"<<endl;
cout <<""<< a[0][0] <<endl;
cout <<""<< a[0][1] <<endl;
cout <<""<< a[1][0] <<endl;
cout <<""<< a[1][1] <<endl;
cout <<"----------------"<<endl;
cout <<"Second box"<<endl;
cout <<""<< a[2][0] <<endl;
cout <<""<< a[2][1] <<endl;
cout <<""<< a[3][0] <<endl;
cout <<""<< a[3][1] <<endl;
cout <<"----------------"<<endl;
cout <<"Thirth box"<<endl;
cout <<""<< a[0][2] <<endl;
cout <<""<< a[0][3] <<endl;
cout <<""<< a[1][2] <<endl;
cout <<""<< a[1][3] <<endl;
cout <<"----------------"<<endl;
cout <<"Fourth box"<<endl;
cout <<""<< a[2][2] <<endl;
cout <<""<< a[2][3] <<endl;
cout <<""<< a[3][2] <<endl;
cout <<""<< a[3][3] <<endl;
cout <<"----------------"<<endl;

}
}

}``````
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.