• C/++/#

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:
Help with Code Tags
cpluspluis Syntax (Toggle Plain Text)

1.
#include <cstdlib>
2.
#include <iostream>
3.
#include <fstream>
4.
#include <vector>
5.
using namespace std;
6.

7.
char file[] = "sud3.txt";
8.

9.
int main()
10.
{
11.
cout << file << ":" << endl << endl;
12.
ifstream in(file);
13.
vector<int> S;
14.
int buffer;
15.
while(in>>buffer)
16.
S.push_back(buffer);
17.
if(S.size()==81)
18.
cout << "input worked" << endl;
19.
else
20.
cout << "input error" << endl;

#include <cstdlib> #include <iostream> #include <fstream> #include <vector> using namespace std; char file[] = "sud3.txt"; int main() { cout << file << ":" << endl << endl; ifstream <strong class="highlight">in</strong>(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:
Help with Code Tags
cplusplus Syntax (Toggle Plain Text)

1.
for(int i=1;i<=9;i++)
2.
{
3.
if(i==4 || i==7 )
4.
{
5.
for(int j=0;j<19;j++)
6.
cout << "-";
7.
cout << endl;
8.
}
9.
for(int j=1;j<=9;j++)
10.
{
11.
if(S[(i-1)*9+j-1]=='0')
12.
cout << " ";
13.
else
14.
cout << S[(i-1)*9+j-1] << " ";
15.
if(j==3 || j==6)
16.
cout << "|";
17.
}
18.
cout << endl;
19.
}
20.
// Now S contains the entries of Sudoku square

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:
Help with Code Tags
cplusplus Syntax (Toggle Plain Text)

1.
# include <cstdlib >
2.
# include <iostream >
3.
using namespace std;
4.
// generate 30 random numbers in
5.
// the range 1 ,... ,100 and check which
6.
// numbers appears more that once
7.
int main ()
8.
{
9.
bool HasOccurred [101];
10.
// HasOccurred == true will mean that
11.
12.
for(int i=0;i <101; i++)
13.
HasOccurred = false ;
14.
int number ;
15.
for(int j=0;j <30; j++)
16.
{
17.
number = 1+ rand ()%100;
18.
if( HasOccurred [ number ])
19.
cout << number << " occurred twice " << endl ;
20.
HasOccurred [ number ]= true ;
21.
}
22.
system (" PAUSE ");
23.
return EXIT_SUCCESS ;
24.
}

# include <cstdlib > # include <iostream > using namespace std; // generate 30 random numbers <strong class="highlight">in</strong> // the range 1 ,... ,100 and check which // numbers appears more that once int main () { bool HasOccurred [101]; // HasOccurred == true will mean that // i has occurred already for(int i=0;i <101; i++) HasOccurred = 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.
Help with Code Tags
cplusplus Syntax (Toggle Plain Text)

1.
bool test[10];
2.
int number;
3.
for(int row=1;row<=9;row++)
4.
{
5.
cout << "Row " << row << ": ";
6.
for(int i=0;i<9;i++)
7.
number= S[(row-1)*9+i];
8.

9.
for(int i=0; i<9; i++)
10.
test=false;
11.
if( test[number])
12.
{
13.
cout << number << " is covered twice in Row: " << row << endl;
14.
test[number]=true;
15.
}
16.

17.
cout << endl;
18.
}

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=false; if( test[number]) { cout << number << " is covered twice <strong class="highlight">in</strong> 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!

Last edited by a moderator:

This post is very hard to read. I don't understand what it is you are trying to ask.

- Use the "code" tag (there's an icon above the message area) to wrap code samples.
- Be careful when you copypaste. Obviously "Help with Code Tags" doesn't help us any, also obviously we won't be readily able to compile any code samples you post if there are line numbers scattered throughout.
- Always be SPECIFIC: *EXACTLY* what isn't working? What is it doing instead of working?

At least some of the test=false type things, it's there because when you declare an array, like:

bool HasOccurred [101];

...the initial contents of that array will be effectively random, or rather will contain whatever was sitting around in memory. Therefore before you use HasOccurred you ought to say something like:

for(int i = 0; i < 101; i++) HasOccurred = false;

So that the contents of HasOccurred will be initialized to something known.

(I also notice that you declare a:
bool test[10];
then zero it out with:
for(int i=0; i<9; i++) test=false;
This will not zero out the entire array! You will miss the tenth element.)

Probably the purpose of setting test[0..9] to false, then going back and setting some values of test[number] to true, is that it is marking "I have not seen any of the digits 0 through 9" and then for the numbers it does see, marking "I have seen this number". After it does this test[number] will be true for the values it saw and false for the values it didn't.

Since I don't understand what problem you're having this is probably as much as I can tell you. I am heading off to bed and probably will not see any responses to this post before your seven-hour deadline...

When you have code to show, anything more than 10 lines, you would be wise to state what the code is supposed to do, and what it actually does. Show also what you have done to fix it.
You have done well in stating the compiler, because some problems are compiler specific.

The code tags are not hard to do, here is an example,
(code)
#include <stdio.h>
...
(/code)

If you replace the parentheses () above by square brackets [ ], you will see this:

Code:
#include <stdio.h>
...

Finally, if it is not too late, you can still post again and get help.

I will be off for a few minutes and will be back to look out for you.

Transcription:

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:

Code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

char file[] = "sudtxt";

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;

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:

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:

Code:
# 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

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.

Code:
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!

Last edited:

To facilitate verifications, could you post also the data file that the program is supposed to read?

Code:
for(int i=1;i<=9;i++)
Since you have not posted the declaration of S[][], I would like to confirm that you have declared S as 10x10, since C++ arrays start with subscripts of 0, so a 10x10 array will go from S[0][0] to S[9][9].

My suggestion is to stick to the C++ convention of working with 0 to 9 instead of 1 to 10 leaving some cells unused. In fact, you use the 0-9 convention later in the array 'test', which would make reading your code difficult, but most of all, it would be even more difficult for yourself to verify subscripts.

By reading through your teacher's example for checking duplicates, you will note that he initializes the test array to false before (and not during) the actual testing. In your testing method, you have a problem of the scope of each for-loop which make your code not work. If you format the code with the usual indenting style, it will avoid a lot of problems.

If your code is indented, and implicit braces added, it looks like this:
Code:
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;
}
You will see some problems:
1. number= S[(row-1)*9+i]; // S is a 2-D array!!!
So the statement will probably not compile.
In addition, you try to do it nine times before you do anything else. So the 8 previous values of number get lost.
The problem is the for(i=..) loop should embrace the rest of the code, so that you work with one cell at a time. If you don't use braces to enclose your code following the for(.. loop, the loop will use only the next statement.
2. You have set test to be false and then test right after. So it will always test negative. See second paragraph of point 1 above.

I suggest you rework your code, make sure it compiles correctly, and post the complete code in one piece so there will not be parts of the code that we don't see which may turn out to be critical.

Tell us HOW you expect the code to work, and also how it ACTUALLY works. Tell us also what is not right, according to you.

If you are not able to make it compile, post the complete error messages and the code.

Last edited:

I've just terminated another post. I will look at your latest post and get back to you presently.

I'm not the OP, I just fixed his post.

Sigh!!!
It looks like other people are working harder than the OP.
Thanks!