problems with arrays and loops
Jul 16, 2008 at 2:22am UTC
ok so im writing a soduku puzzle validator to make sure the puzzle is correct. where the numbers 1-9 can only appear once on every line. i got that part of the code working except i need help outputing a line that would look like this
cout<<"duplicate"<<number<<"on row:"<<row<<" columns: "<<columns its repeated on
here is my code so far if anyone can help i would appreciate it Thank you in advance.
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
void readAPuzzle(int grid[] [9]);
void printGrid(int grid[] [9]);
void ValidPuzzle(int grid[] [9]);
int main()
{
int grid[9][9];
readAPuzzle(grid);
printGrid(grid);
ValidPuzzle(grid);
system("pause" );
return 0;
}
void readAPuzzle(int grid[] [9])
{
ifstream in_f;
in_f.open("soduku.txt" );
for (int r = 0; r < 9; r++)
for (int c = 0; c < 9; c++)
in_f >> grid[r] [c];
}
//Prints out the puzzle read from file (keep)
void printGrid(int grid[] [9])
{
cout<<"Your Puzzle Is Listed Below: " <<endl;
for (int r = 0; r < 9; r++)
{
if ((r)%3 == 0)
cout<<endl<<endl;
for (int c = 0; c < 9; c++)
{
cout << (grid[r] [c]) << " " ;
if ((c+1)%3 == 0)
cout<<" " ;
}
cout<<endl;
}
cout<<endl;
}
//Check Validity Of Puzzle
void ValidPuzzle(int grid[] [9])
{
int sum = 0;
int nototest = 0;
int count = 0;
int no_to_test;
// check columns
for (int c=0;c<9;c++)
for (int r=0;r<9;r++)
{
no_to_test = r+1;
count=0;
for (int r=0;r<9;r++)
{
if (grid[r][c]==no_to_test)
count++;
}
if (count>1)
{
cout <<"The Number: " <<no_to_test<< " is duplicated on column: " << c+1;
cout<<endl;
}
}
// check rows
cout<<endl;
for (int r=0;r<9;r++)
for (int c=0;c<9;c++)
{
no_to_test = c+1;
count=0;
for (int c=0;c<9;c++)
{
if (grid[r][c]==no_to_test)
count++;
}
if (count>1)
{
cout<<"The Number: " <<no_to_test<< " is duplicated on row: " << r+1;
cout<<endl;
}
}
cout<<endl;
}
Topic archived. No new replies allowed.