I was creating a Pascal's triangle program in C++, but the output displayed is not as expected.
Output Expected
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
Output got
1
1 1
1 2 1
1 3 3 1
1 2 2 2 1
1 6 6 6 6 1
Till i = 4, output displayed is correct, but after that I couldn't figure out how it goes wrong. Hers is the source code to get reviewed
int main()
{ int num, a[37680], t = 0, b = 2, l;
cout<<"Enter the number of rows: ";
cin>>num;
for (int i = 1; i <= num; i++)
{
for (int j = 1; j <= (num - i); j++)
{
cout<<" ";
}
for (int k = 1; k <= i; k++)
{
l = k;
if (k == 1 || k == i)
{
a[t] = 1;
cout<<a[t]<<" ";
t+=1;
}
else
{
a[t] = a[t - b] + a[t - b - 1];
cout<<a[t]<<" ";
t+=1;
if ( l = (i - 1) )
{
b+=1;
}
}
}
cout<<endl;
}
return 0;}
Equality checking in c++ is done using == and not =, so:
if(l=(i-1))
Should be:
if(l==(i-1))
I am trying to develop the code for an exercise that was assigned to me, but I can't find any way to do what I need to accomplish. The exercise in question consists of changing and printing the values of a 10x10 2d array filled with zeroes generating a pattern like this:
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 1
1 0 1 1 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 0
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 0 1
1 0 1 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0 0 0
1 0 1 0 1 0 1 0 1 1
1 0 1 1 1 0 1 1 1 0
It is probably not very noticeable, but starting from the index a[9][0] and ending at a[8][9] of the array it starts to go through changing those zeros of the array, forming a snake-like pattern. Thinking about it for a while, I tried to solve it with this code that I made, but I think it has several errors within it even though the compiler doesn't mark any so evident:
#include <iostream>
using namespace std;
int main()
{
int px = 0;
int py = 9;
int a[10][10];
for(int i = 0; i <= 10; i++) {
for(int j = 0; j <= 10; j++) {
a[i][j] = 0;
}
}
if (py == 9 && px == 0) {
while(py >= 0) {
a[py][px] = 1;
py = py - 1;
}
}
if (py == 0 && px == 0) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 0 && px == 9) {
while(py <= 2) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 2 && px == 9) {
while(px >= 2) {
a[py][px] = 1;
px = px - 1;
}
}
if (py == 2 && px == 2) {
while(py <= 9) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 9 && px == 2) {
while(px <= 4) {
a[px][py] = 1;
px = px + 1;
}
}
if (py == 9 && px == 4) {
while(py >= 4) {
a[px][py] = 1;
py = py - 1;
}
}
if (py == 4 && px == 4) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 4 && px == 9) {
while(py <= 6) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 6 && px == 9) {
while(px >= 6) {
a[py][px] = 1;
px = px - 1;
}
}
if (py == 6 && px == 6) {
while(py <= 9) {
a[py][px] = 1;
py = py + 1;
}
}
if (py == 9 && px == 6) {
while(px <= 8) {
a[py][px] = 1;
px = px + 1;
}
}
if (py == 9 && px == 8) {
while(py >= 8) {
a[py][px] = 1;
py = py - 1;
}
}
if (py == 8 && px == 8) {
while(px <= 9) {
a[py][px] = 1;
px = px + 1;
}
}
for(int k=0; k<=9; k++) {
for(int l=0; l<=9; l++) {
cout << a[k][l] << " ";
}
cout << endl;
}
return 0;
}
I hope you can help me with this, still I continue learning a little the basic thing of C++, probably it has been that it has escaped me some concept that I don't know or something like that. Thanks in advance.
Kindly refer to this
In first conv function I made a general pattern without snake
Then I focused on positions from where the snake connects other rows or column
#include <bits/stdc++.h>
using namespace std;
void conv1(int a[10][10])
{
int temp=0;
for(int i=0; i<10; i++)
{
if(i%2==0)
{
for(int j=temp; j<10; j++)
{
a[i][j]=1;
a[j][i]=1;
}
}
temp=temp+1;
}
}
/*
1 1 1 1 1 1 1 1 1 1
1 0 0 0 0 0 0 0 0 0
1 0 1 1 1 1 1 1 1 1
1 0 1 0 0 0 0 0 0 0
1 0 1 0 1 1 1 1 1 1
1 0 1 0 1 0 0 0 0 0
1 0 1 0 1 0 1 1 1 1
1 0 1 0 1 0 1 0 0 0
1 0 1 0 1 0 1 0 1 1
1 0 1 0 1 0 1 0 1 0
*/
void conv2(int a[10][10])
{
int temp=0;
for(int i=0; i<10; i++)
{
if(i%2!=0&&temp==0)
{
a[i][9]=1;
temp=1;
}
else if(i%2!=0)
{
temp=0;
}
}
for(int i=0; i<10; i++)
{
if(i%2!=0&&temp==0)
{
a[9][i]=1;
temp=1;
}
else if(i%2!=0)
{
temp=0;
}
}
a[9][9]=0;
}
int main(){
int a[10][10];
for(int i=0; i<10;i++)
{
for(int j=0; j<10; j++)
{
a[i][j]=0;
}
}
conv1(a);
conv2(a);
for(int i=0; i<10;i++)
{
for(int j=0; j<10; j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
Initially my algorithm involved testing every number from 1->n of an nxn sudoku but since it couldn't solve 16x16 sudokus (had the algorithm running for 3 hours with no luck) I thought it would be better to choose numbers in a cell based on the most valid and least used number on the board. I think I've figured out most of it, e.g. I made a 2D vector called usage (first column is number and second is usage) which I sort every time I make a change on the board. Right now I can solve an easy 9x9 and an empty 16x16 but I'm having no luck elsewhere. Wondering if somebody could help me spot where my logic is lacking.
//I used stack <pair<int, int>> to store the most recent row and column we've placed a value in. The first int is the row and the second is the column.
#include <stack>
#include <vector>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
bool isRowValid(vector <vector <int>> &sudoku, int row, int possval);
bool isColumnValid(vector <vector <int>> &sudoku, int column, int possval);
int assignBlock9x9(int pos);
int assignBlock16x16(int pos);
int assignBlock25x25(int pos);
bool isBlockValid(vector <vector <int>> &sudoku, int row, int column, int possval);
bool isValidSingle(vector <vector <int>> &sudoku, int row, int column, int possval);
bool isRowValid2(vector <vector <int>> &sudoku, int row, int possval);
bool isColumnValid2(vector <vector <int>>& sudoku, int column, int possval);
bool isBlockValid2(vector <vector <int>>& sudoku, int row, int column, int possval);
bool isValidSingle2(vector <vector <int>>& sudoku, int row, int column, int possval);
int solve(vector <vector <int>>& sudoku);
vector <vector<int>> Usage(vector <vector <int>>& sudoku);
bool sortcol(const vector<int>& v1, const vector<int>& v2);
bool sortcol2(const vector<int>& v1, const vector<int>& v2);
void printSudoku(vector <vector <int>>& sudokusudoku);
void printUsage(vector<vector<int>>& usage);
/* easy sudoku (9x9_1.txt) - actually solves
0 4 0 0 0 0 1 7 9
0 0 2 0 0 8 0 5 4
0 0 6 0 0 5 0 0 8
0 8 0 0 7 0 9 1 0
0 5 0 0 9 0 0 3 0
0 1 9 0 6 0 0 4 0
3 0 0 4 0 0 7 0 0
5 7 0 1 0 0 2 0 0
9 2 8 0 0 0 0 6 0*/
/*solution
8 4 5 6 3 2 1 7 9
7 3 2 9 1 8 6 5 4
1 9 6 7 4 5 3 2 8
6 8 3 5 7 4 9 1 2
4 5 7 2 9 1 8 3 6
2 1 9 8 6 3 5 4 7
3 6 1 4 2 9 7 8 5
5 7 4 1 8 6 2 9 3
9 2 8 3 5 7 4 6 1*/
/*medium sudoku (9x9_2.txt) - can't solve
0 9 0 3 8 4 0 0 0
0 0 2 0 7 0 0 0 0
0 0 0 0 0 0 0 7 1
5 0 0 0 0 3 2 4 0
0 3 0 0 0 0 0 0 0
0 0 1 0 0 5 0 9 0
0 0 0 8 0 0 0 0 0
7 0 6 5 2 0 0 0 0
0 0 0 0 0 6 4 0 0*/
/*solution
1 9 7 3 8 4 5 6 2
8 5 2 6 7 1 9 3 4
4 6 3 9 5 2 8 7 1
5 8 9 7 1 3 2 4 6
6 3 4 2 9 8 7 1 5
2 7 1 4 6 5 3 9 8
3 1 5 8 4 7 6 2 9
7 4 6 5 2 9 1 8 3
9 2 8 1 3 6 4 5 7*/
/* hard sudoku (9x9_3.txt) - can't solve
7 9 0 0 0 0 0 0 3
0 0 0 0 0 0 0 6 0
8 0 1 0 0 4 0 0 2
0 0 5 0 0 0 0 0 0
3 0 0 1 0 0 0 0 0
0 4 0 0 0 6 2 0 9
2 0 0 0 3 0 0 0 6
0 3 0 6 0 5 4 2 1
0 0 0 0 0 0 0 0 0*/
/*solution
7 9 2 5 6 8 1 4 3
4 5 3 2 1 9 8 6 7
8 6 1 3 7 4 9 5 2
6 2 5 8 9 3 7 1 4
3 7 9 1 4 2 6 8 5
1 4 8 7 5 6 2 3 9
2 8 4 9 3 1 5 7 6
9 3 7 6 8 5 4 2 1
5 1 6 4 2 7 3 9 8*/
/* no solution sudoku (9x9_4.txt) - can't solve
0 0 0 0 0 0 0 0 7
7 2 0 3 0 9 0 0 1
0 0 8 7 0 5 0 6 0
5 0 2 8 9 0 0 0 0
0 4 0 5 0 1 0 9 0
0 0 0 0 6 3 7 0 5
0 3 0 9 0 6 1 7 0
2 0 0 1 0 7 0 5 3
9 0 0 0 0 0 0 0 0
*/
int main()
{
fstream MySudoku("9x9_1.txt");
vector <vector <int>> sudoku;
//input from text file
if (MySudoku.is_open()) {
string row;
while (getline(MySudoku, row)) {
stringstream rowelement (row);
string num;
vector <int> row;
while (getline(rowelement, num, ' ')) {
if ((int)num[0] >= 65) {
row.push_back((int)num[0] - 55);
}
else {
row.push_back((int)num[0] - 48);
}
}
sudoku.push_back(row);
rowelement.clear();
//cout << row;
//cout << endl;
}
MySudoku.close();
}
//input from testing bot
/*string row;
int i = 0;
int rowsize = 9;
while (i < rowsize) {
getline(cin, row);
stringstream rowelement(row);
string num;
vector <int> row;
rowsize = 0;
while (getline(rowelement, num, ' ')) {
if ((int)num[0] >= 65) {
row.push_back((int)num[0] - 55);
}
else {
row.push_back((int)num[0] - 48);
//cout << 500 + (int)num[0] - 48;
}
++rowsize;
}
//cout << rowsize;
sudoku.push_back(row);
rowelement.clear();
++i;
}
*/
//solving and printing out the resulting sudoku if possible
int solved = solve(sudoku);
if (solved == 1) {
printSudoku(sudoku);
}
else {
cout << "No Solution";
}
}
void printSudoku(vector <vector <int>>& sudoku) {
for (unsigned int i = 0; i < sudoku.size(); i++) {
for (unsigned int j = 0; j < sudoku.size(); j++) {
if (sudoku[i][j] >= 10) {
if (j == sudoku.size() - 1) {
cout << (char)(sudoku[i][j] + 55);
}
else {
cout << (char)(sudoku[i][j] + 55) << " ";
}
}
else {
if (j == sudoku.size() - 1) {
cout << sudoku[i][j];
}
else {
cout << sudoku[i][j] << " ";
}
}
}
cout << endl;
}
}
void printUsage(vector<vector<int>> &usage) {
for (vector <int> usagerow : usage) {
cout << usagerow[0] << " : " << usagerow[1] << " ";
}
cout << endl << endl;
}
int solve(vector <vector <int>>& sudoku) {
stack <pair<int, int>> solution;
vector <vector <int>> usage = Usage(sudoku);
//printUsage(usage);
///////////initializes solution
int breaker = 0;
//pushes value on empty cell
for (unsigned int row = 0; row < sudoku.size(); row++) {
if (breaker == 2002) {
break;
}
for (unsigned int column = 0; column < sudoku.size(); column++) {
if (breaker == 2002) {
break;
}
if (sudoku[row][column] == 0) {
//choose option based on usage and then change usage list
int vectorpos = 0;
while (!isValidSingle(sudoku, row, column, usage[vectorpos][0]) && vectorpos != sudoku.size() - 1) {
++vectorpos;
}
//cout << usage[vectorpos][0] << " was pushed at " << row << column << endl;
sudoku[row][column] = usage[vectorpos][0];
usage[vectorpos][1] = usage[vectorpos][1] + 1;
sort(usage.begin(), usage.begin() + usage.size(), sortcol);
sort(usage.begin(), usage.begin() + usage.size(), sortcol2);
solution.push(make_pair(row, column));
breaker = 2002;
break;
}
}
}
//printSudoku(sudoku);
//just prints out the usage vector {not useful}
//printUsage(usage);
/////actual solution
bool solved = false;
while (!solved) {
//cout << "top of stack is " << solution.top().first << solution.top().second << endl;
int lastrow = solution.top().first;
int lastcolumn = solution.top().second;
//checks if sudoku is in invalid state
if (!isValidSingle2(sudoku, lastrow, lastcolumn, sudoku[lastrow][lastcolumn])) {
//copy of usage vector to allow us to see if we're at the last option for the cell
vector <vector <int>> usagefuture = usage;
for (vector<int> &usagerowdec : usagefuture) {
if (sudoku[solution.top().first][solution.top().second] == usagerowdec[0]) {
usagerowdec[1] = usagerowdec[1] - 1;
sort(usagefuture.begin(), usagefuture.begin() + usagefuture.size(), sortcol);
sort(usagefuture.begin(), usagefuture.begin() + usagefuture.size(), sortcol2);
break;
}
}
//cout << "future -> "; printUsage(usagefuture);
//keeps popping last decision until sudoku no longer in invalid state
while (sudoku[solution.top().first][solution.top().second] == usagefuture[sudoku.size()-1][0]) {
//looks for where the last entered value occurs in the usage vector
int val = sudoku[solution.top().first][solution.top().second];
for (vector <int> usagerowdec : usage) {
if (usagerowdec[0]==val) {
//sets cell to 0 and pops
sudoku[solution.top().first][solution.top().second] = 0;
//cout << usagerowdec[0] << " was set back to 0" /*<< solution.top().first << solution.top().first*/ << endl;
solution.pop();
//cout << "top of stack is now " << solution.top().first << solution.top().second << endl;
//permanently changes the usage vector
usage = usagefuture;
sort(usage.begin(), usage.begin() + usage.size(), sortcol);
sort(usage.begin(), usage.begin() + usage.size(), sortcol2);
break;
}
}
if (solution.size() == 0) {
return 0;
}
//copy of usage vector to allow us to see if we're at the last option for the cell
usagefuture = usage;
for (vector<int>& usagerowdec : usagefuture) {
if (sudoku[solution.top().first][solution.top().second] == usagerowdec[0]) {
usagerowdec[1] = usagerowdec[1] - 1;
sort(usagefuture.begin(), usagefuture.begin() + usagefuture.size(), sortcol);
sort(usagefuture.begin(), usagefuture.begin() + usagefuture.size(), sortcol2);
break;
}
}
//cout << "future -> "; printUsage(usagefuture);
}
if (solution.size() == 0) {
return 0;
}
else {
int row = solution.top().first;
int column = solution.top().second;
//printUsage(usage);
//decrements usage vector at value we're changing
for (vector<int>& usagerowdec : usage) {
if (sudoku[row][column] == usagerowdec[0]) {
usagerowdec[1] = usagerowdec[1] - 1;
sort(usage.begin(), usage.begin() + usage.size(), sortcol);
sort(usage.begin(), usage.begin() + usage.size(), sortcol2);
break;
}
}
//printSudoku(sudoku);
//finds which value we're on in the usage vector
int vectorpos = 0;
for (int i = 0; i < usage.size(); i++) {
if (sudoku[row][column] == usage[i][0]) {
vectorpos = i;
break;
}
}
//sees what next value we can set it to. If nothing it becomes the last possible value
while (!isValidSingle(sudoku, row, column, usage[vectorpos][0]) && vectorpos != sudoku.size() - 1) {
vectorpos++;
}
//cout << "vectorpos is " << vectorpos << ": ";
//completes the change
//cout << sudoku[row][column] << " was changed to " << usage[vectorpos][0] << " at " << row << column << endl;
sudoku[row][column] = usage[vectorpos][0];
usage[vectorpos][1] = usage[vectorpos][1] + 1;
sort(usage.begin(), usage.begin() + usage.size(), sortcol);
sort(usage.begin(), usage.begin() + usage.size(), sortcol2);
}
}
//if sudoku in valid state
else {
int breaker = 0;
//looks for empty cell to push to
for (unsigned int row = 0; row < sudoku.size(); row++) {
if (breaker == 2002) {
break;
}
for (unsigned int column = 0; column < sudoku.size(); column++) {
if (breaker == 2002) {
break;
}
if (sudoku[row][column] == 0) {
//choose option based on usage and then change usage list, worst case is the last value like with the change
int vectorpos = 0;
while (!isValidSingle(sudoku, row, column, usage[vectorpos][0]) && vectorpos != sudoku.size() - 1) {
++vectorpos;
}
//cout << usage[vectorpos][0] << " was pushed at " << row << column << endl;
sudoku[row][column] = usage[vectorpos][0];
usage[vectorpos][1] = usage[vectorpos][1] + 1;
sort(usage.begin(), usage.begin() + usage.size(), sortcol);
sort(usage.begin(), usage.begin() + usage.size(), sortcol2);
solution.push(make_pair(row, column));
breaker = 2002;
break;
}
}
}
}
//checks if we're at the end
if (solution.top().first == sudoku.size() - 1 && solution.top().second == sudoku.size() - 1){
solved = true;
}
//printSudoku(sudoku);
//prints out usage vector {not useful}
//printUsage(usage);
}
return 1;
}
bool isRowValid(vector <vector <int>> &sudoku, int row, int possval) {
for (unsigned int i = 0; i < sudoku.size(); i++) {
if (possval == sudoku[row][i]) {
return false;
}
}
return true;
}
bool isColumnValid(vector <vector <int>> &sudoku, int column, int possval) {
for (unsigned int j = 0; j < sudoku.size(); j++) {
if (possval == sudoku[j][column]) {
return false;
}
}
return true;
}
int assignBlock9x9(int pos) {
if (0 <= pos && pos <= 2) {
return 0;
}
else if (3 <= pos && pos <= 5) {
return 3;
}
else {
return 6;
}
}
int assignBlock16x16(int pos) {
if (0 <= pos && pos <= 3) {
return 0;
}
else if (4 <= pos && pos <= 7) {
return 4;
}
else if (8 <= pos && pos <= 11){
return 8;
}
else {
return 12;
}
}
int assignBlock25x25(int pos) {
if (0 <= pos && pos <= 4) {
return 0;
}
else if (5 <= pos && pos <= 9) {
return 5;
}
else if(10 <= pos && pos <= 14){
return 10;
}
else if (15 <= pos && pos <= 19) {
return 15;
}
else {
return 20;
}
}
bool isBlockValid(vector <vector <int>> &sudoku, int row, int column, int possval) {
//if 9x9
if (sudoku.size() == 9) {
int startrow = assignBlock9x9(row);
int startcolumn = assignBlock9x9(column);
for (int i = startrow; i <= startrow + 2; i++) {
for (int j = startcolumn; j <= startcolumn + 2; j++) {
if (possval == sudoku[i][j]) {
return false;
}
}
}
}
//if 16x16
else if (sudoku.size()==16) {
int startrow = assignBlock16x16(row);
int startcolumn = assignBlock16x16(column);
for (int i = startrow; i <= startrow + 3; i++) {
for (int j = startcolumn; j <= startcolumn + 3; j++) {
if (possval == sudoku[i][j]) {
return false;
}
}
}
}
//if 25x25
else {
int startrow = assignBlock25x25(row);
int startcolumn = assignBlock25x25(column);
for (int i = startrow; i <= startrow + 4; i++) {
for (int j = startcolumn; j <= startcolumn + 4; j++) {
if (possval == sudoku[i][j]) {
return false;
}
}
}
}
return true;
}
bool isValidSingle(vector <vector <int>> &sudoku, int row, int column, int possval) {
if (isRowValid(sudoku, row, possval) && isColumnValid(sudoku, column, possval) && isBlockValid(sudoku, row, column, possval)) {
return true;
}
return false;
}
bool isRowValid2(vector <vector <int>> &sudoku, int row, int possval) {
int samecount = 0;
for (unsigned int i = 0; i < sudoku.size(); i++) {
if (sudoku[row][i] == possval) {
++samecount;
}
if (samecount == 2) {
return false;
}
}
return true;
}
bool isColumnValid2(vector <vector <int>> &sudoku, int column, int possval) {
int samecount = 0;
for (unsigned int j = 0; j < sudoku.size(); j++) {
if (sudoku[j][column] == possval) {
++samecount;
}
if (samecount == 2) {
return false;
}
}
return true;
}
bool isBlockValid2(vector <vector <int>>& sudoku, int row, int column, int possval) {
int samecount = 0;
//if 9x9
if (sudoku.size() == 9) {
int startrow = assignBlock9x9(row);
int startcolumn = assignBlock9x9(column);
for (int i = startrow; i <= startrow + 2; i++) {
for (int j = startcolumn; j <= startcolumn + 2; j++) {
if (possval == sudoku[i][j]) {
++samecount;
}
if (samecount == 2) {
return false;
}
}
}
}
//if 16x16
else if (sudoku.size() == 16) {
int startrow = assignBlock16x16(row);
int startcolumn = assignBlock16x16(column);
for (int i = startrow; i <= startrow + 3; i++) {
for (int j = startcolumn; j <= startcolumn + 3; j++) {
if (possval == sudoku[i][j]) {
++samecount;
}
if (samecount == 2) {
return false;
}
}
}
}
//if 25x25
else {
int startrow = assignBlock25x25(row);
int startcolumn = assignBlock25x25(column);
for (int i = startrow; i <= startrow + 4; i++) {
for (int j = startcolumn; j <= startcolumn + 4; j++) {
if (possval == sudoku[i][j]) {
++samecount;
}
if (samecount == 2) {
return false;
}
}
}
}
return true;
}
bool isValidSingle2(vector <vector <int>> &sudoku, int row, int column, int possval) {
if (isRowValid2(sudoku, row, possval) && isColumnValid2(sudoku, column, possval) && isBlockValid2(sudoku, row, column, possval)) {
return true;
}
return false;
}
bool sortcol(const vector<int>& v1, const vector<int>& v2) {
return v1[1] < v2[1];
}
bool sortcol2(const vector<int>& v1, const vector<int>& v2) {
return v1[0] < v2[0] && v1[1] == v2[1];
}
vector <vector <int>> Usage(vector <vector <int>> &sudoku) {
vector <int> usage;
vector <vector <int>> usage2;
for (unsigned int i = 0; i < sudoku.size(); i++) {
usage.push_back(0);
}
for (unsigned int row = 0; row < sudoku.size(); row++) {
for (unsigned int column = 0; column < sudoku.size(); column++) {
if (sudoku[row][column] == 0) {
continue;
}
usage[sudoku[row][column] -1] = usage[sudoku[row][column] - 1] + 1;
}
}
for (unsigned int i = 1; i <= sudoku.size(); i++) {
usage2.push_back({(int) i, usage[i - 1] });
}
sort(usage2.begin(), usage2.begin() + usage2.size(), sortcol);
sort(usage2.begin(), usage2.begin() + usage2.size(), sortcol2);
return usage2;
}
I'm trying to generate all the undirected graphs with n nodes, using recursive backtracking. I have to write their matrix (I don't know how is it called in english - in my language it would be adjacent matrix - is that right?) into a file.
For example:
input
3
output
8
0 0 0
0 0 0
0 0 0
0 0 0
0 0 1
0 1 0
0 0 1
0 0 0
1 0 0
0 0 1
0 0 1
1 1 0
0 1 0
1 0 0
0 0 0
0 1 0
1 0 1
0 1 0
0 1 1
1 0 0
1 0 0
0 1 1
1 0 1
1 1 0
Here is my program:
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("gengraf.in");
ofstream g("gengraf.out");
int st[100], n, adiacenta[100][100], l=1;
void tipar(int k)
{
for (int i = 1; i < k; i++)
{
for (int j = i+1; j < k; j++)
{
adiacenta[i][j] = adiacenta[j][i] = st[l];
}
l++;
}
for (int i = 1; i < k; i++)
{
for (int j = 1; j < k; j++)
{
g << adiacenta[i][j] << " ";
}
g << endl;
}
}
int valid(int k)
{
return 1;
}
void back(int k)
{
if (k == ((n - 1) * n / 2) + 1)
{
l = 1;
tipar(k);
g << endl;
}
else
{
for (int i = 0; i <= 1; i++)
{
st[k] = i;
if (valid(k))
{
back(k + 1);
}
}
}
}
int main()
{
f >> n;
g << pow(2, (n * (n - 1))/2);
g << endl;
back(1);
}
but my output is:
8
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 1
0 1 0
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 0
0 1 1
1 0 0
1 0 0
0 1 1
1 0 1
1 1 0
0 1 1
1 0 1
1 1 0
and I don't know how to fix that.
I see why does happen - I generate 2^(n*(n-1))/2) graphs (because that's how many undirected graphs with n nodes are), and instead of generating 8 distinct ones, I get only 4 distinct ones, shown 2 times.
That is (I suppose) because my program outputs a graph with, let's say, a link between the node 1 and 3 and another graph with a link between the node 3 and 1. And that is basically the same undirected graph.
So if I am right, I should make my program not show each graph twice and it should work. So basically I have to get rid of each graph with the "reversed" node (so if I got one with a link between 1 and 3, I shouldn't get another one with a link between 3 and 1 because they are the same).
Am I right?
If so, how can I do that?
Thanks.
Problems with your code:
Value of l in tipar() id not increased after assignment.
Size of adjacency matrix is n * n not k * k.
This code work as expected.
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("gengraf.in");
ofstream g("gengraf.out");
int st[100], n, adiacenta[100][100], l=1;
int pow(int a, int b) {
int r = 1;
while (b) {
if (b&1) r *= a;
b >>= 1;
a *= a;
}
return r;
}
void tipar()
{
for (int i = 1; i <= n; i++)
{
for (int j = i+1; j <= n; j++)
{
adiacenta[i][j] = adiacenta[j][i] = st[l];
l++;
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
g << adiacenta[i][j] << " ";
}
g << endl;
}
}
int valid(int k)
{
return 1;
}
void back(int k)
{
if (k == (n * (n-1) / 2) + 1)
{
l = 1;
tipar();
g << endl;
}
else
{
for (int i = 0; i <= 1; i++)
{
st[k] = i;
if (valid(k))
{
back(k+1);
}
}
}
}
int main()
{
cin >> n;
g << pow(2, (n * (n - 1))/2);
g << endl;
back(1);
}
I want to change the for-loop to block scheme
I have this for loop that does this:
let say n = 8
and node = 4
n: [1][2][3][4][5][6][7][8]
id: 0 1 2 3 0 1 2 3
id = 0;
while (id < node){
for (i = id + 1; i <= n; i = i + node)
{
//do stuff here
id = i;
}enter code here
id+1;
}//end while
and i want it to do this:
n = 8
node= 4
n: [1][2][3][4][5][6][7][8]
id: 0 0 1 1 2 2 3 3
n = 16
node = 4
n: [1][2][3][4][5][6][7][8] ... [13][14][15][16]
id: 0 0 0 0 1 1 1 1 ... 3 3 3 3
n = 8
node= 2
n: [1][2][3][4][5][6][7][8]
id: 0 0 0 0 1 1 1 1
where each id is assign to the top n show in examples
I have this but it only works for the specific scenario n= 8 & node = 4
...
b = id + 1
for (i = n-(n-(id+b)); i <= (n-(n-(id+b))+1); i+= 1)
...
I perhaps misunderstood your question but this gives the output for id as you described:
for (int i = 0; i < n; i++)
{
id = i / (n / node);
// do stuff
}
I use n starting from 0, but if you start from 1, just adjust the calculation by ofsetting everything to n-1.
Try this code:
int node = 4;
int n = 16;
for (id = 0; id < node; id++) {
for(int j = 0; j < n / node; j++) {
//do some stuff
}
}
Or you can use this code:
int node = 4;
int n = 16;
for (int i = 0; i < node; i++) {
id = i;
for(int j = 0; j < n / node; j++) {
//do some stuff
}
}
They do the same.