Argh! I've been trying to work out this problem for an entire week and i'm now at my wits end! I'm supposed to hand in this assignment in 7 hours and am posting my problem here as a last resort. Can i please have someone's assistance in this? I use Dev-C++.
Basically i was given this code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
char file[] = "sud3.txt";
int main()
{
cout << file << ":" << endl << endl;
ifstream in(file);
vector<int> S;
int buffer;
while(in>>buffer)
S.push_back(buffer);
if(S.size()==81)
cout << "input worked" << endl;
else
cout << "input error" << endl;
and was told to print the sudoku puzzle and check, with the use of for-loops, if any of the numbers repeat themselves in each row, column or 3x3 box. Apparently, i am also supposed to print the numbers that repeat and state which row/column/box they are from.
So far, I have managed to print out the sudoku puzzle from the hints and examples our prof supplied us by adding this to the code:
for(int i=1;i<=9;i++)
{
if(i==4 || i==7 )
{
for(int j=0;j<19;j++)
cout << "-";
cout << endl;
}
for(int j=1;j<=9;j++)
{
if(S[(i-1)*9+j-1]=='0')
cout << " ";
else
cout << S[(i-1)*9+j-1] << " ";
if(j==3 || j==6)
cout << "|";
}
cout << endl;
}
// Now S contains the entries of Sudoku square
My prof gave an example on a method that we can use to check and print the repeat numbers. He mentioned that we'd have to refashion it to suit our purposes:
# include <cstdlib >
# include <iostream >
using namespace std;
// generate 30 random numbers in
// the range 1 ,... ,100 and check which
// numbers appears more that once
int main ()
{
bool HasOccurred [101];
// HasOccurred [i]== true will mean that
// i has occurred already
for(int i=0;i <101; i++)
HasOccurred [i]= false ;
int number ;
for(int j=0;j <30; j++)
{
number = 1+ rand ()%100;
if( HasOccurred [ number ])
cout << number << " occurred twice " << endl ;
HasOccurred [ number ]= true ;
}
system (" PAUSE ");
return EXIT_SUCCESS ;
}
Now, i know how to access and print each row, column and 3x3 box of the puzzle. But i just cannot seem to integrate the two together (the accessing with the checking).
Wwhat on earth am i doing wrong? I would really like to know how i've screwed up. I don't really care if i don't hand in this assignment, because i'm more worried about not understanding this subject since i'll have to take this course for the rest of my semester.
Here is one of my failed attempts at attempting to check and print the problems for each row. Sorry for being a noob.
bool test[10];
int number;
for(int row=1;row<=9;row++)
{
cout << "Row " << row << ": ";
for(int i=0;i<9;i++)
number= S[(row-1)*9+i];
for(int i=0; i<9; i++)
test[i]=false;
if( test[number])
{
cout << number << " is covered twice in Row: " << row << endl;
test[number]=true;
}
cout << endl;
}
I have tried doing all sorts of things..i don't really understand what significance 'test=false and test[number]=true' has though. But i do know that it has something to do with the functions of a boolean variable. May i ask what the other dumbass mistakes are? I know that i've been spoonfed with hints and examples..which is why i'm feeling so dejected (and dumb) right now. I think i just need to see how the two (accessing the numbers in the rows/columns/3x3 boxes and the checking and printing of numbers that repeat themselves) are integrated to solve it for the columns and the 3x3 boxes..that is, unless my IQ has plummeted to an all time low. Thanks in advance!