I am new to C++ and few days ago I chose Knight's Tour to practice backtracking. This program runs well when the knight doesn't have to visit each tiles, they can even miss 1 or 2 tiles and the program still works. But when it comes to the point that I have to cover the whole board, the program seems to enter infinite loop. (Yes I did tried to debug it but I still didn't figure it out)
Note: the starting position of the Knight is always [1; 1]
#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <iomanip>
using namespace std;
int n, moves[100][100];
void Import()
{
ifstream f;
f.open("Knight's Tour.inp");
f >> n;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) moves[i][j] = -1;
f.close();
}
bool If_Knight_Moves_Possible(int x, int y)
{
return (x >= 1 && y >= 1 && x <= n && y <= n && moves[x][y] == -1);
}
bool All_Knight_Conditions_Met(int x, int y, int ways_of_x[], int ways_of_y[], int moves_count)
{
if (moves_count > (n * n)) return(1);
// If you input n = 7, you minus (n * n) by 1 and the code works, for n = 8, you minus it by 2
for (int i = 1; i <= n; i++)
{
long int next_x = x + ways_of_x[i], next_y = y + ways_of_y[i];
if (If_Knight_Moves_Possible(next_x, next_y))
{
moves[next_x][next_y] = moves_count;
if (All_Knight_Conditions_Met(next_x, next_y, ways_of_x, ways_of_y, moves_count + 1)) return(1);
else moves[next_x][next_y] = -1;
}
}
return(0);
}
void Analysis()
{
}
void Export()
{
ofstream f("Knight's Tour.out");
int ways_of_x[8] = { -1, -2, -2, -1, 1, 2, 2, 1 };
int ways_of_y[8] = { -2, -1, 1, 2, 2, 1, -1, -2 };
moves[1][1] = 1;
if (All_Knight_Conditions_Met(1, 1, ways_of_x, ways_of_y, 2))
{
f << "There is a possible solution:" << endl;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
f << setw(2) << moves[i][j] << " ";
f << endl;
}
}
else f << "There is no possible solution";
f.close();
}
int main()
{
Import();
Analysis();
Export();
}
In one of the largest cities in Bytland, Bytesburg, construction of the second stage of the metro is underway. During the construction of the first stage, N stations were built that were not interconnected. According to the master plan, the metro in Bytesburg should consist of no more than two lines. each metro line is straight. The president of the company responsible for laying the lines wants to make sure that no more than two metro lines can be laid, so that all stations built lie on at least one of the two
Exaple 1
input:
6
0 1
1 1
2 1
0 2
1 3
2 2
output:
no
Example 2
input:
6
2 2
4 6
1 0
2 1
6 1
1 1
output:
yes
I wrote the code, but on the seventh test on the testing system, it gives the wrong answer. Help me please. This is my code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct line {
int k, b;
bool x = false;
long long c = 0;
};
int n;
vector<line> lines;
vector<pair<int, int>> p;
bool Is3PointsOnLine(pair<float, float> p1, pair<float, float> p2, pair<float, float> p3) {
return ((p3.first - p1.first)*(p2.second - p1.second) == (p3.second - p1.second)*(p2.first - p1.first));
}
bool PointIsOnLine(line l, int x, int y) {
if (!l.x) {
if (y == ((l.k * x) + l.b))
return true;
}
else {
if (x == l.b)
return true;
}
return false;
}
line f(pair<int, int> c1, pair<int, int> c2) {
int x1 = c1.first;
int y1 = c1.second;
int x2 = c2.first;
int y2 = c2.second;
line ans;
if (x2 != x1) {
ans.k = (y2 - y1) / (x2 - x1);//fix
ans.b = -(x1 * y2 - x1 * y1 - x2 * y1 + x1 * y1) / (x2 - x1);
ans.x = false;
ans.c = 0;
}
else {
ans.k = 0;
ans.b = x1;
ans.x = true;
ans.c = 0;
}
return ans;
}
int a() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != i) {
for (int k = 0; k < n; k++) {
if ((k != i) && (k != j) && (Is3PointsOnLine(p[i], p[j], p[k]))) {
lines.push_back(f(p[i], p[j]));
return 0;
}
}
}
}
}
}
int main() {
cin >> n;
p.resize(n);
vector<int> not_used;
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
if (n < 5) {
cout << "yes";
return 0;
}
else {
a();
if (lines.size() == 0) {
cout << "no";
return 0;
}
pair<int, int> e = { -8,-8 };
for (int i = 0; i < n; i++) {
if (PointIsOnLine(lines[0], p[i].first, p[i].second)) {
lines[0].c++;
}
else if (e.first == -8) {
e.first = i;
}
else if (e.second == -8) {
e.second = i;
lines.push_back(f(p[e.first], p[e.second]));
for (int j = 0; j <= i; j++) {
if (PointIsOnLine(lines[1], p[j].first, p[j].second)) {
lines[1].c++;
}
}
e = { -5,-5 };
}
else if (PointIsOnLine(lines[1], p[i].first, p[i].second)) {
lines[1].c++;
}
else {
cout << "no";
return 0;
}
}
if (lines[0].c+1 >= n && e.first != -2 && e.second == -8) {
cout << "yes";
return 0;
}
else if (lines[0].c + lines[1].c >= n) {
cout << "yes";
return 0;
}
else {
cout << "no";
return 0;
}
}
return 0;
}
Code v2
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct line {
pair<int, int> p1, p2;
long long c = 0;
};
int n;
vector<line> lines;
vector<pair<int, int>> p;
bool Is3PointsOnLine(pair<float, float> p1, pair<float, float> p2, pair<float, float> p3) {
return ((p3.first - p1.first)*(p2.second - p1.second) == (p3.second - p1.second)*(p2.first - p1.first));
}
//лежит ли точка на прямой
bool PointIsOnLine(line l, int x, int y) {
return Is3PointsOnLine(l.p1, l.p2, {x , y});
}
int a() {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != i) {
for (int k = 0; k < n; k++) {
if ((k != i) && (k != j) && (Is3PointsOnLine(p[i], p[j], p[k]))) {
line sdafsadf;
sdafsadf.p1 = p[i]; sdafsadf.p2 = p[j];
lines.push_back(sdafsadf);
return 0;
}
}
}
}
}
}
int main() {
cin >> n;
p.resize(n);
vector<int> not_used;
for (int i = 0; i < n; i++)
cin >> p[i].first >> p[i].second;
if (n < 5) {
cout << "yes";
return 0;
}
else {
//ищем первую прямую
a();
if (lines.size() == 0) {
cout << "no";
return 0;
}
pair<int, int> e = { -8,-8 };
for (int i = 0; i < n; i++) {
if (PointIsOnLine(lines[0], p[i].first, p[i].second)) {
lines[0].c++;
}
else if (e.first == -8) {
e.first = i;
}
else if (e.second == -8) {
e.second = i;
line sdafsadf;
sdafsadf.p1 = p[e.first]; sdafsadf.p2 = p[e.second];
lines.push_back(sdafsadf);
for (int j = 0; j <= i; j++) {
if (PointIsOnLine(lines[1], p[j].first, p[j].second)) {
lines[1].c++;
}
}
e = { -5,-5 };
}
else if (PointIsOnLine(lines[1], p[i].first, p[i].second)) {
lines[1].c++;
}
else {
cout << "no";
return 0;
}
}
if (lines[0].c+1 >= n && e.first != -2 && e.second == -8) {
cout << "yes";
return 0;
}
else if ((lines[0].c + lines[1].c >= n) && (lines[0].p1 != lines[1].p1) && (lines[0].p1 != lines[1].p2) && (lines[0].p2 != lines[1].p1) && (lines[0].p2 != lines[1].p2)) {
cout << "yes";
return 0;
}
else {
cout << "no";
return 0;
}
}
return 0;
}
Your function f is horribly broken because it uses integer division. Then everything after that which relies on k and b, including PointIsOnLine, will give wrong results. Is3PointsOnLine seems ok, change your line struct to store two points, not slope+intercept, and then PointInOnLine can just call Is3PointsOnLine.
I'm amazed that you passed any non-trivial test cases with such a bug.
This also will not work
if (lines[0].c + lines[1].c >= n)
because you aren't testing that lines[1] isn't a duplicate of lines[0].
Also, you can reach lines[0].c + lines[1].c == n and still miss one station completely, if another station lies at the intersection of the lines and gets double-counted.
so I tried to recreate Conways Game of Life and i got it pretty much working but i have numbers in my output and i have no idea where they are coming from.
Im fairly new to c++and programming in general i learned python and java before but i have never seen this and google didnt seem to understand my question so i hope you fellow humans do.
My output looks like this:
1664447571170186994045474010000255652800 /its about this number
0000000000
0011000000
0110000000
0001000000
0000000000
0000000000
0000000000
0000000000
0000000000
anyone has any idea where they come from cause i certainly don't.
sometimes these numbers even "collide" with the board like this:
0000-1-1-1-116644475711701869940
4547401010000000
791621423110000000
0101000000
0000000000
0000000000
0000000000
0000000000
0000000000
0020000000
My sphagetthi code:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
int main(){
int x = 10, y = 10, i, j, c, b;
int field[y][x] =
{{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,1,1,0,0,0,0},
{ 0,0,0,0,1,0,1,0,0,0},
{ 0,0,0,0,1,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0}};
int updateField[y][x];
cout << "start";
for(b = 0;b<=10; b++){
cout << "main loop: "<<b<<"\n";
for(y = 0; y < 10; y++)
{
for(x = 0; x < 10; x++)
{
c = 0;
if(y == 0 || y == 9 || x == 0 || x == 9){
}
else{
/*v v v lebende zelle v v v*/
if(field[y][x] == 1){
if(field[y-1][x-1] == 1){
c++;
}
if(field[y-1][x] == 1){
c++;
}
if(field[y-1][x+1] == 1){
c++;
}
if(field[y][x-1] == 1){
c++;
}
if(field[y+1][x-1] == 1){
c++;
}
if(field[y+1][x] == 1){
c++;
}
if(field[y+1][x+1] == 1){
c++;
}
if(field[y][x+1] == 1){
c++;
}
/*regeln v v v*/
if(c < 2 or c > 3){
updateField[y][x] = 0;
}
else
{
updateField[y][x] = 1;
}
}
/*^ ^ ^ lebende zelle ^ ^ ^*/
/*v v v tote zelle v v v*/
else if(field[y][x] == 0){
if(field[y-1][x-1] == 1){
c++;
}
if(field[y-1][x] == 1){
c++;
}
if(field[y-1][x+1] == 1){
c++;
}
if(field[y][x-1] == 1){
c++;
}
if(field[y+1][x-1] == 1){
c++;
}
if(field[y+1][x] == 1){
c++;
}
if(field[y+1][x+1] == 1){
c++;
}
if(field[y][x+1] == 1){
c++;
}
/*regeln v v v*/
if(c == 3)
{
updateField[y][x] = 1;
}
else
{
updateField[y][x] = 0;
}
}
}
}
}
cout << "\n";
for(i = 0; i < 10; i++){
for(j = 0; j < 10; j++){
field[i][j] = updateField[i][j];
}
}
for(i = 0;i < x; i++){
for(j = 0; j < y; j++ )
{
cout << field[i][j];
}
cout << "\n";
}
sleep_for(nanoseconds(500000000));
}
}
I'd appreciate any answer. thanks in advance.
The problem is that you ignore the borders when building updateField (0 or 9 in a component) but then copy updateField including those borders into field. The borders in updateField were never set, so they just contain any number which was in the memory before which is not controlled by your program.
You should either set the borders of updateField to zero or do not copy the borders into field iterating from 1 to excluding 9 instead of from 0 to 10.
While I hope that this solves your problem I would also like to share some ideas how to refactor your code. For instance you could use arrays of boolean values instead of integers as the values clearly can only be 0 or 1 according to the games logic. Scanning the neighborhood of a cell also adds a lot of redundant code which can be made shorter. Just see the following as a general idea about some improvements which can be done:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
using namespace std::this_thread;
using namespace std::chrono;
int main(){
int x = 10, y = 10, i, j, c, b;
bool field[y][x] =
{{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,1,1,0,0,0,0},
{ 0,0,0,0,1,0,1,0,0,0},
{ 0,0,0,0,1,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0},
{ 0,0,0,0,0,0,0,0,0,0}};//too lazy to rewrite to true/false, should also work this way
bool updateField[y][x];
cout << "start";
for(b = 0;b<=10; b++){
cout << "main loop: "<<b<<"\n";
for(y = 0; y < 10; y++)
{
for(x = 0; x < 10; x++)
{
c = 0;
if(y == 0 || y == 9 || x == 0 || x == 9){
updateField[y][x] = false;
}
else{
for(i=-1;i<2;i++){
for(j=-1;j<2;j++){
if((i != 0 || j != 0) && field[y+i][x+j] == 1){// exclude i == j == 0
c++;
}
}
}
if(field[y][x]){
if(c < 2 or c > 3){
updateField[y][x] = false;
}
else
{
updateField[y][x] = true;
}
}else{
if(c == 3)
{
updateField[y][x] = true;
}
else
{
updateField[y][x] = false;
}
}
}
}
}
cout << "\n";
for(i = 0; i < 10; i++){
for(j = 0; j < 10; j++){
field[i][j] = updateField[i][j];
}
}
for(i = 0;i < x; i++){
for(j = 0; j < y; j++ )
{
cout << field[i][j];
}
cout << "\n";
}
sleep_for(nanoseconds(500000000));
}
}
If you initialize updateField as well, the weird numbers will be gone.
Because of this if statement, the edge of updatefield doesn't update:
if (y == 0 || y == 9 || x == 0 || x == 9) ;
else {
///long statements
if (c < 2 || c > 3) {
updateField[y][x] = 0;
}
else
{
updateField[y][x] = 1;
}
}
Also, you should be more consistent with your usage of variables, for example you define your map to be x*y sized, but later in the for loops you specify the maximum value of x and y to be 10.
What you're seeing is the contents of uninitialized memory. When you declare int updateField[y][x]; the compiler reserves an appropriate space of memory for you, but it doesn't initialize the memory for you, so it contains whatever random junk was already there.
To fix this, initialize updateField to be identical to field before running the loop:
for (int i = 0; i < y; ++i) {
for (int j = 0; j < x; ++j) {
updateField[i][j] = field[i][j];
}
}
I was writing a program to print 70 prime numbers (7 per row).
I was required to define and use two functions isprime() and printprime.
the program builds and runs but it doesn't print the prime numbers.
Can anyone tell me what's wrong with this code that why I am unable to print the prime numbers?
edit: corrected some mistakes in the for loops and isprime()
#include "pch.h"
#include <iostream>
using namespace std;
bool isprime(int num)
{
bool f = true;
int c,h = 0;
for (int j = 1; j <= num; j++)
{
c = num % j;
if (c == 0)
{
h++;
if (h > 2)
{
f = false;
break;
}
}
}
return f;
}
void printprime(int x, int y)
{
bool f = false;
int s = 0, h = 0, j = 0, num = 2;
num = 1; x = 70; y = 7;
for (int d = 0; d < x; d+=7)
{
s = 0;
cout << " " << endl;
for (;s < 7;s++)
{
f = isprime(num);
if (f == true)
{
cout <<" "<<num;
num++;
}
else if (f == false)
{
num++;
s--;
}
}
}
}
int main()
{
int x = 70, d = 0, h = 0, j = 0, y = 7, num = 1;
num = 1; y = 7;
printprime(x, y);
return 0;
}
There are couple of issues,
bool isprime(int num) is not returning the computed value f instead it returning true in all the case
if number of deviser are more than 2 you can break the loop, instead computing till the end.
printprime is not collecting return value from isprime
Code snippet as follows,
bool isprime(int num)
{
bool f = true;
int c,h = 0;
for (int j = 1; j <= num; j++)
{
c = num % j;
if (c == 0)
{
h++;
if (h > 2) //Check for more than 2 since every prime has min 2 devisers 1 and number iteslf
{
f = false;
break;
}
}
}
return f;//return the computed value
}
void printprime(int x, int y)
{
bool f = false;
int s = 0, num = 2;
//Note Num started from 2 as 1 is not a prime
for (int d = 0;d < x; d+=7)
{
s = 0;
while (s < 7)
{
f = isprime(num);
if (f == true)
{
cout << num <<" ";
s++;
}
num++;
}
cout << endl;
}
}
int main()
{
int x = 70, y = 7;
printprime(x, y);
return 0;
}
This line in printprime() is likely the cause of your issue:
for ((d < x); d += y;);
That trailing semi-colon stops the body of your for loop from executing.
I've been working on a program in one of my college classes. I have been having trouble with the implementation of my LRU code as it is not displaying any errors or anything, but compiles. There are two parts. The main that we input the values into, which we then specify which algorithm we want to use to find page faults. I know the main works, along with the FIFO algorithm, but I'm not getting anything with my LRU code (It compiles and "runs" but displays nothing as if I did not click to use the algorithm). Can anyone help me figure out what is wrong?
main.cpp
#include <iostream>
#include <string>
//#include "fifo.cpp"
#include "lru.cpp"
//#include "optimal.cpp"
using namespace std;
int main() {
// List of different variables
string pagestring;
int fs,pn[50], n;
// Prompt for page references
cout<<"Virtual Memory Simulation\nBy blah\n----------\nEnter the number of pages : " << endl;
cin >> n;
cout<<"\n-------------\nPlease enter a list of page numbers separated by commas.\n"<< endl;
cin>>pagestring;
// algorithm to use
char algo;
while (true) {
// Prompt algorithm to use
cout<<"----------\nPlease select an algorithm to use.\n\n1: First-In-First-Out (FIFO)\n2: Least-Recently-Used (LRU)\n3: Optimal\n0: Quit\n"<<endl;
cin>>algo;
if (algo == '1') {
//fifo(pagestring);
}
else if (algo == '2'){
LRU_Execute(pagestring, n);
}
else if (algo == '3'){
cout<<"Optimal Not yet coded"<<endl;
}
else if (algo == '0'){
break;
}
else {
cout<<"Invalid choice. Please try again."<<endl;
}
}
cout<<"Goodbye!!"<<endl;
};
LRU.cpp
#include <iostream>
#include <string>
using namespace std;
class pra
{
int fs,z;
int frame[50], frame1[50][2], pn[50], n, cnt, p, x;
public:
pra();
void init(string pagestring);
void getdata(string pagestring, int n);
void lru(int* pn, int n, string pagestring);
};
pra::pra()
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::init(string pagestring)
{
int i;
for (i = 0; i < fs; i++)
{
frame[i] = -1;
}
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
p = 0;
cnt = 0;
}
void pra::getdata(string pagestring, int n)
{
fs=3;
// index to loop through input string
int i = 0;
// current input string character
char z = pagestring[i];
int x = 0;
//cout << "\nEnter the page numbers : ";
while (z != '\0'){
// skip over commas and spaces
if (!(z == ',')) {
pn[x] = z;
x++;
// cout<<pn[x]<<"-This is pn[x]\n";
}
z = pagestring[++i];
}
//cout<<pn[x]<<"-This is pn[x] AGAIN\n";
this->lru(pn, n, pagestring);
}
void pra::lru(int* pn, int n, string pagestring)
{
init(pagestring);
int ind = 0, fault = 0, pi = 0, j, fn;
char i, z;
p = 0;
cnt = 0;
int min;
cout<<n<<"---"<<i<<" - "<<j<<" - "<<" - "<<fn<<" - "<<z;
for (i = 0; i < fs; i++)
{
frame1[i][0] = -1;
frame1[i][1] = 0;
}
pi = 0;
for (i = 0; i < n; i++)
{
j = 0;
if (ind > fs - 1)
ind = 0;
fault = 1;
min = 999;
while (j < fs)
{
if (frame1[j][0] = pn[pi])
{
fault = 0;
p++;
frame1[j][1] = p;
goto l2;
}
if (frame1[j][1] < min)
{
min = frame1[j][1];
fn = j;
}
j++;
}
j = 0;
while (j < fs)
{
if (frame1[j][0] = -1)
{
fault = 1;
fn = j;
goto l2;
}
j++;
}
ind++;
l2:
if (fault == 1)
{
p++;
frame1[fn][0] = pn[pi];
frame1[fn][1] = p;
cnt++;
}
cout << "\nElement: " << pn[pi];
pi++;
for (z = 0; z < fs; z++)
{
cout << "\t" << frame1[z][0];
}
if (fault == 1)
cout << "\t**Page Fault**";
else
cout << "\t--No Page Fault--";
}
cout << "\nTotal number of page faults: " << cnt;
cout << "\n";
}
void LRU_Execute(string pagestring, int n)
{
pra p;
int j, fault = 0, i, pi, z, fn, ind = 0, ans, ch;
p.getdata(pagestring, n);
//p.lru();
while (ans == 1);
//return 1;
}