- #1
steelband
- 2
- 0
Please help in c++ urgent
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.
// i has occurred already
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!
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.
// i has occurred already
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: