Hi, i am currently taking a module in c++ in the university, and was given an assignment. because i have no prior background on the subject, everything is kind of new to me. i have tried for quite some time and still not able to get the solution out. so i hope you guys can help me out. of course i am not expecting a full solution, but i would greatly appreciate it if anyone can suggest to me what should i do. i am very new to this subject so please help me out as much as you can if you have the time. i have put the questions as well as my half-worked answers here. please have a look. thanks!
the assignment:
Assignment 1, Instructions
Problem: Write a C++ program that checks if a 9x9 Sudoku square is filled in correctly. The
81 numbers in the Sudoku square have to be read from a textfile which contains the numbers
row-wise, separated by spaces.
Instructions:
The Sudoku rules are explained in the attached document on the Sudoku rules. As in this
document, we call the 3x3 subsquares of a Sudoku square regions.
The square is saved in a vector<int> S with 81 entries such that the entry in row i and
column j is S[9*(i-1)+j-1].
• Compile and run Assignment1.cpp . It will read the Sudoku square from the file sud1.txt
(which you should have saved) and print a
message to the screen indicating if the input has worked.
• Crucial part: put C++ commands to check
if the Sudoku square is filled in according to the rules. Details:
• Whenever any number 1,2,...,9 appears more than once in any row, column, or region of
the square, this fact should be printed to the screen (by a cout command).
• If the square is completely correct, nothing should be printed to the screen.
• You program will be tested on further examples. Therefore, it is recommended that you
do more tests to make sure your program is correct.
• The following program contains a basic idea needed for checking if a number occurs at
least twice among several numbers. You can use this idea (but not the program itself - it
has to be adjusted to fit the purpose).
# 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 ;
}
my half-worked on solution:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
char file[] = "sud1.txt";
int main(int argc, char *argv[])
{
cout << file << ":" << endl << endl;
ifstream in("C:\Document s and Settings\HP\Des ktop\assignment \sud1.txt");
vector<int> S;
int buffer;
while(in>>buffe r)
S.push_back(buf fer);
if(S.size()==81 )
cout << "input worked" << endl;
else
cout << "input error" << endl;
int counter=0;
for(int i=1;i<4;i++)&&f or(int i=4;i<7;i++)&&f or(int i=7;i<10;i++))
{
for(int j=1;j<4;j++)&&f or(int j=4;j<7;i++)&&f or(int j=7;j<10;i++))
{
bool S[9];
for (int i=1;i<10;i++)
{
for (int j=1;j<10;i++)
if(S[i])
counter++;
}
}
cout<< "A[i] has occurred "<< counter << "times" << endl;
}
for((int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
bool S[9];
for (int i=1;i<10;i++)
{
for (int j=1;j<10;i++)
if(S[i])
counter++;
}
}
cout<< "A[i] has occurred "<< counter << "times" << endl;
}
system("PAUSE") ;
return EXIT_SUCCESS;
}
my answer might seem stupid to you, but this what i can come up with after many hours.
the assignment:
Assignment 1, Instructions
Problem: Write a C++ program that checks if a 9x9 Sudoku square is filled in correctly. The
81 numbers in the Sudoku square have to be read from a textfile which contains the numbers
row-wise, separated by spaces.
Instructions:
The Sudoku rules are explained in the attached document on the Sudoku rules. As in this
document, we call the 3x3 subsquares of a Sudoku square regions.
The square is saved in a vector<int> S with 81 entries such that the entry in row i and
column j is S[9*(i-1)+j-1].
• Compile and run Assignment1.cpp . It will read the Sudoku square from the file sud1.txt
(which you should have saved) and print a
message to the screen indicating if the input has worked.
• Crucial part: put C++ commands to check
if the Sudoku square is filled in according to the rules. Details:
• Whenever any number 1,2,...,9 appears more than once in any row, column, or region of
the square, this fact should be printed to the screen (by a cout command).
• If the square is completely correct, nothing should be printed to the screen.
• You program will be tested on further examples. Therefore, it is recommended that you
do more tests to make sure your program is correct.
• The following program contains a basic idea needed for checking if a number occurs at
least twice among several numbers. You can use this idea (but not the program itself - it
has to be adjusted to fit the purpose).
# 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 ;
}
my half-worked on solution:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
char file[] = "sud1.txt";
int main(int argc, char *argv[])
{
cout << file << ":" << endl << endl;
ifstream in("C:\Document s and Settings\HP\Des ktop\assignment \sud1.txt");
vector<int> S;
int buffer;
while(in>>buffe r)
S.push_back(buf fer);
if(S.size()==81 )
cout << "input worked" << endl;
else
cout << "input error" << endl;
int counter=0;
for(int i=1;i<4;i++)&&f or(int i=4;i<7;i++)&&f or(int i=7;i<10;i++))
{
for(int j=1;j<4;j++)&&f or(int j=4;j<7;i++)&&f or(int j=7;j<10;i++))
{
bool S[9];
for (int i=1;i<10;i++)
{
for (int j=1;j<10;i++)
if(S[i])
counter++;
}
}
cout<< "A[i] has occurred "<< counter << "times" << endl;
}
for((int i=1;i<10;i++)
{
for(int j=1;j<10;j++)
{
bool S[9];
for (int i=1;i<10;i++)
{
for (int j=1;j<10;i++)
if(S[i])
counter++;
}
}
cout<< "A[i] has occurred "<< counter << "times" << endl;
}
system("PAUSE") ;
return EXIT_SUCCESS;
}
my answer might seem stupid to you, but this what i can come up with after many hours.
Comment