#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
const int MAX = 9;
typedef char BOARD[MAX][MAX];
void dis_board(BOARD);
void new_board(BOARD, ifstream& in);
void add();
void remove(BOARD);
bool chk_row(BOARD arr, int row, int col);
bool chk_col(BOARD arr, int row, int col);
bool chk_sqr(BOARD arr, int row, int col);
int main()
{
BOARD board;
int row=0, col=0, num_ele = 0, num;
char ch1;
ifstream infile;
infile.open("mp6in.txt");
infile.get(ch1);
while (infile)
{
if (ch1 == 'N')
new_board(board, infile);
if (chk_row == false)
cout << "There is an error" << endl;
if (chk_col == false)
cout << "There is an error" << endl;
chk_sqr(board, 0, 0);
if (chk_sqr == false)
cout << "There is an error" << endl;
if (ch1 == 'D')
dis_board(board);
infile.get(ch1);
}
}
void dis_board(BOARD arr)
{
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
cout << setw(3) << arr[i][j];
cout << endl;
}
cout << endl;
}
void new_board(BOARD arr, ifstream& in)
{
in.ignore(100, '\n');
for (int i = 0; i < MAX; i++)
{
for (int j = 0; j < MAX; j++)
in.get(arr[i][j]);
in.ignore(100, '\n');
}
}
bool chk_row(BOARD arr, int row, int col)
{
for (int i = 0; i < MAX; i++)
{
if (i != col)
{
if (arr[row][i] == arr[row][col])
{
return false;
}
}
}
return true;
}
bool chk_col(BOARD arr, int row, int col)
{
for (int i = 0; i < MAX; i++)
{
if (i != row)
{
if (arr[i][col] == arr[row][col])
{
return false;
}
}
}
return true;
}
bool chk_sqr(BOARD arr, int row, int col)
{
int asquare = row / 3;
int bsquare = col / 3;
for (int i = asquare * 3; i < (asquare * 3 + 3); i++)
{
for (int j = bsquare * 3; j < (bsquare * 3 + 3); j++)
{
if (!(i == row && j == col))
{
if (arr[row][col] == arr[i][j])
{
return false;
}
}
}
}
return true;
}
Just in case I'm talking to anyone besides myself. I tried implementing the following checking functions and of course they aren't working correctly. For instance I know my first board has a duplicate number in the lower right block but my function can't find it.
chk_row is a function - you need to call it. Currently you are just checking with:
if (chk_row == false)
This is just checking if the function pointer is false, which is never going to happen. You need to call the function like this:
if (chk_row(board, row, col) == false)
You will need to work out what row and col should be. Probably you need a loop to check all the rows and columns, but I figure this is homework, so you should try it yourself.
Related
#include <iostream>
#include <fstream>
using namespace std;
void PrintSolution(int board[8][8], ofstream& out, int& count)
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++) {
out << board[i][j] << " ";
}
out << endl;
}
out << endl;
count++;
}
int IsSafely(int board[][8], int row, int col)
{
for (int i = 0; i < row; i++)
{
if (board[i][col] == 1)
{
return 0;
}
}
for (int i = row, j = col; i >= 0 && j >= 0; i--, j--)
{
if (board[i][j] == 1)
{
return 0;
}
}
for (int i = row, j = col; i >= 0 && j < 8; i--, j++)
{
if (board[i][j] == 1)
{
return 0;
}
}
return 1;
}
void FindSolution(int board[][8], int row, ofstream& out, int& count)
{
if (row == 8)
{
PrintSolution(board, out, count);
return;
}
for (int i = 0; i < 8; i++)
{
if (IsSafely(board, row, i))
{
board[row][i] = 1;
FindSolution(board, row + 1, out, count);
board[row][i] = 0;
}
}
}
void FillTheBoard(int board[][8])
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
board[i][j] = 0;
}
}
}
int main()
{
int board[8][8];
int count = 0;
ofstream output;
output.open("out.txt");
FillTheBoard(board);
FindSolution(board, 0, output, count);
output << count << endl;
output.close();
}
Hello everyone, I have a program that counts the number of arrangements of 8 queens on an 8x8 chessboard. I need to make a program to count the number of arrangements for 5 queens on an 8x8 board. Could you help me? What can I change in my program? For 8 queens, the program works correctly, it gives 92 solutions. For 5 queens, there should be 728 solutions on the 8x8 board.
I am coding a Conway's Game of Life.My task is reading from file.txt to array of strings.And then using this array as the input array of the game. I have written a code about it. But it is full of error. I don't know what is the correct way to do this.
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <fstream>
using namespace std;
namespace gamearray {
int main()
ifstream file_("file.txt");
if(file.is_open())
{
string myArray[10];
for(int i = 0; i < 10; ++i)
{
for(int i = 0; i < 10; i++)
file >> array1[j][i];
}
}
return 0;
}
void copy(int array1[10][10], int array2[10][10])
{
for(int j = 0; j < 10; j++)
{
for(int i = 0; i < 10; i++)
array2[j][i] = array1[j][i];
}
}
void life(int array[10][10], char choice)
{
int temp[10][10];
copy(array, temp);
for(int j = 0; j < 10; j++)
{
for(int i = 0; i < 10; i++)
{
if(choice == 'm')
{
int count = 0;
count = array[j-1][i] +
array[j-1][i-1] +
array[j][i-1] +
array[j+1][i-1] +
array[j+1][i] +
array[j+1][i+1] +
array[j][i+1] +
array[j-1][i+1];
if(count < 2 || count > 3)
temp[j][i] = 0;
if(count == 2)
temp[j][i] = array[j][i];
if(count == 3)
temp[j][i] = 1;
}
}
}
copy(temp, array);
}
bool compare(int array1[10][10], int array2[10][10])
{
int count = 0;
for(int j = 0; j < 10; j++)
{
for(int i = 0; i < 10; i++)
{
if(array1[j][i]==array2[j][i])
count++;
}
}
if(count == 10*10)
return true;
else
return false;
}
void print(int array[10][10])
{
for(int j = 0; j < 10; j++)
{
for(int i = 0; i < 10; i++)
{
if(array[j][i] == 1)
cout << '*';
else
cout << ' ';
}
cout << endl;
}
}
int main()
{gamearray::main();
int gen0[10][10];
int todo[10][10];
int backup[10][10];
char neighborhood;
char again;
char cont;
bool comparison;
{do
{
do
{
cout << "Which neighborhood would you like to use (m): ";
cin >> neighborhood;
}while(neighborhood != 'm');
system("CLS");
int i = 0;
do
{
srand(time(NULL));
for(int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
gen0[j][i] = rand() % 2;
}
if(i == 0)
copy(gen0, todo);
copy(todo, backup);
print(todo);
life(todo, neighborhood);
i++;
system("sleep .5");
if(i % 10 == 1 && i != 1)
{
cout << endl;
do
{
cout << "Would you like to continue this simulation? (y/n): ";
cin >> cont;
}while(cont != 'y' && cont != 'n');
if(cont == 'n')
break;
}
comparison = compare(todo, backup);
if(comparison == false)
system("CLS");
if(comparison == true)
cout << endl;
}while(comparison == false);
do
{
cout << "Would you like to run another simulation? (y/n): ";
cin >> again;
}while(again != 'y' && again != 'n');
}while(again == 'y');
return 0;
}
}
/*
* spielm.cpp
*
* Created on: 22.09.2016
* Author: fislam
*/
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
using namespace std;
void GetFile();
bool MakeArray();
char ChgArray();
//char GameBoard();
const int ROW1 =10;
const int COL1 =10;
const int BOARD_ROWS(10);
const int BOARD_COLS(10);
ifstream myfile;
string filename;
char live = 'X';
char dead = '.';
char board [BOARD_ROWS][BOARD_COLS];
int main()
{
int q;
//GetFile();
if ( MakeArray() ){
for ( int i(0); i <10; i++)
ChgArray();
}
else {
cout << "Error parsing input file" << endl;
}
cin >> q;
return 0;
}
void GetFile()
{
cout<<"Enter the filename: \n";
cin>>filename;
return;
}
bool MakeArray()
{
bool ret(false);
char val;
int totCnt = BOARD_ROWS*BOARD_COLS;
myfile.open (/*filename.c_str()*/"c_str.txt");
if ( myfile.is_open() ) {
for (int r=0; r<ROW1; r++)
{
for (int c=0; c<COL1; c++)
{
myfile>>val;
if ( val == dead || val == live ) {
board[r-1][c-1] = val;
totCnt--;
}
}
}
if ( !totCnt ) {
ret = true;
}
myfile.close();
}
return ret;
}
char getNextState(char b[BOARD_ROWS][BOARD_COLS], int r, int c)
{
char ret;
return ret;
}
char ChgArray()
{
char boardTmp[BOARD_ROWS][BOARD_COLS];
for (int r=0; r<BOARD_ROWS; r++)
{
for (int c=0; c<BOARD_COLS; c++)
{
boardTmp[r][c] = getNextState(board,r,c);
cout << boardTmp[r][c];
}
cout<<endl;
}
memcpy(board,boardTmp,sizeof(board));
cout << endl;
}
Instead of the previous I wrote new. and it worked
aftert main you are missing {
in first and second loop you have i loop twice
why do you need to copy ?
you can use this function fscanf() to read your file
e.g.
File= fopen(FileName, "r");
if( File == NULL ){
printf("Impossible to open file %s ! \n",FileName);
}
if( File != NULL)
{
char text[255];
while(!feof(File))
{
fscanf(File,"%s\n",&text);
....
}
}
Whenever I run this code and enter a number over 500,000, the program crashes. Also is there away to make this more simple/efficient without using: vectors, multiplication, division and %. Thanks a lot!
#include <iostream>
using namespace std;
int get_primes(int array[], int num);
void sieve(int array[], int num);
void goldbach(int primes[], int nprimes, int num);
void show(int array[], int num);
int main()
{
int num;
cout << "Enter a number to calculate up to." << endl;
cin>>num;
if ( num < 2 )
return 0;
int array[num];
array[0]= array[1]= 0;
for ( int i= 2; i < num; ++i )
array[i]= i;
int nprimes = get_primes(array, num);
show(array, nprimes);
goldbach(array, nprimes, num);
return 0;
}
void show(int array[], int num)
{
for (int i=0; i<num; i++)
if (array[i] > 0)
cout << array[i] << " "<< endl;
cout << endl;
}
int get_primes(int array[], int num)
{
sieve(array, num);
int pos = 0;
for (int i = 2; i < num; ++i)
if (array[i] > 0)
array[pos++] = array[i];
return pos;
}
void sieve( int array[], int num )
{
for ( int i= 0; i < num; ++i )
{
if ( array[i] > 0 )
{
for ( int j = i+i; j < num; j += i )
{
array[j] = 0;
}
}
}
}
void goldbach(int primes[], int nprimes, int num)
{
int a;
for (int a = 4; a<=num; a+=2)
{
bool found = false;
int i, j;
for (i = 0; !found && i < nprimes && primes[i]; ++i)
for (j = 0; !found && j < nprimes && primes[i]; ++j)
{
found = a == (primes[i] + primes[j]);
if (found)
cout << a << '\t' << primes[i] << " + " << primes[j] <<endl;
}
if (!found)
cout << a << "\tnot found" << endl;
}
}
Saying int array[num] is saying "allocate num * sizeof(int) bytes from the stack."
So it might be that your stack is only one megabyte in size, and when you ask for more you hit an underflow condition.
Change
int array[num];
to
int* array = new int[num];
and change all the function arguments from using
int array[]
to
int* array
you will still be able to access the pointers normally like array[i].
I would recommend you to look up stack vs heap memory in C++ to understand why this works, and to read up on pointers too.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
This sudoku solving program is getting compiled but getting error "segmentation fault " please help me in solving this problem and clarify why i am getting segmentation error as i have written every thing properlu .thanks in advance
#include<iostream>
#include<math.h>
class sudoku
{
public:
sudoku();
void initializeSudokuGrid();
void printSudokuGrid();
bool solveSudoku();
bool findEmptyGridSlot(int &row, int &col);
bool canPlaceNum(int row, int col, int num);
bool numAlreadyInRow(int row, int num);
bool numAlreadyInCol(int col, int num);
bool numAlreadyInBox(int smallGridRow, int smallGridCol, int num);
int grid[9][9];
};
sudoku::sudoku()
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
grid[i][j] = 0;
}
}
std::cout << "\n all the grid locations are initialise to zero";
}
void sudoku::initializeSudokuGrid()
{
char x = 'y';
while (x == 'y')
{
int row, col, var;
std::cout
<< "\n enter the row,column and integer in the box(that is 1-9 numbers \n";
std::cin >> row;
std::cin >> col;
std::cin >> var;
grid[row][col] = var;
std::cout
<< "\n are there any slots that u want to enter the numbers into the boxs enter y else enter n \n";
std::cin >> x;
}
}
void sudoku::printSudokuGrid()
{
std::cout << "\n";
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
std::cout << grid[i][j] << " ";
}
std::cout << "\n";
}
}
bool sudoku::solveSudoku()
{
int row, col;
if (findEmptyGridSlot(row, col))
{
for (int num = 1; num <= 9; num++)
{
if (canPlaceNum(row, col, num))
{
grid[row][col] = num;
if (solveSudoku()) //recursive call
return true;
grid[row][col] = 0;
}
}
return false; //backtrack
}
else
return true; //there are no empty slots
}
bool sudoku::numAlreadyInRow(int row, int num)
{
for (int i = 0; i < 9; i++)
{
if (num != 0 && grid[row][i] == num)
return true;
}
return false;
}
bool sudoku::numAlreadyInCol(int col, int num)
{
for (int i = 0; i < 9; i++)
{
if (num != 0 && grid[i][col] == num)
return true;
}
return false;
}
bool sudoku::canPlaceNum(int row, int col, int num)
{
if (!numAlreadyInRow(row, num))
{
if (!numAlreadyInCol(col, num))
{
int smallGridRow = row - row % 3;
int smallGridCol = col - col % 3;
if (!numAlreadyInBox(smallGridRow, smallGridCol, num))
{
return true;
}
}
}
return false;
}
bool sudoku::numAlreadyInBox(int smallGridRow, int smallGridCol, int num)
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (grid[i + smallGridRow][j + smallGridCol] == num)
return true;
}
}
return false;
}
bool sudoku::findEmptyGridSlot(int &row, int &col)
{
for (int row = 0; row < 9; row++)
{
for (int col = 0; col < 9; col++)
{
if (grid[row][col] == 0)
return true;
}
}
return false;
}
int main()
{
sudoku s;
s.printSudokuGrid();
s.initializeSudokuGrid();
s.printSudokuGrid();
std::cout << "\n after solving the problem \n";
if (s.solveSudoku())
s.printSudokuGrid();
else
std::cout << "\n solution doesnt exist for this type of solution \n";
return 0;
}
There's a mistake in your findEmptyGridSlot function
bool sudoku::findEmptyGridSlot(int &row, int &col)
{
for(int row=0;row<9;row++)
{
for(int col=0;col<9;col++)
{
...
You're declaring new int variables row and col in the loops, but you probably meant to use the ones passed as parameters.
Remove the int keyword to use the parameters instead of declaring new variables:
for(row=0;row<9;row++)
{
for(col=0;col<9;col++)
{
I'm writing some game at c++. The problem is with arrays. It always shows the 'desk' with points (empty parts). Need comments are in the code:
void showDesk(int someArray[][3])
{
for(int i = 0; i < 3 ;i++)
{
for (int j = 0; j < 3; j++)
{
if(someArray [i][j] == 0) cout << ".";
else if (someArray[i][j] == 1) cout << "x";
else if (someArray[i][j] == 2) cout << "o";
}
cout<<"\n";
}
}
void newDesk (int someArray[][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
someArray [i][j] = 0;
}
bool empty(int someArray[][3], int x, int y)
{
if (someArray[x][y] == 0)
return true; // It returns true
else
return false;
}
void setMark(int someArray[][3],int x,int y,int mark)
{
if (empty(someArray,x,y))
someArray [x][y] == mark; // But this never calls!
}
int main(int argc, char** argv) {
int x,y;
int mark;
int someArray [3][3];
newDesk(someArray);
showDesk(someArray);
cout << "------------------\n";
while (true)
{
cout<< "put x,y\n";
cin >> x>>y;
cout << "put mark\n";
cin >> mark;
setMark(someArray,x,y,mark);
showDesk(someArray);
}
return 0;
}
== is comparison, = is assignment.
Your line:
someArray [x][y] == mark;
Does nothing. It should be:
someArray [x][y] = mark;
someArray [x][y] == mark tests the array to see if its equal to mark, but never actually sets it to anything...