I made a sudoku solver, but need to be able to take inputs in to set the puzzle before being solved. The input will be in a 1,2,,,,3,4 format where a space between 2 commas indicates a blank space (and obviously this will all be done row by row). I also have my code set up to use blank spots as 0s and would like to know how to read the input, parse each number into an int and also parse blanks as 0s.
As stated above, this is for C++ and is in Visual Studio Express 2013.
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "cstdlib"
#include "stdio.h"
#include "iostream"
#include "sstream"
using namespace std;
//find the first empty slot on the puzzle
bool FindEmptyCell(int puzzle[9][9], int &row, int &colm);
//check if the inserted number is a legal move
bool isLegal(int puzzle[9][9], int row, int colm, int num);
//being solving through backtracking
bool solve(int puzzle[9][9])
{
int row, colm; //establish rows and columns
//check if there are any empty slots
if (!FindEmptyCell(puzzle, row, colm))
{
return true; //puzzle is assumed solved
}
else
{
//start backtracking with the number 1
for (int i = 1; i<10; i++)
{
if (isLegal(puzzle, row, colm, i))
{
puzzle[row][colm] = i;
if (solve(puzzle) == true)
{
return true; //if there is no problem with the first number, move on
}
else
{
puzzle[row][colm] = 0;
}
}
}
}
return false; //start recursion to try next number
}
//check if the move is legal in the 3x3 square, needs the row and column of the square
bool CheckSquare(int puzzle[9][9], int sqRow, int sqColm, int chkNum)
{
for (int row = 0; row < 3; row++)
{
for (int colm = 0; colm < 3; colm++)
{
if (puzzle[row + sqRow][colm + sqColm] == chkNum)
{
return true; //the number is there and the move is illegal
}
}
}
return false; //the number is not there and the move is assumed legal
}
//check if the move is legal in the row
bool CheckRow(int puzzle[9][9], int row, int chkNum)
{
for (int colm = 0; colm <9; colm++)
{
if (puzzle[row][colm] == chkNum)
{
return true; //the number is there and the move is illegal
}
}
return false; // the number is not there and the move is assumed legal
}
//check if the move is legal in the column
bool CheckColm(int puzzle[9][9], int colm, int chkNum)
{
for (int row = 0; row <9; row++)
{
if (puzzle[row][colm] == chkNum)
{
return true; //the number is there and the move is illegal
}
}
return false; // the number is not there and the move is assumed legal
}
//definition of finding empty slot method
bool FindEmptyCell(int puzzle[9][9], int &row, int &colm)
{
for (colm = 0; colm < 9; colm++)
{
for (row = 0; row < 9; row++)
{
if (puzzle[row][colm] == 0)
{
return true;
}
}
}
return false;
}
//definition of is legal method
bool isLegal(int p[9][9], int r, int c, int cN)
{
if (CheckRow(p, r, cN) == false)
{
if (CheckColm(p, c, cN) == false)
{
if (CheckSquare(p, r - r % 3, c - c % 3, cN) == false) //use % to find the beginning row/column of the square
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
int main()
{
int puzzle[9][9];
string input;
for (int i=1; i <10; i++)
{
for (int j=1;j <10; j++)
{
cout << "Please insert the number for ["<< i << "," << j << "] (0 for no number)" << endl;
getline(cin, input);
int value = atoi(input.c_str());
puzzle[i-1][j-1] = value;
//get puzzle into correct format
}
}
if (solve(puzzle) == true)
{
string s;
cout << "Solving the puzzle..." << endl;
//print the puzzle to the screen
for (int i = 0; i<9;i++)
{
for (int j = 0; j<9; j++)
{
cout << puzzle[i][j] << ",";
}
cout << endl;
}
cout << "Press any button to escape";
getline(cin, s);
}
else
{
cout << "Can not solve the puzzle" << endl;
}
return 0;
}
Not sure why you need the code but here it is. Everything works properly, it just doesnt take input in the correct format currently.
As your numbers are only of 1 digit, you can:
int puzzle[9][9] = {};
for (int i = 1; i < 10; ++i)
{
cout << "Please input row #" << i << ": ";
getline(cin, input);
int j = 0;
const char *ptr = input.c_str();
while (*ptr)
{
if (isspace(*ptr)) ; // do nothing
else if (*ptr == ',') ++j; // new column
else puzzle[i - 1][j] = *ptr - '0';
++ptr;
}
}
To parse empty spaces as zerors, you need:
Initialize your matrix to zeros int puzzle[9][9] = { };
read the input line by line (each line representing a whole row)
use function strtok() to split (separate) the values using delimeter `','
for each separated value, trim it (i.e. remove spaces). Then, Use function atoi to covert it as an integer
Notice that you should check for empty separated string if (strlen(str))
Related
I'm trying to implement a program which is able to compute a matrix-product.
GOAL: Input v or m and the program automatically computes dot-product, matrix-vector product or matrix product.
The program is working for vectors, and quadratic-matrices but for non-quadratic-matrices the program crashes with.
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 1) >= this->size() (which is 1)
I'm not sure if the function matrix_input() really is the issue or if my way to enter 2D-Vektors is wrong.
When I input m 2 1 1 2 m 1 2 2 1 the program crashes in the function matrix_input() because it outputs error-2, error-3 not anymore.
I have noticed that the program crashes because in the second matrix the last row doesn't exist or something similar.
#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
//Checks if input is a matrix or vector
//PRE: Char v or m
//POST: Returns number of the char 1 or 2 or 0 as error
int m_or_v(char m_v) {
if(m_v=='v')
return 1;
else if(m_v=='m')
return 2;
assert("error");
return 0;
}
//Inputs a vector if input was 'v'
//PRE: Takes vector
//POST: Checks length of the vector, inputs values into the vector
void vector_input(vector<int> &vector_v) {
int row = 0;
cin >> row;
int cache = 0;
for(int i = 0;i<row;i++) {
cin >> cache;
vector_v.push_back(cache);
}
}
//Inputs a matrix if input was 'm'
//PRE: Takes matrix
//POST: Checks number row, col and inputs the values into the matrix
void matrix_input(vector<vector<int>> &matrix_m) {
int row = 0;
int col = 0;
int cache_m = 0;
cin >> row;
cin >> col;
for(int r = 0;r<row;r++) {
vector<int> empty = {};
cout << "error-1";
for(int c = 0;c<col;c++) {
cin >> cache_m;
empty.push_back(cache_m);
cout << "error-2";
}
//I believe the program crashes in at this point, because the last row doesn't exist.
matrix_m.push_back(empty);
}
cout << "error-3";
}
//Computes the dot product if the input were two vectors
void scalar_product(const vector<int> &vector_1,const vector<int> &vector_2) {
int value = 0;
if(vector_1.size()==vector_2.size()) {
for(unsigned long i = 0;i<vector_1.size();i++) {
value += (vector_1.at(i)*vector_2.at(i));
}
cout << "s" << endl;
cout << value;
}
else
cout << "error";
}
//Checks if dot product, matrix-vector product or matrix product
int mult_mode(int mode1, int mode2) {
if(mode1==1&&mode2==2) {
cout << "error";
return 0;
}
else if(mode1==1&&mode2==1)
return 1;
else if((mode1==2&&mode2==1)||(mode1==1&&mode2==2))
return 2;
else if(mode1==2&&mode2==2)
return 3;
else {
cout << "error";
return 0;
}
}
//Check if Matrix-Vektor Product is possible
bool mvp_valid(const vector<vector<int>> &matrix_1,
const vector<int> &vector_2) {
if(matrix_1.at(0).size()==vector_2.size())
return true;
return false;
}
//Computes Vector-Matrix product
void mvp_result(const vector<vector<int>> &matrix_1,
const vector<int> &vector_2, vector<int> &result) {
int cache = 0;
for(unsigned long row = 0;row<matrix_1.size();row++) {
for(unsigned long col = 0;col<vector_2.size();col++) {
cache += matrix_1.at(row).at(col)*vector_2.at(col);
}
result.push_back(cache);
cache = 0;
}
}
//Outputs a vector
void output_v(vector<int> &vec) {
cout << "v " << vec.size() << endl;
for(unsigned long i = 0;i<vec.size();i++) {
cout << vec.at(i) << " ";
}
}
//Check if Matrix x Matrix is valid
bool mxm_valid(
const vector<vector<int>> &matrix_1,
const vector<vector<int>> &matrix_2) {
if(matrix_1.at(0).size()==matrix_2.size())
return true;
return false;
}
//Computes the matrix-matrix product
void mxm_result(
const vector<vector<int>> &matrix_1,
const vector<vector<int>> &matrix_2,
vector<vector<int>> &result_m) {
int cache = 0;
vector<int> empty;
for(unsigned long row = 0;row<matrix_1.size();row++) {
result_m.push_back(empty);
for(unsigned long col = 0;col<matrix_2.at(row).size();col++) {
for(unsigned long row_col = 0;row_col<matrix_2.size();row_col++) {
cache+=matrix_1.at(row).at(row_col)*matrix_2.at(row_col).at(col);
}
result_m.at(row).push_back(cache);
cache = 0;
}
}
}
//Outputs a matrix
void output_m(const vector<vector<int>> &matx) {
for(unsigned long col = 0;col<matx.size();col++) {
for(unsigned long row = 0;row<matx.size();row++) {
cout << matx.at(col).at(row) << " ";
}
cout << endl;
}
}
//Outputs the size of the matrix
void size_m(const vector<vector<int>> &matx) {
cout<<"m "<<matx.size()<<" "<<matx.at(0).size()<<endl;
}
int main() {
char mode_c = ' ';
int mode1 = 0;
int mode2 = 0;
vector<int> vector_1;
vector<vector<int>> matrix_1;
vector<int> vector_2;
vector<vector<int>> matrix_2;
//Vector or Matrix Number 1
cin >> mode_c;
mode1 = m_or_v(mode_c);
if(mode1==1) {
vector_input(vector_1);
}
else if(mode1==2) {
matrix_input(matrix_1);
}
//Vector or Matrix Number 2
cin >> mode_c;
mode2 = m_or_v(mode_c);
if(mode2==1) {
vector_input(vector_2);
}
else if(mode2==2) {
cout << "error-10";
matrix_input(matrix_2);
cout << "error-11";
}
switch(mult_mode(mode1, mode2)) {
case 1:
scalar_product(vector_1,vector_2);
break;
case 2:
if(mvp_valid(matrix_1, vector_2)==true) {
vector<int> result;
mvp_result(matrix_1, vector_2, result);
output_v(result);
}
else
cout << "error";
break;
case 3:
if(mxm_valid(matrix_1, matrix_2)==true) {
vector<vector<int>> result_m;
mxm_result(matrix_1, matrix_2, result_m);
size_m(result_m);
output_m(result_m);
}
else
cout << "error";
break;
}
}
I am trying to implement the algorithm RLE with simple input like:
ddddddddddhhhhhhhhhhhhhhhttttttttttttt
code:
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int main() {
vector<char> read;
ifstream file;
file.open("file.txt");
if (!file) {
cout << "Unable to open";
}
char v;
while(file>>v) {
read.push_back(v);
}
char x;
int count=0;
for(int i=0; i<read.size(); i++) {
x = read[i];
if(x != read[++i]) {
cout << x << "1";
}
while(x == read[++i]) {
count++;
}
cout << x << count;
count = 0;
}
return 0;
}
The output I am getting is:
d9d1h12h1t10t1
Please help me with the code.
Update: I have updated the question as I have realized few things.
Plus: This code produced no output, is there anything wrong which I am doing wrong?
char o;
char n;
int count=0;
for(int i=0; i<read.size(); i++) {
o = read[i];
n = read[++i];
while(o == n) {
count++;
}
cout << o << count;
if(o != n) {
cout << o << "1";
} count = 0;
}
return 0;
This loop:
char x;
int count=0;
for(int i=0; i<read.size(); i++) {
int j=i;
x = read[i];
if(x != read[++j]) {
cout << x << "1";
}
while(x == read[++j]) {
count++;
}
cout << x << count;
}
Has several errors. First, you should use two indices, i and j. i is going through each element of read, but then j is iterating through a subsequence too. What you want is to go through each element only once, and in each case either print or increase the count. However having a for loop and moving the index inside too is not a very good practice, is rather error-prone. Also you have to cout statements that are do not run at the right time (you don't wan to print something on every iteration, only when the character changes). You could do it with a while loop, or using a simpler structure like:
// If there are no characters finish
if (read.empty()) {
return 0;
}
// Get the first character
char lastChar = read[0];
int count = 1; // We have counted one character for now
// Go through each character (note start from 1 instead of 0)
for(int i = 1; i < read.size(); i++) {
// Get the next char
char newChar = read[i];
// If it is different, print the current count and reset the counter
if (lastChar != newChar) {
cout << lastChar << count;
count = 1;
lastChar = newChar;
} else { // Else increase the counter
count++;
}
}
// Print the last one
cout << lastChar << count;
return 0;
I'm coding a recursive algorithm to take a user input N and make a N x N grid where the same number does not appear twice on either a row or a column. Almost everything's working, and duplicates don't appear in columns, but I'm having trouble getting rows working.
My code for checking duplicates in rows is the function noRowDuplicates. Duplicates are still appearing, and occasionally it'll throw a segmentation fault, but I'm not sure why.
Thanks in advance for the help!
// Author: Eric Benjamin
// This problem was solved using recursion. fill() is the recursive function.
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void fillOptions();
void fill(int arrayPosition);
int inputNum;
int gridSize;
int *grid;
int allOptionsSize = 0;
int *allOptions;
int main() {
cout << "Please enter a number!" << endl;
cin >> inputNum;
gridSize = inputNum * inputNum;
grid = new int[gridSize];
allOptions = new int[inputNum];
for (int i = 0; i < inputNum; i++) {
allOptions[i] = i + 1;
allOptionsSize++;
}
srand((unsigned)time(0));
fill(0);
delete[] grid;
delete[] allOptions;
return 0;
}
bool noColumnDuplicates(int arrPosition, int valueToCheck) {
for (int i = 1; i < inputNum; i++) {
if (arrPosition - (inputNum * i) >= 0) {
if (grid[arrPosition - (inputNum * i)] == valueToCheck) {
return false;
}
}
}
return true;
}
bool noRowDuplicates(int arrPosition, int valueToCheck) {
int rowPosition = arrPosition % inputNum; // 0 to num - 1
if (rowPosition > 0) {
for (int p = 1; p < rowPosition; p++) {
if (grid[arrPosition - p] == valueToCheck) {
return false;
}
}
}
return true;
}
void fill(int arrayPosition) {
if (arrayPosition < gridSize) {
int randomPosition = rand() % allOptionsSize;
grid[arrayPosition] = allOptions[randomPosition];
if (noColumnDuplicates(arrayPosition, grid[arrayPosition])) {
if (noRowDuplicates(arrayPosition, grid[arrayPosition])) {
if (arrayPosition % inputNum == 0) {
cout << endl;
}
cout << grid[arrayPosition] << " ";
fill(arrayPosition + 1);
} else {
fill (arrayPosition);
}
} else {
fill(arrayPosition);
}
}
}
noRowDuplicates never tests the first element of a row, which makes sense when you are trying to fill the first element of a row, but not any other time.
I'm new to C++ and I'm trying to figure out why I get two "*" symbols in my game board when I'm moving around. The game is supposed to be about avoiding the troll (#). But I am getting duplicate # and * symbols, and I can't figure out why. It seems that the problem is either in one of the for loops or in the posX or posY variables, which I found out by commenting out segments of the code:
#include <iostream>
#include <string>
using namespace std;
void ClearScreen()
{
cout << string(100, '\n');
}
main()
{
int size_arrx = 10;
int size_arry = 20;
int posX = 0;
int posY = 0;
int trollX = size_arrx - 1;
int trollY = size_arry - 1;
char a[size_arry][size_arrx];
bool Alive = true;
char player = '*';
char troll = '#';
while (Alive == true) {
ClearScreen();
for (int i = 0; i<size_arrx; i++)
{
for (int j = 0; j<size_arry; j++)
{
a[i][j] = 'x';
}
}
for (int i = 0; i<size_arrx; i++)
{
for (int j = 0; j<size_arry; j++)
{
a[posX][posY] = player;
a[trollX][trollY] = troll;
cout << a[i][j];
if (posX< 0) {
a[posX = 0][posY] = player;
cout << a[i][j];
}
else if (posY< 0) {
a[posX][posY = 0] = player;
cout << a[i][j];
}
else if (posY > size_arry - 1) {
a[posX][posY = size_arry - 1] = player;
cout << a[i][j];
}
else if (posX > size_arrx - 1) {
a[posX = size_arrx - 1][posY] = player;
cout << a[i][j];
}
}
cout << endl;
}
char dir;
cin >> dir;
if (dir == 'w') {
trollX++;
posX--;
}
if (dir == 's') {
trollX--;
posX++;
}
if (dir == 'd') {
trollY--;
posY++;
}
if (dir == 'a') {
trollY++;
posY--;
}
}
if ((trollX == posX) && (trollY == posY)) {
Alive == false;
}
}
The result looks like this. I only want one *. The * can move perfectly fine, but a duplicate * follows the original * but 11 X's away.
xxxxxxxxxx*xxxxxxxxx <---- This is a duplicate *
*xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx#
xxxxxxxxx#xxxxxxxxxx <---- This is a duplicate #
Thanks in advance if you can help me
for (int i=0;i<size_arrx;i++){
for (int j=0;j<size_arry;j++){
a[i][j]='x';
}
}
a[posX][posY]=player;
a[trollX][trollY]=troll;
for (int i=0;i<size_arrx;i++){
for (int j=0;j<size_arry;j++){
cout << a[i][j];
Using this code gave the same error. I'm interpreting this as a[i][j]='x' populates all positions of a[][] with X's. a[posX][posY]=player; overwrites the position of the player with an * (could be x 2 y 5 for example) and then the board gets printed by cout << a[i][j];. I don't understand how a duplicate symbol gets thrown in there.
Let's simplify your program.
Initialize the board outside of the while loop.
There should be no reason to keep initializing it:
for (unsigned int row = 0; row < size_arry; ++row)
{
std::fill(&a[row][0], &a[row][size_arrx], 'x'); // Fill a row.
}
Printing the board should be simple:
for (unsigned int row = 0; row < size_arry; ++row)
{
for (unsigned int column = 0; column < size_arrx; ++column)
{
cout << a[row][column];
}
cout << '\n';
}
Now the character logic.
Every character has a position, row and column, of where it is. To ease restoration, every character should have a previous position also.
struct Position
{
unsigned int row;
unsigned int column;
};
Sorry about that code, the fingers and keyboard are not cooperating.
To move a character to a valid new position, you have to restore the previous position:
unsigned int player_now_x;
unsigned int player_now_y;
unsigned int player_prev_x;
unsigned int player_prev_y;
//...
a[player_prev_y][player_prev_x] = 'x';
a[player_now_y][player_now_y] = player;
For processing single letter commands, a switch statement may be more readable:
// Update previous position.
player_prev_x = player_now_x;
player_prev_y = player_now_y;
switch (dir)
{
case 'd':
if (player_now_y < size_arry)
{
++player_now_y;
}
break;
case 's':
if (player_now_x < size_arrx)
{
++player_now_x;
}
break;
// ...
}
Simplifications.
You can print the board with one cout if you add an extra column. The ending column of each row (except the last) will have a line ending character, '\n'. The last column of the last row will have a termination character, '\0'.
struct Board
{
void set_player(const Position& pos, char player_token)
{
a[pos.x][pos.y] = player_token;
}
void move_player(const Position& new_position,
const Position& previous_position,
char player_token)
{
set_player(previous_position, 'x');
set_player(new_position, player_token);
}
void print()
{
std::cout << &a[0][0] << "\n";
}
Board()
{
for (unsigned int y = 0; y < size_arry; ++y)
{
std::fill(&a[y][0], &a[y][size_arrx], 'x');
a[y][size_arrx - 1] = '\n';
}
a[size_arry - 1][size_arrx - 1] = '\0';
}
};
//...
Board b;
Position player_now;
Position player_prev;
const char player_token = '*';
//...
switch (dir)
{
case 'd':
if (player_now.y < size_arry)
{
++player_now.y;
}
//...
}
b.move_player(player_now, player_previous, player_token);
Sorry again, for the above code fragment, it's the fingers typing out what they want.
So I am trying to create this program to check the sort of a list of words to see whether they are in ascending, or descending order. I am copying the words from a file to an array of strings. I am told the regular comparison operators function the same with strings as they do with ints. However, when I run the program, it always outputs that the list is unordered (even when it is). I would greatly appreciate any help one could offer me. Thank you!
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int checkArraySort(int array_max, string arr[]);
int main(void)
{
const int array_max = 20;
string arr[array_max];
int d;
ifstream myfile_in;
myfile_in.open ("words_in.txt");
string line;
for(int i = 0; i < array_max; i++)
{
getline(myfile_in, line);
}
d = checkArraySort(array_max, arr);
if(d == -1)
{
cout << "The array is sorted in descending order!" << endl;
}
if(d == 0)
{
cout << "The array is not sorted!" << endl;
}
if(d == 1)
{
cout << "The array is sorted in ascending order!" << endl;
}
myfile_in.close();
return 0;
}
int checkArraySort(int array_max, string arr[])
{
bool y = false;
int j = 0;
for(int i = 0; i < array_max; i++)
{
if(arr[i] < arr[i-1])
{
j++;
}
if(j == (array_max))
{
y = true;
return -1;
}
}
j = 0;
for(int i = 0; i < array_max; i++)
{
if(arr[i] > arr[i-1])
{
j++;
}
if(j == (array_max))
{
y = true;
return 1;
}
}
if(y = false)
{
return 0;
}
}
if(y = false)
should be
if(y == false)